Posted by: Cirilo Meggiolaro | 05/9/2009

Tip of the day #207 – ASP.NET MVC – Architecture best practices – Moving the validation to the service layer

On Tip #206 we saw an example on how to create a service layer that may be used by both an ASP.NET MVC web application and a windows client application. The main idea was to show how to code against interfaces and how to consume on an ASP.NET MVC application.

If you have followed my ASP.NET MVC posts you will remember how important the dictionaries are in ASP.NET MVC applications and how UI validation works. If you haven’t read yet, it is a good time to read:

Today we are going to check how to add validation to our service layer and how to communicate back to the service consumer providing a list of messages if something goes wrong. The scenario and examples are the same from the Tip #206.

First of all let’s create our communication mechanism. We are going to provide a dictionary of messages to the consumer application. Again we are going to develop against an interface so you can create a concrete class for each or your applications that handles UI in a way that fits better for each application.

Let’s create an interface called IValidationResult containing methods to either add or remove an entry from a dictionary and a property that returns a Boolean flag indicating whether the operation succeeded or not.

public interface IValidationResult
{
    void Add(string key, string message);
    void Remove(string key);
    bool Succeeded { get; }
    IDictionary Details { get; }
}

Now, let’s change our concrete service class to store an object that implements the IValidationResult interface and change its constructor:

public class ClientStockService : IStockService
{
    private IValidationResult _validationResult;

    public ClientStockService(IValidationResult validationResult)
    {
        this._validationResult = validationResult;
    }

    // Code removed for brevity
}

Now you need to change the StockController class to provide an object that implements the IValidationResult interface. One way to implement it is to create a class that stores the messages in a dictionary that you can use anywhere in your code.

public class ValidationResult : IValidationResult
{
    private IDictionary _validationResult = new HybridDictionary();

    public void Add(string key, string message)
    {
        _validationResult[key] = message;
    }

    public void Remove(string key)
    {
        _validationResult.Remove(key);
    }

    public bool Succeeded
    {
        get
        {
            return _validationResult.Count == 0;
        }
    }

    public IDictionary Details
    {
        get
        {
            return _validationResult;
        }
    }
}

This is an example that may be used in different application types.

Changing the controller class will result in a code similar to the following:

public class StockController : Controller
{
    IValidationResult _validationResult;
    IStockService _stockService;

    public StockController()
    {
        _validationResult = new ValidationResult();
        this._stockService = new ClientStockService(_validationResult);
    }

    // Code removed for brevity
}

Advertisements

Responses

  1. You´re a guy ahead of time dude…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: