Vlad's blog

In programming veritas

Posts Tagged ‘Patterns

DisposableAction

with one comment

While programming is quite common situation where some logic is implemented in three phases:

  1. Initialization
  2. Business logic
  3. Finalization

In C# is commonly used for this operator using, which gets a reference to the IDisposable interface in the beginning of the block and calls the Dispose() method at the end. The problem is that the IDisposable interface is implemented at the class level while the initialization-logic-finalization strategy usually refers to the methods of the class. Suppose that we implement a class HtmlReportBuilder, which generates a HTML report. Method Begin() starts the procedure of constructing the report, creates a buffer for HTML content, and adds the required html and body tags. Methods InsertRow(), InsertSummary() implement some business logic of building a report. Method of Finish() adds the closing html and body tags and finalizes report generation.

class HtmlReportBuilder
{
    private StringBuilder _html;

    public string Html { get { return _html.ToString(); } }

    public void Begin()
    {
        _html = new StringBuilder();

        _html.AppendLine("<html><body>");
    }

    public void Finish()
    {
        _html.AppendLine("</body></html>");
    }

    public void InsertRow() {}
    public void InsertSummary() {}
}

This class is used as follows.

var builder = new HtmlReportBuilder();
builder.Begin();
builder.InsertRow();
builder.InsertSummary();
builder.Finish();

In this implementation, the call Finish() is optional, that is not very convenient. I would like to Finish() is invoked implicitly. For this problem, there is a fairly simple and elegant solution – DisposableAction.

public class DisposableAction : IDisposable
{
    private readonly Action _action;

    public DisposableAction(Action action)
    {
        _action = action;
    }

    public void Dispose()
    {
        if (_action != null)
            _action();
    }
}

DisposableAction class implements the IDisposable.The constructor receives a delegate that is invoked in the method Dispose().This class is used as follows.

class HtmlReportBuilder
{
    private StringBuilder _html;

    public string Html { get { return _html.ToString(); } }
        
    public IDisposable Begin()
    {
        _html = new StringBuilder();

        _html.AppendLine("<html><body>");

        return new DisposableAction(() =>_html.AppendLine("</body></html>"));
    }
}

Method Begin() now returns a reference to IDisposable, which can be used in using.

var builder = new HtmlReportBuilder();
using(builder.Begin())
{
    builder.InsertRow();
    builder.InsertSummary()
}
Console.WriteLine(builder.Html);

When application leaves the using block, it will automatically be invoked delegate, which will add the finishing body tags, and html.

Advertisements

Written by vsukhachev

November 8, 2011 at 4:02 am

Posted in Development

Tagged with ,

Specification pattern

leave a comment »

Specification pattern is intended to solve the problem of entities selection according to particular search criteria. The main idea is to move selection logic from class representing entity into individual specification class.
Let’s consider online bookstore application like Amazon. A book is represented by Book class that has Title, Price and ReleaseYear attributes.

public class Book
{
    private readonly List _offerings;

    public int Id { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public decimal ReleaseYear { get; set; }
    public IEnumerable Offerings { get { return _offerings; } }
}

Each Book might have one or more related Offering class instances. Offering stores information about quantity of books available at particular stock. There is one-to-many relationship between Book and Offering.

public class Offering
{
    public int Id { get; set; }
    public Book Book { get; set; }
    public string Address { get; set; }
    public int Quantity { get; set; }
}

There is the following search criteria defined.

  • Publication date in the specified range
  • Available in stock
  • Price in the specified range

The first thing that comes to mind is to add corresponding search methods in Book class. But this approach becomes inconvenient when amount of search criteria grows up. In this case probably the better solution is to move selection logic into separate specification class. In our example a specification is represented by IBookSpecification interface that declares single SatisfiedBy method. The method receives a collection of Book entities and returns a collection that satisfies particular search criteria.

public interface IBookSpecification
{
    IEnumerable<Book> SatisfiedBy(IEnumerable<Book> books);
}

Read the rest of this entry »

Written by vsukhachev

June 7, 2011 at 5:46 am

Posted in Development

Tagged with ,