Vlad's blog

In programming veritas

Posts Tagged ‘C#

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.

Written by vsukhachev

November 8, 2011 at 4:02 am

Posted in Development

Tagged with ,

Creating unit tests with Rhino.Mocks

leave a comment »

When you write unit tests you often need to create stubs for classes, interfaces and methods used in method which is tested. This task is tedious because you usually have to write a lot of code. One way how to automate this process is use isolation framework. One of the most popular isolation frameworks in .Net is Rhino Mocks.
For the sake of demonstration suppose we develop a system for collecting telemetry data from remote analogue devices. A device is represented by the following class.

public class Analog
{
    public string Name { get; set; }
    public double Value { get; set; }
    public double LowLimit { get; set; }
    public double HiLimit { get; set; }
}

LowLimit and HiLimit properties define low and high alarm limit respectively. If current value is greater or equal to HiLimit the High alarm will be generated. Similarly, if value is less or equal to LowLimit a system will generate the Low alarm. Alarm is represented by the following class.

public class Alarm
{
    public string AnalogName { get; set; }
    public string Description { get; set; }
}

Data Access Layer defined by the following interfaces.

public interface IAnalogDao
{
    Analog Find(string name);
    void SaveChanges(Analog a);
}

public interface IAlarmDao
{
    void AddAlarm(Alarm alarm);
}

public interface IAlarmFactory
{
    Alarm CreateAnalogHiAlarm(string name, double value);
    Alarm CreateAnalogLowAlarm(string name, double value);
}

Our goal is to implement AnalogProcessor class that is responsible for alarms generation.

public class AnalogProcessor
{
    private IAlarmDao _alarmDao;
    private IAlarmFactory _alarmFactory;
    private IAnalogDao _analogDao;

    public AnalogProcessor(IAlarmDao alarmDao, IAlarmFactory alarmFactory, IAnalogDao analogDao)
    {
        _alarmDao = alarmDao;
        _analogDao = analogDao;
        _alarmFactory = alarmFactory;
    }

    public void Process(string name, double value)
    {
        Analog a = _analogDao.Find(name);

        if (a != null)
        {
            if (value >= a.HiLimit)
            {
                Alarm alarm = _alarmFactory.CreateAnalogHiAlarm(a.Name, value);
                _alarmDao.AddAlarm(alarm);
            }
            if (value <= a.LowLimit)
            {
                Alarm alarm = _alarmFactory.CreateAnalogLowAlarm(a.Name, value);
                _alarmDao.AddAlarm(alarm);
            }

            a.Value = value;
            _analogDao.SaveChanges(a);
        }
    }
}

Read the rest of this entry »

Written by vsukhachev

September 19, 2011 at 8:09 am

Posted in Development

Tagged with ,

Aspect-oriented programming in PostSharp

with one comment

Every application has a lot of code that can not be considered as a part of domain model or infrastructure. Examples are tracing, validation, caching and logging. This functionality is also called crosscutting concerns because it “cuts across” multiple abstractions in a program. Aspect-oriented programming (AOP) provides a way how to extract crosscutting concerns from code in separate components. Postsharp is one of the most popular AOP frameworks in .Net land. Classic AOP approach introduces a number of new concepts such as aspect,advice,pointcut. Postsharp suggests two model of programming. The first is straightforward and based on well known ideas of classes and method overloading. The second approach is more AOP centric and requires a long discussion. We will be focused on the first approach in this post.
Read the rest of this entry »

Written by vsukhachev

September 6, 2011 at 11:29 am

Posted in Development

Tagged with , ,

Introduction to Code Contracts

leave a comment »

Starting from .Net 4.0 we have a support of new design by contracts technology called Code Contracts. This post is quick introduction to Code Contract’s basic elements: preconditions, postconditions and invariants with practic examples how to use them.

Suppose we develop an application for online flight booking service. User should go through the following steps in order to book a ticket.

  • Choose a seat from seat map
  • Login into the system
  • Payment

This logic is usually implemented as series of screens that follow each other. The Command pattern can be used in the implementation. Consider ICommand interface which must be implemented by each screen.

Read the rest of this entry »

Written by vsukhachev

July 12, 2011 at 4:15 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 ,