WCF and SOA Tips

Steve Eichert has posted a set of WCF and SOA Tips on this blog.

Advertisements

The Builder Pattern

Nat Pryce has a good blog post on using the Builder pattern.

I especially like the result:

"Another benefit is that the test code is easier to write and read because the parameters are clearly identified. Compare:"

TestAddresses.newAddress( "Sherlock Holmes", "222b Baker Street", "London", "NW1"); to: new AddressBuilder() .withName("Sherlock Holmes") .withStreet("222b Baker Street") .withCity("London") .withPostCode("NW1", "3RX") .build();

I hope to find some opportunities to use this in the code I write.  This is the type of syntax I really like in the NHibernate ICriteria queries that are built.

Update: the correct term is ‘fluent interfaces’ – here is another example from the Eleutian SpeakENG blog on ‘Fluent Fixtures’

Dependency Injection/IoC MSDN Articles (links)

It’s common outside of the .NET world of programming (ie. Java) to hear of using inversion of control containers.  Much of this is spurred on by Martin Fowler and his ‘Patterns of Enterprise Application Architecture’ book that provide ways to right good software.  In this book he covers Inversion of Control containers and Dependency Injection pattern.

The use of these containers following the DI pattern, which in my opinion has helped me write code that follows the DRY (‘do not repeat’) and the SRP  (‘single responsibility principle’) .  Both critical to writing loosely coupled and cohesive software.  I find with DI I tend to allow create software that is based on a service and component model with the emphasis on design to interfaces.

That all being said, in the .NET world it can sometimes be hard to find these examples.  Your certainly not going to find them in a C# book or introduction to asp.net.  It took me years of programming myself before I had even heard of these concepts!

So, I have found two MSDN articles that cover Inversion of control containers.  One that uses Castle’s Windsor and the other using Spring.NET (those in the java world are familiar with Spring).

Building Distributed Applications: Inversion of Control and Dependency Injection: Working with Windsor Container

Design Patterns: Dependency Injection (Spring.NET)

Enjoy!

Fluent interface refactoring example

Joey Beninghove has an excellent example in his blog post Refactoring towards a DRY fluent interface

I like the way he first shows the need for the refactor and the reasons why. I especially like the use of the anonymous delegates. I’ve been a big fan of anonymous delegates in my querying of generic collections with the List in C#. I typically use an inline anonymous delegate – his example shows both.

Notice the use of the injection from the IoC container (I’m fairly certain he is using Windsor).

The first time I really have been able to work with the fluent interfaces is with the ICriteria in NHibernate. The ability to chain together the queries offers a powerful and and easy to comprehend syntax.

The example given in the above link:

IList cats = sess.CreateCriteria(typeof(Cat))
    .Add( Expression.Like("Name", "Fritz%") )
    .Add( Expression.Or(
        Expression.Eq( "Age", 0 ),
        Expression.IsNull("Age")
    ) )
    .List();

Joey’s article gives a good explanation and example of how to do this in your own solutions.

Go check out the article, it’s good to see these examples!