Friday, September 16, 2011

Seterlund.CodeGuard is now on NuGet

My small guard library is now available on NuGet.

The package name is Seterlund.CodeGuard and is available under the New BDS License.

The Guard.That(...) will throw an exception, when some condition is not met

public void SomeMethod(int arg1, int arg2)
{
    // This line will throw an exception when the arg1 is less or equal to arg2
    Guard.That(() => arg1).IsGreaterThan(arg2);

    // This will check that arg1 is not null and that is in some range 1..100
    Guard.That(arg2).IsNotNull().IsInRange(1,100);

    // Several checks can be added.
    Guard.That(arg1)
      .IsInRange(100,1000)
      .IsEven()
      .IsTrue(x => x > 50, "Must be over 500");

    // Do stuff
}


The Validate.That(...) method makes it possible to return a list of all error conditions

public void OtherMethod(int arg1)
{
    // Get a list of errors
    List<string> errors = Validate.That(() => arg1).IsNotNull().GetResult();
}
---
Share:

Tuesday, September 13, 2011

Howto create a simple IOC container

I'm using StructureMap in my current project and it works pretty well.
But how difficult could it be to make a simple IOC container. So I set out to create my own implementation and it proved to be quite easy to get the basics down.

How to setup the container.
var c = new Container();
c.For<IFoo>().Use<Bar>();

In case you need more controll over the creation of the class creation, I have added the posiblitity to use lamdas.
c.For<IFoo>().Use(() => new Bar1("someArgument"));
c.For<IFoo>().Use(ctx => new Bar2(ctx.Get<ISomeinterface>()));

How to resolve
var foo = c.Get<IFoo>();

The road ahead could be to handle scope/lifecycle.

The complete code can be found on GitHub

---
Share: