Project Description
Suture is a project that provides a mechanism for using the builder pattern using a late bound fluent interface.

Example

Consider the following class:

public class MyClass
{
    public MyClass(IFoo foo, IBah bah)
    {
        this.Foo = foo;
        this.Bah = bah;
    }
    
    public IFoo Foo { get; set; }
    public IBah Bah { get; set; }
}

Creating an instance of this class in a test would require you to create stubs or mocks for these dependencies. With Suture any dependencies you don't supply are automatically stubbed. The following is a simple example where all the dependencies are automatically stubbed.

[Test]
public void Should_automatically_stub_dependencies()
{
    var sut = Builder.For<MyClass>().Build();
    Assert.That(sut.Foo, Is.Not.Null);
    Assert.That(sut.Bah, Is.Not.Null);
}


Suture allows you to override the default stubbing on a per parameter basis using a fluent interface.
[Test]
public void Should_use_argument_provided()
{
   var expected = new Foo();
   var sut = Builder.For<MyClass>()
                     .WithFoo(expected)
                     .Build();

   Assert.That(sut.Foo, Is.EqualTo(expected));
}


By default Suture uses the greediest matching constructor. What this means is that if you don't override a dependency the greediest constructor will be used, however if you specify a dependency using the "With_xxx" method then the greediest constructor that matches the dependencies you specify will be used.

For example:
public class MyClass
{
    public MyClass(IFoo foo, IBah bah)
    {
        ...
    }

    public MyClass(IBaz baz)
    {
        ...
    }
}
...

//The second constructor will be called here as it is the only one that has a dependency on "baz"
Builder.For<MyClass>()
                  .WithBaz(new Baz())
                  .Build();


If this behaviour is not flexible enough, then Suture has an answer for that too. You can target a specific constructor using the UsingConstructor method. This method matches the constructor to the generic type arguments supplied.

//Using MyClass from the previous example you can explicitly target the second constructor
Builder.For<MyClass>()
          .UsingConstructor<IBaz>()
          .Build();

Last edited Jun 28, 2013 at 10:57 AM by garethevans, version 7