Archive for July, 2006

Encapsulate Collection refactoring with generics

Encapsulate Collection is a refactoring from Martin Fowler’s book Refactoring: Improving the Design of Existing Code, the refactoring is applied when you have a class with a public property of a collection type, the intent is to stop the collections internal data structures from being exposed.

I was thinking about how to apply this refactoring using .NET 2.0 and generics and decided on the implementation below;

public class Order
{
private List<IOrderLine> _orderLines;

public ReadOnlyCollectionList<IOrderLine> OrderLines
{
get { return _orderLines.AsReadOnly(); }
}

public Order()
{
_orderLines = new List<IOrderLine>();
}

public void AddOrderLine(IOrderLine orderLine)
{
_ orderLines.Add(orderLine);
}

}

The generic ReadOnlyCollection Class provides a base class for a generic readonly collection, using this class as opposed to the generic IList, has the advantage that someone implementing the class can see at design time that the class is readonly; however the generic IList may be a better alternative as more of collections internal data structure is hidden: any thoughs on this would be much appreciated.
Refactoring: Improving the Design of Existing Code

Comments del.icio.us

A New Project with .Net2.0 and ORM

I am have just started a new .Net 2.0 project in my spare time, I will be developing an estimating app. We are soon to be moving over to .Net 2.0 in work, so hopefully this project will give me a chance to get up to speed.

I also want to try out some new things, like Object Relational Mapping, which really seems like a big step forward; this was one of the features that really impressed me about Rails. Rails enforced the Active Record Pattern; I assume that .Net implementations will be a little more flexible.

One of the things that concerned me initially about object relational mapping was the potential performance hit over the traditional approach of using stored procedures, in the book I am currently reading Applying Domain-Driven Design and Patterns: With Examples in C# and .NET the author suggests that ORM could be used for the majority of the code, with performance critical parts being written with stored procedures; this really seems like a good idea to me, I can imagine the time that could be saved on the majority of projects using this approach.

Applying Domain-Driven Design and Patterns: With Examples in C# and .NET

Comments del.icio.us