Wednesday, December 28, 2011

Introduce NValidator


I had spent 2 weeks before Christmas to develop a small library to do the validation. It's a lot similar to FluentValidation in syntax. Actually, the implementation and fluent syntax are inspired from this well-known library Fluent Validation. However, I implemented it using Chain of Reponsibility pattern and made it support Dependency Injection for validator classes. Here is a simple example that can help you to start:
public class UserValidator : TypeValidator<User>
{
    public UserValidator()
    {
        RuleFor(user => user.Id)
            .NotEmpty()
            .NotNull();

        RuleFor(user => user.UserName)
            .NotNull()
            .Length(6, 10)
            .Must(x => !x.Contains(" ")).WithMessage("@PropertyName cannot contain empty string.");

        RuleFor(user => user.Password)
            .StopOnFirstError()
            .NotEmpty()
            .Length(6, 20)
            .Must(x => x.Any(c => c >= 'a' && c <= 'z') &&
                       x.Any(c => c >= '0' && c <= '9') &&
                       x.Any(c => c >= 'A' && c <= 'Z')).WithMessage("@PropertyName must contain both letter and digit.");

        RuleFor(user => user.Title)
            .In("MR", "MS", "MRS", "DR", "PROF", "REV", "OTHER");

        RuleFor(user => user.FirstName)
            .NotEmpty().WithMessage("Please specify the first name.");

        RuleFor(user => user.LastName)
            .NotEmpty().WithMessage("Please specify the last name.");
        
        RuleFor(user => user.Email)
            .Email();

        RuleFor(user => user.Address)
            .SetValidator<AddressValidator>()
            .When(x => x != null);
    }
}
I'm currently using this library for a heavy server side validation project and so far so good. You may ask me why don't I just use FluentValidation instead of reinventing the wheel? My answer is I want to keep myself busy. I have a passionate on Fluent interfaces for a while and this is a good opportunity for me to "do something" :D Anyway, the source code repository for this project is on GitHub. The NuGet package "NValidator" is also available. Please checkout the document and other topics to get started.
Happy New Year.

Monday, November 21, 2011

Validation attribute on nested list item

I'm working on some data annotation validation stuff and today I came to a requirement to validate a type nested property which is a list of string. The requirement is quite simple, just make sure every string in the list must be numeric. NumericAttribute is a custom ValidationAttribute to validate a string and to check whether it contains only numeric. Well this is a very basic example and I'm thinking futher, may be I'll need to validate different object type in the list instead of simple string and we might need different validation logic to apply on them. That leads me to this post:
Let's say I have an object definition like below:
public class Parent
{
    public List<ChildObject> Children {get;set;}
}

I want to have a validation attribute to decorate on the Children property and when the validation process happen, that attribute will validate every single ChildObject item in the list.
The attribute I want is a derived of ValidationAttribute, take different types of other Validation Attributes as the constructor parameters. And certainly, when override method IsValid, it will loop through the validation attributes set in the constructor to validate all items in the list:
public class ValidateItemUsingAttribute : ValidationAttribute
{
    private readonly Type[] _attributeTypes;

    public ValidateItemUsingAttribute(params Type[] attributes)
    {
        Check.Requires<ArgumentNullException>(attributes != null);
        Check.Requires<ArgumentException>(!attributes.Any(x => !typeof(ValidationAttribute).IsAssignableFrom(x)));
        _attributeTypes = attributes;
    }

    public override bool IsValid(object value)
    {
        if (value == null || !(value is IEnumerable) || value is string)
        {
            return true;
        }

        var index = 0;
        foreach(var item in value as IEnumerable)
        {
            foreach (var type in _attributeTypes)
            {
                var validator = Activator.CreateInstance(type) as ValidationAttribute;

                var validationResult = validator.GetValidationResult(item, new ValidationContext(item, null, null));
                if (validationResult != null)
                {
                    ErrorMessage = validationResult.ErrorMessage;
                    MemberName = string.Format("{0}[{1}]", MemberName, index);
                    return false;
                }
            }
            index++;
        }
        return true;
    }
}

Because this attribute uses Activator to create instance of other validation attribute, that require those nested validation attributes to have parameterless constructor. So for example we need to use type of StringLengthAttribute for the constructor, we may implement a derived class of StringLengthAttribute and use the new type for the constructor :D
public class Parent
{
    [ValidateItemUsing(typeof(SomeValidationAttribute), typeof(AnotherValidationAttribute))]
    public List<ChildObject> Children {get;set;}
}

Anyways, I hope this is a good idea. Cheers.

Monday, November 7, 2011

WCF Validation Engine with Data Annotation style like ASP.NET MVC

    There are many articles about WCF validation. Some are about using WCF with Microsoft Application Block, many others are about using Data Annotation. But there is not any thing like the validation engine of ASP.NET MVC. Honestly, I'm very impressed of MVC Team's work. The code is very SOLID that makes it too easy to extend without changing the implementation. There are still something i don't like in the Framework but overall, I think it's an awesome library. I have been working with ASP.NET MVC for over 2 years and now I'm having a chance to go back to WCF. I realized that WCF is designed a lot as open as MVC, there are so many extensible points in WCF that allows users to customize the services behavior, validation is one of them. I expected to find some existing code to use for WCF Validation like the one in MVC for me to copy and paste but there is not. I wanted to have a framework that we can switch any validation logic any time, just change the provider. Therefore, I decided to copy some of the implementation from MVC source code and use for this WCF validation demo.

    Firstly, I want to say about the way MVC Validation engine work. If you have ever read the MVC source code, you mush have known that in MVC, there is a ModelMetadata provider which will read the view model class and make a ModelMetaData. Then there is one or many ModelValidators which are registered before to create validator objects base the model metadata. Each validator will produce ValidationResults when validate the object. The validation process will happing during model binding and before Action method is executed. If there is an error, the error information will be append to Model State of current Controller context. And finally, in side the action method, we'll check the ModelState.IsValid to decide what to do.

    So I guess the MVC validation engine will not be able to validate nested object. But in MVC application, we still can see the error if nested object has validation error. I'm pretty sure that error is found while model binding process happen. In WCF, we don't have to worry about the model binder thing so one of the problem I have to solve is making the code validate nested object and child objects in an enumerable.

    Secondly, the ModelMetadata class and the ModelMetadataProvider classes in MVC aware of ControllerContext object which I don't want to use and there is not any reason to use it in WCF application so the next problem for me is trimming any thing related to ControllerContext.

    And finally, The MVC validation engine itself contains logic for Client validation. Again, these things will add no value for a WCF application. My aim would be copying only the code needed for validation the contract parameters in WCF.

    You may ask me why not using the classes in System.Web.Mvc.dll instead of rewriting/copying them. The answer is we cannot use that dll outside a web environment due to the assembly setting of that library.

    Alright, the way I inject validation logic into WCF method is making a IOperationBehavior as an Attribute to decorate on the action method. It's pretty much the same as other's guide. Here is the implementation:

[AttributeUsage(AttributeTargets.Method)]
public sealed class ParameterValidatorAttribute : Attribute, IOperationBehavior
{
    public ParameterValidatorAttribute()
    {
        ThrowErrorOnFirstError = false;
        ThrowErrorAfterValidation = true;
    }

    public bool ThrowErrorOnFirstError { get; set; }
    public bool ThrowErrorAfterValidation { get; set; }

    void IOperationBehavior.Validate(OperationDescription description)
    {
    }

    void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
    {
    }

    void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
    {
    }

    void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
    {
        dispatch.ParameterInspectors.Add(new ParameterValidatorBehavior(ThrowErrorOnFirstError, ThrowErrorAfterValidation));
    }
}

    The ParameterValidatorBehavior implements IParameterInspector, before a service method is called, it will invoke the validation engine to validate every single input parameter. If there is a validation error, the error will be appended to ModelState object. That requires the service implementation must implement the following interface
public interface IHasModelStateService
{
    ModelState ModelState { get; set; }
}

    Here is the code of the parameter inspector:
public object BeforeCall(string operationName, object[] inputs)
{
    // validate parameters before call
    var serviceIntance = OperationContext.Current.InstanceContext.GetServiceInstance() as IHasModelStateService;
    if (serviceIntance != null)
    {
        if (serviceIntance.ModelState == null)
        {
            serviceIntance.ModelState = new ModelState();
        }
        if (serviceIntance.ModelState.Errors == null)
        {
            serviceIntance.ModelState.Errors = new List<ModelError>();
        }

        IEnumerable<ModelValidationResult> validationResults = new ModelValidationResult[] { };
        foreach (object input in inputs)
        {
            if (input != null)
            {
                ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => input, input.GetType());

                validationResults = ModelValidator.GetModelValidator(metadata).Validate(null);
                foreach (ModelValidationResult validationResult in validationResults)
                {
                    var temp = validationResult;

                    if (ThrowErrorOnFirstError)
                    {
                        throw new FaultException<ValidationFault>(new ValidationFault(new[] { temp }), "Validation error");
                    }

                    serviceIntance.ModelState.Errors.Add(new ModelError
                    {
                        MemberName = temp.MemberName,
                        Message = temp.Message
                    });
                }
            }
        }
        if (ThrowErrorAfterValidation && !serviceIntance.ModelState.IsValid)
        {
            throw new FaultException<ValidationFault>(new ValidationFault(validationResults), "Validation error");
        }
    }
    return null;
}

    Summary, to use this library. We need to do following steps:
  • Decorate the service method with [FaultContract(typeof(ValidationFault))]
  • Decorate the service method implementation or contract with [ParameterValidator], by default it will throw fault exception if there is validation error
  • Make service implementation implement interface IHasModelStateService
  • If we dont want to throw exception when validation error, we can check the ModelState.IsValid like MVC way and do whatever we want:

    if (!ModelState.IsValid)
    {
        var error = new StringBuilder();
        foreach (var e in ModelState.Errors)
        {
            error.Append(string.Format("Validation error on {0}:{1}\n", e.MemberName, e.Message));
        }
        throw new FaultException(error.ToString());
    }

    That's it. If we want to use Microsoft Validation Application Block or Fluent Validation, just replace the ModelMetadataProvider and ModelValidatorProvider like what people did with ASP.NET MVC. There is definitely alot of things to improve such as implementing an IgnoreValidationAttribute on a certain complex property of a view model or supporting the validation with validation attribute decorated inside the method contract, etc. I would happy to implement all of them when I have a chance to apply this stuff in my real project. Now it's pretty enough for my birthday night :D

Please refer to full source code here: https://github.com/vanthoainguyen/Blog/tree/master/WCF.Validation.Demo Cheers.

Saturday, October 29, 2011

Autofac, AutoMapper and custom converter with dependency injection

Whenever I work with a library or a technology, one of the question in my mind is how good is it to support dependency injection. I came back to use Autofac in a company's project recently and that question is the one I need to find the answer. Basically, Autofac is a pretty cool library to help converting between domain objects and view models. For example:
Mapper.CreateMap<Order, OrderViewModel>();

Or even better:
Mapper.CreateMap<Order, OrderViewModel>()
      .ConvertUsing<OrderConverter>();

The good thing of Automapper is that it supports very good custom converters following the way above. Most of the cases, the custom converter is just a simple class to map from fields to fields when the structure of the domain object is so complicated. Well, it's good if we can make it simple at first place but life is not that easy. The project I'm working on was created by an offshore team and everyday looking to the code, I just wonder to myself: "WTF is this shit"
The domain object is so complicated to map to the View Model by convention. ANd the ViewMOdel itself is complicated as well. It has logic to access WCF service to fetch domain data and map to itself :D. I'm spending my time to move the code to where it should be and using AutoMapper to map things is one of the steps. I hope you will never face anything like this, but in case you would and need a custom converter to access a service, this is the post for you.
Now, my need is that the OrderConverter will need to call a service method to fetch some data while mapping objects. I also want the service interface to be the dependency of the converter, it will be the parameter in OrderCOnverter constructor and when we call Mapper.Map, the dependency will be injected by a IOC library, here i use Autofac. Below is the naive implementation of OrderConverter:
public class OrderConverter : ITypeConverter<Order, OrderViewModel>
{
    private readonly IOrderService _orderService;

    public OrderConverter(IOrderService orderService)
    {
        _orderService = orderService;
    }

    public OrderViewModel Convert(ResolutionContext context)
    {
        var order = context.SourceValue as Order;
        if (order == null)
        {
            return null;
        }

        var orderDetails = _orderService.GetOrderDetailsByOrderId(order.Id);
        return new OrderViewModel
        {
            Id = order.Id,
            Details = Mapper.Map<IEnumerable<OrderDetails>, IEnumerable<OrderDetailsViewModel>>(orderDetails).ToList()
        };
    }
}

I thought Autofac should have supported the way to resolve Converter. I didn't know how to do it until reading through the code. The Mapper static class has a method Initialize and through this, we can pass in custom logic to resolve objects.
// AutoMapper initialization
Mapper.Initialize(x =>
{
     x.ConstructServicesUsing(type => container.Resolve(type));
});

The tricky thing is we need to call that method before we register mapper classes. The way I register mapper classes is using AutoFac scanning assemblies and make the mapper class implement IStartable.
var builder = new ContainerBuilder();
// startable which include mapper classes
builder.RegisterAssemblyTypes(assemblies)
       .Where(t => typeof(IStartable).IsAssignableFrom(t))
       .As<IStartable>()
       .SingleInstance();

These lines suppose to be after the Initialize method. I made the wrong mistake to put them in wrong order so the code couldn't work :D. I use the same way to register all the custom converters:
// converters
builder.RegisterAssemblyTypes(assemblies)
       .AsClosedTypesOf(typeof(ITypeConverter<, >))
       .AsSelf();

These lines would be executed in the BootStrapper before the main application run:
BootStrapper.Run();

var order = new Order {Id = 1};

try
{
    var viewModel = Mapper.Map<Order, OrderViewModel>(order);

    Console.WriteLine(string.Format("\nThere are {0} order details", viewModel.Details.Count));
}
catch (Exception ex)
{
    Console.WriteLine(ex.StackTrace);
}
finally
{
    Console.ReadLine();
}

That's it. Please check out the full application on github: https://github.com/vanthoainguyen/Blog/tree/master/TestAutoMapper

Friday, September 23, 2011

Combine logic of MVC FilterAttribute classes

    Hi mates, today I would like to write another blog about the reason why I need an Autofac IContainer as the dependency in some of my classes. In my team, we are developing a website that can allow users create and manage information about organisations. We have frontend pages for normal users to manage their organisations and we also have backend pages for the administrator to do that. So on frontend pages, we created a custom MVC filter attribute to decorate on every action methods that requires a need to check whether current user has the permission to view or edit the organisation. This filter attribute is also used in other places where we need similar authorization logic. On the other hand, we just need built-in Authorize attribute for all the admin pages. That looks like a straightforward solution, right?

    However, life is not that easy. Life is like a dick and sometime it's getting hard for no reason :D. The team decided to use MVC areas for Administrator and Organisations. That means the OrganisationManageController will be shared between those areas. Therefore, the authorization requirement would be: "Either the user is admin or the owner of the orngaisation, let him in". The first thing poped up in my mind was creating another filter attribute, inherit from Authorize filter attribute and copy the logic of my custom filter attribute which willl check the ownership. But not long after that, I realized it was so naive and ugly since the logic will be duplicated in those 2 attributes. Not only that, I was going to violate the cross-concern of the MVC Filter attributes because each of them should care and know about one and only one responsibility.

    Then I come up with the idea i'm gonna write in this blog. I decided to create another Filter attribute that implements IAuthorizeFilter. It will be resonpsible for executing all the nested IAuthorizeFilter attributes and let the user pass in if one of the filter was satisfied. The usasge will look like:
[HttpGet, Either(typeof(AuthorizeAttribute), @"Roles = ""Administrator""", typeof(CheckOwnerAttribute))]
public ActionResult Edit(ObjectId id)
{
	// ..........
}

    The EitherAttribute class will have 1 public constructor that accepts an array of objects, begin by the type of an IAuthorizeFilter, followed by it's setter information or parameters for it's constructor or both and so on. So on method OnAuthorization, it will iterate through the child filters and execute method OnAuthorization on this filter. If after executing, the filterContext.Result is modified which means the user is not authorized, i'll reset the result and go for next filter. If it's the last filter which modified the filterContext.Result, i'll just simply return. Here is the implementation:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class EitherAttribute : FilterAttribute, IAuthorizationFilter
{
    // I need a dependency of ContainerFactory here
    public Func<IContainer> ContainerFactory { get; set; }
    
    private readonly object[] _filterConstructDescriptions;

    public EitherAttribute(params object[] filterConstructDescriptions)
    {
        _filterConstructDescriptions = filterConstructDescriptions;
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        List<IAuthorizationFilter> _filters = CreateFilters(_filterConstructDescriptions);
        for (var i = 0; i < _filters.Count(); i++)
        {
            var filter = _filters[i];
            filter.OnAuthorization(filterContext);
            if (filterContext.Result == null)
            {
                return;
            }
            
            // Check next authorize filter
            if (i < _filters.Count() - 1)
            {
                filterContext.Result = null;
            }
        }
    }

    // ..........
}

    There is one interesting point to note here is the EitherAttribute has a dependency on Autofac IContainer because the nested filter will be created by Activator and if it has dependency on some services, we can use IContainer to inject require informations in. That's the reason I wrote about in previous post.

    So with this awesome attribute, I don't have to duplicate the logic across different attributes and now can use it for many different IAuthorizeFilter classes. There is only 1 limitation which is the type it support is IAuthorizeFilter only but I haven't known yet any other cases that need to extend the EitherAttribute to support other filter types.

    So please checkout the source code that includes some unit tests. Cheers

Thursday, September 22, 2011

Resolve Autofac IContainer Dependency Injection

    Today, i come to a need of using Autofac IContainer somewhere in my code. I know it's a bad practice using IContainer this way but i'm pretty sure sometime we must violate the convention. I'll show you the reason and the case on next post. Now i just say about how to have the IContainer itself resolvable in some of the classes. I guess perhaps that's the intention of the Autofac author not to make the IContainer interface to be the dependency of classes. However, we have the ServiceLocator anyway so it's not that easy to force the developer following best practices.
    Okey, here is the problem. We have the ContainerBuilder to register types, instances, etc and in the end we build the container builder to return the container instance. So If I register the instance of IContainer using the way below, it's not gonna work:
var builder = new ContainerBuilder();
// Register other dependencies in your app

IContainer container = null;
builder.RegisterInstance(container);

container = builder.Build();

The reason is we cannot register a null object. So here is the work around:
var builder = new ContainerBuilder();
// Register other dependencies in your app

IContainer container = null;
Func<IContainer> factory = () => container;
builder.RegisterInstance(factory);

container = builder.Build();

And in our code, we will make the factory to be it's dependency:
public class MyClass
{
    public MyClass(Func<IContainer> containerFactory)
    {
    }
}

Dirty, but works :D

Thursday, August 25, 2011

Yet another way programming to the Interface In JavaScript, sort of

    i have just blogged about inheritance in Javascript, so next target would be programming to the interface.     I had two approaches in mind. The first solution is making an interface as a normal object that contains properties and methods. This object will be static and remain unchanged during the lifetime of the application. The second solution is making the interface using the same way as creating the class in previous post. That means the type of the interface is also a javascript "function". And because i want to use the operator instanceOf in javascript to check whether an object is an instance of a given type so apparently, the first solution will not work. So I have only 1 option and I need to make the interface not to be initialised like a class. Obvisously, you can't create an object with just an interface definition, can you?
    Alright, an interface would have a name and a constract that contains it's members. So I will make an utility which can return a function represents our interface:
function interface(name, contract){
    var _interface = function() {
        if (this._isImplementing !== true){
            throw 'Cannot initialize interface "' + name + '"';
        }    
        
        if (name === '' || typeof name !== 'string'){
            throw 'Must provide interface name';
        }
        
        if (contract == null || typeof contract !== 'object') {
            throw 'Must provide a contract as an object';
        }
        
        this._name = name;            
        this._contract = contract;    
    };        
    return _interface;
}

    I'm using a flag _isImplementing to avoid creating instances using above function. Now I need a method similar to method "Inherit" in previous post but instead of inheriting from a class, it would implement the interface and make the class has all members of the interface's contract:
Object.defineProperty(Object.prototype, "Implement", {
    enumerable: false,
    value: function(anInterface) {        
        if (typeof (anInterface) != 'function') {
            throw 'Can implement and interface only';
        }
        if (typeof (this) != 'function') {
            throw 'Can call method on a function/class only';
        }        
        anInterface.prototype._isImplementing = true;
        this.prototype = new anInterface();
        anInterface.prototype._isImplementing = false;
        
        this.prototype.extend(this.prototype._contract);
        this.prototype._contract = null;
    }
});

    On line 14, I used method extend which just simply copies all members from the contract to "this". So the class will have all members of the contract. You can find the implementation for that method in the file I attach at the end of this post. Finally, with all those stuff ready, i can introduce the demo below:
var IPet = interface('IPet', {
    Name: '', 
    Species: '', 
    GetDescription : function() {
        console.log('function.GetDescription: ' + this.Name + ' is a ' + this.Species);
    }
});
// define a class constructor
var Cat = function(name) {
    this.Name = name;
    this.Species = 'Cat';
}
// make that class implement an interface
Cat.Implement(IPet);

// create another class inherit from the above class
var Tiger = inheritFrom(Cat);


var cat = new Cat('Garfield');
if (cat instanceof IPet) {
    console.log('cat is an instance of IPet');           // Output: cat is an instance of Ipet
}
cat.GetDescription();                                    // Output: function.GetDescription: Garfield is a Cat

var tiger = new Tiger('Tony');
tiger.Species = 'Tiger';
if (tiger instanceof Cat){
    console.log('tiger is an instance of Cat');          // Output: tiger is an instance of Cat
}
if (tiger instanceof IPet){
    console.log('tiger is also an instance of IPet');    // Output: tiger is also an instance of IPet
}
tiger.GetDescription();                                  // Output: function.GetDescription: Tony is a Tiger
 
var tryToInitializeAnInterface = new IPet();             // ERROR: Can not initialize interface "IPet"

Well, the interface should not contain implementation. In fact, I should throw "Not implemented exception" in the contract when i create the interface, and implement that in one of the top class that implements this interface. Honestly, in my real code I would be happy to do like that, but I still keep this mistake for the demo :D Again, comments are welcome!!!
Source code

Cheers

Wednesday, August 24, 2011

Yet another way for Javacript class inheritance, sort of

    Recently, I'm so interested in Node.js and i'm planning to do something cool with that new stuff. I have .NET background so i would expect I can write Javascript in OOP style or at least I want to create classes with inheritance support. Again, as usual, I did some googling :D. That leads me to following blog:
http://ejohn.org/blog/simple-javascript-inheritance/
    That is an elegant solution and it works. However, there is a drawback: you can only inherit from object not from type which i want to do. I mean if you want to create a class Pet and another class named Cat inherit from Pet, using this approach, you have to create an object name Pet and extend that pet object to make another Cat object.
    I try to find other solution and find out some magic of using Javascript prototype which leads to my solution. First, we all know that everything in Javascript is object. Second we can create a function and it would be a constructor for any desire class.
    For example, we can create a class Pet like below:
var Pet = function(name, species){
    this.Name = name;
    this.Species = species;        
};

// Define abstract method
Pet.prototype.Shout = function() {
    throw "Not implemented exception"; // Like .NET huh?
};

// and then we can create an object of type Pet:
var pet = new Pet('Scoobydoo', 'Dog');

    Alright, what we want to do is some how creating a function Dog that can create dogs inherit from Pet, dogs should also have Name and Species property.
    I notice that in javascript, we can define methods, properties for "any object" at runtime using Object.defineProperty method. This ability is like extension methods in .NET but it's cooler since we can define the extensions at runtime.
    Okey, go back, with this idea in mine, i would implement the "inherit" method like below:
Object.defineProperty(Object.prototype, "Inherit", {
    enumerable: false,
    value: function(from) {
        if (typeof (from) != 'function') {
            throw 'Can inherit from a class function only';
        }
        if (typeof (this) != 'function') {
            throw 'Can call method on a class function only';
        }
        this.prototype = new from();        
        this.prototype._base = from.prototype;
    }
});

    So I can create the Dog class function like:
var Dog = function(name) {
    Dog.prototype = new Pet(name, 'Dog');
};

// Override the abstract method
Dog.prototype.Shout = function(){
    console.log('Woof!!!');
};

    If you notice, you can see that inside the "Inherit" method, i define a property name _base. That means the new type that inherits from provided type can access the base implementation:
Dog.prototype.Bark = function() {
    this._base.Shout();
};

    Run this code will throw the "Not implemented exception" because of the Pet base method. We can redefine the Bark method to call the override Shout method:
Dog.prototype.Bark = function() {
    this.Shout();
};

    In summary, to be able to call method "Inherit" you need to defined base type, then define a derived type with a constructor and finally call this method. I would make another utility that can be reused to completly inherit from a given type include it's default constructor:
// Call this method to completely inherit from a type include the base constructor
function inheritFrom(type) {
    if (typeof (type) != 'function') {
        throw 'type must be a function';
    }
    
    // constructor with more than 3 params is not a good practice
    // in that case, param3 should be an object that contains the rest params
    var theClass = function(param1, param2, param3){
        type.call(this, param1, param2, param3);
    };    
    
    theClass.Inherit(type);
    return theClass;
}

    There is 1 interesting point about this method is that it returns a function definition which is an object in Javascript. So the overall demo would look like:
// PET: base class with a constructor require 2 params
var Pet = function(name, species){
    this.Name = name;
    this.Species = species;    
};
// Define abstract method
Pet.prototype.Shout = function() {
    throw "Not implemented exception"; // Like .NET huh?
};

// DOG: directly inherit from Pet, include constructor
var Dog = inheritFrom(Pet);
Dog.prototype.Shout = function(){
    console.log('Woof!!!');
};
Dog.prototype.Bark = function() {
    this.Shout();
};

// FISH: define another constructor which call base constructor inside, then inherit from the base type
var Fish = function(name) {
    Pet.call(this, name, 'Fish');
};
Fish.Inherit(Pet);
Fish.prototype.Shout = function(){
    console.log('!!!!!!!!!!!!!!!!!');
};


// CAT: directly inherit from Pet, include constructor
var Cat = inheritFrom(Pet);


var pet1 = new Dog('Scoobydoo', 'Dog');     // call inherit constructor
var pet2 = new Fish('Nemo');                // call custom constructor without specify species
var pet3 = new Cat('Garfield', 'Cat');      // call inherit constructor

pet1.Shout();              // Output 'Woof!!!'
pet1.Bark();               // Output 'Woof!!!'
console.log(pet1.Name);    // Output 'Scoobydoo'
console.log(pet1.Species); // Output 'Dog'


pet2.Shout();              // Output '!!!!!!!!!!!!!!!!!'
pet2.Bark();               // ERROR: Object has no method 'Bark'
console.log(pet2.Name);    // Output 'Nemo'
console.log(pet2.Species); // Output 'Fish'


pet3.Shout();              // ERROR: Not implemented exception
console.log(pet3.Name);    // Output 'Garfield'
console.log(pet3.Species); // Output 'Cat'

Cheers, comments are welcome!!!

Saturday, August 20, 2011

MVC Donut hole caching for Razor View

    I was interested with Donut caching for a while and today I have a need for the Donut hole caching or partial view caching. As many of us, I tried to find whether some smart guys had solved this problem. And it seems like Mr.Haacked had mentioned this in his post. However, that solution is for ASP.NET view engine. I have no choice and have to solve it myself. Hopefully MVC team will support this feature in next versions of MVC framework. Hmmm indeed, it could be supported in version 4: See road map.


    My idea is if a view engine renders the view's content to a stream or text writer, we can intercept that process, cache the output string to somewhere together with the view's id or what ever that can identify the view. Then next time the view is rendered, we can determine whether or not to get the content from cache or let the view engine continue it's job. So i dig into the MVC source code and find this:
public class RazorView : BuildManagerCompiledView
{
    protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
    {
        //.............
    }
}
    That's exactly what i need, isn't it? We probably can create a derived class from RazorView and override above method, get the content that the text writer receive and write it directly to the writer if the view is cached. So, i need a custom text writer that could return to me what it receive :D
public class TrackableTextWriter : TextWriter
{
    private readonly TextWriter _writer;
    private StringBuilder _mem;
    public TrackableTextWriter(TextWriter writer)
    {
        _writer = writer;
        _mem = new StringBuilder();
    }
    public override Encoding Encoding
    {
        get { return _writer.Encoding; }
    }
    public override void Write(string value)
    {
        _writer.Write(value);
        _mem.Append(value);
    }
    public string GetWrittenString()
    {
        return _mem.ToString();
    }
    protected override void Dispose(bool disposing)
    {
        if (!disposing)
        {
            _writer.Dispose();           
        }
        base.Dispose(disposing);
        _mem = null;
    }
}
    Alright, now it's time to create a derived of RazorView:
public class CachableRazorView : RazorView
{
	private const string ViewCachePrefix = "ViewCache__";
    protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
	{
		var appCache = viewContext.HttpContext.Cache;
		var cacheKey = ViewCachePrefix + ViewPath;
		// Check if there was a Cache config that had been fully added to the cache
		var cacheConfiguration = appCache[cacheKey] as CacheConfiguration;
		if (cacheConfiguration != null && cacheConfiguration.Data != null)
		{
			writer.Write(cacheConfiguration.Data);
			return;
		}
		var trackableTextWriter = new TrackableTextWriter(writer);
		base.RenderView(viewContext, trackableTextWriter, instance);
		
		// Cache config has just been added when the view is rendered the first time thanks to the HtmlHelper
		cacheConfiguration = appCache[cacheKey] as CacheConfiguration;
		if (cacheConfiguration != null)
		{
			var writtenString = trackableTextWriter.GetWrittenString();
			cacheConfiguration.Data = writtenString;
			appCache.Remove(cacheConfiguration.Id);
			appCache.Add(cacheConfiguration.Id,
						cacheConfiguration,
						null,
						Cache.NoAbsoluteExpiration,
						TimeSpan.FromSeconds(cacheConfiguration.Duration),
						CacheItemPriority.Default,
						null);
		}
	}
}
    Then there's of course a place we can return this custom view instead of RazorView. That's definitely the RazorViewEngine. So next step is creating our custom razor view engine:
public class CachableRazorViewEngine : RazorViewEngine
{
	protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
	{
		return new CachableRazorView(controllerContext, partialPath, null, false, FileExtensions, ViewPageActivator);
	}

	protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
	{
		return new CachableRazorView(controllerContext, viewPath, masterPath, true, FileExtensions, ViewPageActivator);
	}
}
    As usual, we need to make the web application use this view engine by modifying the globals.asax
protected void Application_Start()
{
	AreaRegistration.RegisterAllAreas();

	RegisterGlobalFilters(GlobalFilters.Filters);

	ViewEngines.Engines.Clear();
	ViewEngines.Engines.Add(new CachableRazorViewEngine());

	RegisterRoutes(RouteTable.Routes);
}
    Using this approach, the traditional OutputCache will not have value. So the idea pops in my head is making a HtmlHelper extension and calling it in the view, something like this:
@{
	Html.OutputCache(new CacheConfiguration { Duration = 10 });
}
    So here is the implementation:
public static class CacheHtmlHelperExtensions
{
	public const string ViewCachePrefix = "ViewCache__";
	public static void OutputCache(this HtmlHelper helper, CacheConfiguration cacheConfiguration)
	{
		var view = helper.ViewContext.View as BuildManagerCompiledView;
		if (view != null)
		{
			cacheConfiguration.Id = ViewCachePrefix + view.ViewPath;
			helper.ViewContext.HttpContext.Cache.Add(cacheConfiguration.Id,
													cacheConfiguration,
                                                    null,
                                                    Cache.NoAbsoluteExpiration,
                                                    TimeSpan.FromSeconds(cacheConfiguration.Duration),
                                                    CacheItemPriority.Default,
                                                    null);
		}
	}
}
    Okey, it's time to test this implementation. I put the Html.OutputCache method in the Index page of default MVC application like above. So the view will be cached in 10 seconds. If the view is not accessed in next 10 seconds, the cache engine will remove the content from the cache. However, within 10 seconds, if the view is accessed again, the cache will remain for next 10 seconds and so on. Pretty cool, huh? I need to run the performance test on this page to see the actual result. There is a very cool tool in Linux named "curl-loader" but I didn't know any for Windows. After a while searching, I found apache benchmark very similar and usefull :D. I use "ab" tool to test the web app when enable and disable the cache. The result is very interesting. Eventhough the Index page is quite simple with only text, no database access but when I enable the cache, the web server can serve 1107 requests per second when I run 10000 requests to server at concurency level at 100 compare to only 531 requests per second when I disable the cache, 2 times faster:

benchmark result

    Summary, there is a outstanding issue using this approach, the builtin output cache of ASP.NET can not be utilised. It also ignores the value of ViewModel, that's mean this implementation has not supported caching by different view model. But I think we can do it if we really need that feature. Just find a way to distinguish the different between view model values, it could be a hash code or something :D.

Source code: Razor.DonutHoleCaching.zip

Cheers

Thursday, July 28, 2011

Unit Test DisplayFormat attribute HtmlHelper

    Previously, I wrote about how to mock a ViewEngine for one of my unit tests. Today, I came across a question on StackOverflow asking about how to unit test the out put of DataAnnotation attribute DisplayFormat. I'm very interested in Unit Test so I decide to find the answer, another reason is to increase my reputation on StackOverflow :D. Honestly, I don't exactly know the reason why to test the output because for me, we don't need to test code that was not written by us. Anyways, this is a interesting question for me.     Let's say we have a ViewModel using DisplayFormat like below:
public class UserViewModel
{        
    [DisplayFormat(DataFormatString = "{0:dd/MM/yy}")]
    public DateTime Birthday { get; set; }
}
    And here is the basic unit test to verify the output
[Test]
public void Test_output_display_format_for_Birthday_property()
{
    // Arrange
    var _model = new UserViewModel {Birthday = DateTime.Parse("28/07/11") };
    var helper = MvcTestControllerBuilder.GetHtmlHelper<UserViewModel>();
    helper.ViewData.Model = _model;
    
    // Action
    var result = helper.DisplayFor(x => x.Birthday);
    
    // Assert
    Assert.That(result.ToHtmlString(), Is.EqualTo("28/07/11"));
}
    Apparently, this test failed like the description in the StackOverflow question. I decided to debug the MVC source code and found 2 reasons: + First, the MVC framework will find the ActionCacheItem using method GetActionCache:
internal static Dictionary<string, ActionCacheItem> GetActionCache(HtmlHelper html)
{
    HttpContextBase httpContext = html.ViewContext.HttpContext;
    if (!httpContext.Items.Contains(cacheItemId))
    {
        Dictionary<string, ActionCacheItem> dictionary = new Dictionary<string, ActionCacheItem>();
        httpContext.Items[cacheItemId] = dictionary;
        return dictionary;
    }
    return (Dictionary<string, ActionCacheItem>) httpContext.Items[cacheItemId];
}
It'll try to find the cache item in httpContext.Items but the Items is null. So the first thing we need to mock the value for httpContext.Items:
var helper = MvcTestControllerBuilder.GetHtmlHelper<UserViewModel>();
helper.ViewContext.HttpContext.Setup(x => x.Items).Returns(new Dictionary<string, object>());
+ Secondly, the MVC framework will try to find the display template for "DateTime" and "String". Obviously we don't have those stuff in the Unit test environment. The code is located in TemplateHelpers.cs:
internal static string ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, string templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
{
    Dictionary<string, ActionCacheItem> actionCache = GetActionCache(html);
    Dictionary<string, Func<HtmlHelper, string>> dictionary2 = getDefaultActions(mode);
    string str = modeViewPaths[mode];
    foreach (string str2 in getViewNames(viewData.ModelMetadata, new string[] { templateName, viewData.ModelMetadata.TemplateHint, viewData.ModelMetadata.DataTypeName }))
    {
        ActionCacheItem item;
        Func<HtmlHelper, string> func;
        string key = str + "/" + str2;
        if (actionCache.TryGetValue(key, out item))
        {
            if (item != null)
            {
                return item.Execute(html, viewData);
            }
            continue;
        }
        ViewEngineResult result = ViewEngines.Engines.FindPartialView(html.ViewContext, key);
        if (result.View != null)
        {
            ActionCacheViewItem item2 = new ActionCacheViewItem {
                ViewName = key
            };
            actionCache[key] = item2;
            using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                result.View.Render(new ViewContext(html.ViewContext, result.View, viewData, html.ViewContext.TempData, writer), writer);
                return writer.ToString();
            }
        }
        if (dictionary2.TryGetValue(str2, out func))
        {
            ActionCacheCodeItem item3 = new ActionCacheCodeItem {
                Action = func
            };
            actionCache[key] = item3;
            return func(MakeHtmlHelper(html, viewData));
        }
        actionCache[key] = null;
    }
    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.TemplateHelpers_NoTemplate, new object[] { viewData.ModelMetadata.RealModelType.FullName }));
}
    So the main thing we need to do is somehow mock the result at line "19" to make the view engine return a ViewEngineResult that has View property equals to null. I make a helper method similar to this post to do this:
public static ViewEngineResult SetupNullViewFor(string viewName)
{
    Mock<IViewEngine> mockedViewEngine = GetCurrentMockViewEngine() ?? new Mock<IViewEngine>();
    var viewEngineResult = new ViewEngineResult(new List<string>());

    mockedViewEngine.Setup(x => x.FindPartialView(It.IsAny<ControllerContext>(), viewName, It.IsAny<bool>()))
                    .Returns(viewEngineResult);
    mockedViewEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), viewName, It.IsAny<string>(), It.IsAny<bool>()))
                    .Returns(viewEngineResult);

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(mockedViewEngine.Object);
    return viewEngineResult;
}

private static Mock<IViewEngine> GetCurrentMockViewEngine()
{
    foreach(var v in ViewEngines.Engines)
    {
        try
        {
            return Mock.Get<IViewEngine>(v);
        }
        catch (Exception)
        {
        }
    }
    return null;
}
    With all of these stuff, the final unit test would look like:
[Test]
public void Test_output_display_format_for_Birthday_property()
{
    // Arrange    
    MvcTestFixtureHelper.SetupNullViewFor("DisplayTemplates/DateTime");
    MvcTestFixtureHelper.SetupNullViewFor("DisplayTemplates/String");
    
    var _model = new UserViewModel {Birthday = DateTime.Parse("28/07/11")};
    var helper = MvcTestControllerBuilder.GetHtmlHelper<UserViewModel>();
    helper.ViewContext.HttpContext.Setup(x => x.Items).Returns(new Dictionary<string, object>());
    helper.ViewData.Model = _model;
    
    // Action
    var result = helper.DisplayFor(x => x.Birthday);
    
    // Assert
    Assert.That(result.ToHtmlString(), Is.EqualTo("28/07/11"));
}
You can refer to my similar posts about MvcTestControllerBuilder (this class has some methods to mock HtmlHelper) and How to mock the ViewEngine. In those posts, I used NSubstitue but now I changed to use Moq, they're quite similar :D. Cheers.

Tuesday, July 19, 2011

Save some lines for your Razor view

We all know that we should not put logic in the MVC view. If we have to do something base on a complex condition, we should better make a HtmlHelper method for that logic. In an attempt to remove some simple if/else statement on the view by using some extention method. Let's say we have something like below in the view:
@if (!Request.IsAuthenticated)
{
    Html.RenderPartial("LoginBox");
}
I attempt to make it look like:
 
@{ Html.Do(x => x.RenderPartial("LoginBox"))
       .When(!Request.IsAuthenticated); }
 
So we can save 2 lines of code or event 3 lines if we write the code in 1 line:
 
@{ Html.Do(x => x.RenderPartial("LoginBox")).When(!Request.IsAuthenticated); }
 
I know it is very hard to remove all condition logic from the view unless you create HtmlHelper methods for all the condition logic on your view. But it could lead to so many methods. I think we should not create helper method if we don't use the method 2 places. So the method I suggest could help :D. I think it improves the readablity for your view code especially both the designers and developers are working on the same file. So here is the code:
public interface IAction<T>
{
    T Object { get;}
    Action<T> Action { get; }
    void When(Expression<Func<T, bool>> when);
    void When(bool when);
}
I will make 2 implementation of the above interface. The first one is OneConditionAction and the other is PostConditionAction. So I can combine multi conditions like:
@{ Html.When(!Request.IsAuthenticated)
       .And(....)
       .And(....)
       .Do(x => x.RenderPartial("LoginBox")); }
Below is the implementation:
public class OneConditionAction<T> : IAction<T>
{
    public T Object { get; private set; }

    public Action<T> Action { get; private set; }

    public virtual void When(Expression<Func<T, bool>> when)
    {
        if (when.Compile()(Object))
        {
            Action(Object);
        }
    }

    public void When(bool when)
    {
        if (when)
        {
            Action(Object);
        }
    }

    public OneConditionAction(Action<T> action, T helper)
    {
        Action = action;
        Object = helper;
    }
}       

public class PostConditionsAction<T> : OneConditionAction<T>
{
    public PostConditionsAction(IAction<T> conditionalAction)
        : base(conditionalAction.Action, conditionalAction.Object)
    {
        Condition = x => true;
    }

    public Expression<Func<T, bool>> Condition { get; set; }

    public override void When(Expression<Func<T, bool>> when)
    {
        var newCondition = this.And(when);
        Condition = newCondition.Condition;
    }
}
We'll need the extension methods:
namespace System.Web.Mvc
{
    public static class ActionExtensions
    {
        public static IAction<T> Do<T>(this T obj, Action<T> action) where T : class
        {
            return new OneConditionAction<T>(action, obj);
        }

        public static IAction<T> Do<T>(this T obj, Action action) where T : class
        {
            return new OneConditionAction<T>(a => action(), obj);
        }
    }
    
    public static class PostConditionsActionExtensions
    {
        public static PostConditionsAction<T> When<T>(this T obj, bool condition)
        {
            var newAction = new PostConditionsAction<T>(new OneConditionAction<T>(x => { }, obj));
            newAction = newAction.And(condition);
            return newAction;
        }

        public static PostConditionsAction<T> When<T>(this T obj, Expression<Func<T, bool>> condition)
        {
            var newAction = new PostConditionsAction<T>(new OneConditionAction<T>(x => { }, obj));
            newAction = newAction.And(condition);
            return newAction;
        }

        public static PostConditionsAction<T> And<T>(this PostConditionsAction<T> postConditions, Expression<Func<T, bool>> andCondition)
        {
            var x = Expression.Parameter(typeof(T));
            postConditions.Condition = Expression.Lambda<Func<T, bool>>(Expression.And(Expression.Invoke(postConditions.Condition, x), Expression.Invoke(andCondition, x)), x);
            return postConditions;
        }

        public static PostConditionsAction<T> And<T>(this PostConditionsAction<T> postConditions, bool andCondition)
        {
            return postConditions.And(x => andCondition);
        }

        public static PostConditionsAction<T> Or<T>(this PostConditionsAction<T> postConditions, Expression<Func<T, bool>> orCondition)
        {
            var x = Expression.Parameter(typeof(T));
            postConditions.Condition = Expression.Lambda<Func<T, bool>>(Expression.Or(Expression.Invoke(postConditions.Condition, x), Expression.Invoke(orCondition, x)), x);
            return postConditions;
        }

        public static PostConditionsAction<T> Or<T>(this PostConditionsAction<T> postConditions, bool orCondition)
        {
            return postConditions.Or(x => orCondition);
        }

        public static void Do<T>(this PostConditionsAction<T> postConditions, Action<T> action) where T : class
        {
            postConditions.Object.Do(action).When(postConditions.Condition);
        }

        public static void Do<T>(this PostConditionsAction<T> postConditions, Action action) where T : class
        {
            postConditions.Object.Do(action).When(postConditions.Condition);
        }
    }
}
Because they are generic classes, so we can use this syntax for any object, not only HtmlHelper :D Cheers

How to mock UrlHelper?

Sometime, you need to write unit test for an action method that needs a UrlHelper. But I'm sure you will never can mock that stuff since it's a concrete class and it's methods are not virtual. Let's say we have following action method and want to test it:
[Authorize]
public ActionResult LogOnAs(Guid userId, string returnUrl)
{
    // Logic to login user by Id ...
    
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }

    return RedirectToAction("Index", "User");
}
I did some research, experimented with some results and find that extracting the UrlHelper methods to an interface then make a wrapper class to implement that interface is the best way. We need to define a new property to the BaseController like below.
 
public new IUrlHelper Url {get; set;}
 
Then in the unit test project, after initializing the controller, you can mock the interface easily. You probably need only some methods from the UrlHelper so I would recommend you extract only those required methods to IUrlHelper. For example:
public interface IUrlHelper
{
    string Action(string actionName, string controllerName);

    string Action(string actionName, string controllerName, object routeValues);

    string Action(string actionName, string controllerName, RouteValueDictionary routeValues);
    
    bool IsLocalUrl(string url);
}
Finally, we can create an adaptor class like below to delegate all method calls to the real UrlHelper object:
public class UrlHelperAdaptor : UrlHelper, IUrlHelper
{
    internal UrlHelperAdaptor(RequestContext requestContext)
        : base(requestContext)
    {
    }

    internal UrlHelperAdaptor(RequestContext requestContext, RouteCollection routeCollection)
        : base(requestContext, routeCollection)
    {
    }

    public UrlHelperAdaptor(UrlHelper helper) 
        : base(helper.RequestContext, helper.RouteCollection)
    {
    }
}
Apparently, we need to initialize the new Url property in the BaseController to make the real code work normally:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    Url = new UrlHelperAdaptor(base.Url);
}
Now, the controller method is fully testable.
[Test]
public void LogonAs_should_return_RedirectToRouteResult()
{
    // Arrange
    var controller = new AccountController();
    /* Create a mock of IUrlHelper */
    controller.Url = Moq.Mock.Of<IUrlHelper>(x => x.IsLocalUrl(It.IsAny<string>()) == false);     

    // Action
    var result = controller.LogOnAs(Guid.NewGuid(), "any-return-url") as RedirectToRouteResult;

    // Assert
    result.Should().Not.Be.Null();
}
Using this approach can help you test any class that depends on IUrlHelper such as custom UrlHelper classes. Cheers

Wednesday, July 6, 2011

Never give a chance to make something wrong

Recectly, I've penetrated the truth that if you don't want others do something (bad), never ever give them a chance to do that. It's true in both my real life and in programming. Since my blog is just about software programming, I would like to write about some funny mistakes that people often make. Well, I did make some of them in the past :D

1/ You may forget about the ability of the constructor.

Let's look at following code:
public class UserViewModel
{    
    // These properties must not be null
    public string FirstName { get; set;}
    public string LastName { get; set;}
    public string Email { get; set;}    
}
We can see from the comment in the class above, the author want 3 propreties must have value. It oould be his intention but someone else may need a instance of this class, so he instantiates a UserViewModel object without setting the values for any of those 3 properties. And that could cause problems at runtime since it's not what the author of UserViewModel want. So instead of doing this, we can make the language ensuring that requirement for us:
using System.Diagnostics.Contracts;
public class UserViewModel
{    
    public UserViewModel(string firstName, string lastName, string email)
    {
        Contract.Assert(!string.IsNullOrEmpty(firstName), "firstName must have value");
        Contract.Assert(!string.IsNullOrEmpty(lastName) , "lastName must have value");
        Contract.Assert(!string.IsNullOrEmpty(email)    , "email must have value");
    
        FirstName = firstName;
        LastName = lastName;
        Email = email;
    }
    
    // These properties must not be null
    public string FirstName { get; private set;}
    public string LastName { get; private set;}
    public string Email { get; private set;}    
}

2/ You won't need a private field for a public property.

My team is applying BDD and using PageObject pattern. A guy in a team created a class that have a something like:
private RegistrationStatus _registrationStatus;

public RegistrationStatus Status
{
    get { return _registrationStatus; }
}
What an unnecessary 4 lines of code. You know what, just a few weeks later, the solution is full of these mistakes because alot of people are writing BDD tests and they like copy & paste. So, just change it to 1 line of code version:
 
public RegistrationStatus Status {get; private set;}
 
Well, if you don't want to see these stuff in the code, don't make any thing like this because we're all doing the "COPY & PASTE" job.

3/ If you have not finished something, use NotImplementedException

Apparently working in a team, someone could create a service interface with some methods then the other could use the service for presentation layer. Here is an example:
public interface IUserService
{
    User Get(Guid id);
    int Count();
}

public UserService : IUserService
{
    public User Get(Guid id)
    {
        return null;
    }
    
    public int Count()
    {
        return -1;
    }
}
Everything is fine when compile but why return something like those when they're useless value. Well, if we need an implementation of IUserService, why not create a Mock or a fake object then everyone knows it's a fake and it will never been used in the production. However, it could be acceptable if we are creating a NullObject, but I mostly throw NotImplementedException instead of doing nothing for a void method or return meaningless value for functions.

4/ it's not really a singleton

Everyone knows about singleton. It supposes to be the easiest most simple design pattern. Let's see this code:
public class Helper
{
    private static Helper _instance;
    public static Helper Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Helper();
            }
            return _instance;            
        }
    }
}
Okey, it's just a very simple implementation. But the idea is that it allows only 1 instance of Helper in the system and whenever you want that instance, you must access it from Helper.Instance. Cool but it's not really singleton. Anyone can create an object of Helper easily because by default, the constructor is public so make it private if you don't want people use it.

5/ We should follow Dependency Inversion Principle

This principle is the one that affects my way of thinking. If you read about the Onion architecture, you'll see that architecture is not complicated. It just applies the DIP and make a flashy name. If we apply DIP strickly, we'll never put implementation and interface together in a class library. Instead, put them in separated project. Classes depend on the interface will never know about the concreate implementation of that interface and they will be connected by some sort of IOC library. However, doing that way can make you create alot of projects in one solution. Honestly I have never done anything like this but, but I think Onion architecture is something to consider when you design new application. I prefer isolating the business implementation from the technology we use so any changes of technology will make a little affect to our code.

Wednesday, June 1, 2011

Basic Unit Of Work Implementation

    I bet you must have heard about Unit Of Work. If you have not, you can find the discription here : http://martinfowler.com/eaaCatalog/unitOfWork.html.    You can change the database with each change to your object model, but this can lead to lots of very small database calls, which ends up being very slow. Furthermore it requires you to have a transaction open for the whole interaction, which is impractical if you have a business transaction that spans multiple requests. The situation is even worse if you need to keep track of the objects you've read so you can avoid inconsistent reads.     Nowadays, DDD is one of the most famous design methodologies in the developer community. Even though 95% of all software applications fall into the “not so good for using DDD” categories, some practices and ideas of DDD can be used in many .NET projects, especially Repository and Domain Service. I have seen many implementations of Repository pattern and UnitOfWork, but i am not so happy with any because of the following reason. We tend to use some kinds of ORM like NHibernate or EF as the technology behind Repository implementation. These frameworks mostly have the Unit Of Work built in, so I think the Domain Service layer should not call IUnitOfWork.CommitChange() when it needs to persist something to the repository. In some complex services, we call a service method which may execute different service methods; and because a service method (that requires changing the database) should call IUnitOfWork.CommitChanges. Therefore, as a result, IUnitOfWork.CommitChanges will be called many times in 1 business transaction.     Another example could be the controller which can use several domain services to execute an action method. Let's say a website user wants to register at your website. When he submits the registration form, the RegisterController will call the UserService to create a user to the repository and another service create a WelcomeEmail in the Email repository. That means there are 2 records are got involed in this transaction (assume that you use SQL as the data source). And a proper UnitOfWork implementation should be called only 1 time to finish the saving task. However, it's easy to see the implementation like this:
[HttpPost]
public ActionResult Register(RegisterUserViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        return View(viewModel)
    }
    
    UserService.Create(user);
    EmailService.Create(welcomeEmail);
    
    return RedirectToAction("Index", "Home");
}
In the user service implementation :
public void Create(User contact)
{    
    _userRepository.Add(contact);
    _unitOfWork.CommitChange();
}
And in the email service implementation :
public void Create(Email email)
{    
    _emailRepository.Add(email);
    _unitOfWork.CommitChange();
}
    It's not difficult to see that the UnitOfWork will call method CommitChange() two times. If you use EF behind the Repository implementation, I'm sure the code above will make your breakpoint stop at method ObjectContext.SaveChanges() 2 times. For me it’s not “Unit Of Work” if we do like that. It would be nice if we could have a way to call the CommitChange() only when we actually need, it could be the end of every http request. Therefore, the domain services should never call CommitChange() on your UnitOfWork.     Base on this idea which is a bit similar to "Open Session In View" pattern, I created a basic implementation of UnitOfWork below:
public interface IUnitOfWork
{
    void CommitChange();
    bool IsDirty { get; set; }
}
The domain service will use a UnitOfWorkScope to ensure the change will be committed in the end or immediately based on the UnitOfWorkScopeOption:
public enum UnitOfWorkScopeOption
{
    Shared,
    CommitImmediately
}

public class UnitOfWorkScope : Disposable
{
    private readonly UnitOfWorkScopeOption _option;
    private readonly IUnitOfWork _unitOfWork;

    public UnitOfWorkScope() : this(UnitOfWorkScopeOption.Shared)
    {
    }

    public UnitOfWorkScope(UnitOfWorkScopeOption option)
    {
        _option = option;
        _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>(); // I'm using StructureMap
    }

    protected override void DisposeCore()
    {
        switch(_option)
        {
            case UnitOfWorkScopeOption.CommitImmediately:
                {
                    _unitOfWork.CommitChange();
                    _unitOfWork.IsDirty = false;
                    break;
                }
            default:
                _unitOfWork.IsDirty = true;
                break;
        }
    }
}
I will change the service methods like below:
public void Create(User contact)
{    
    using (new UnitOfWorkScope())
    {
        _userRepository.Add(contact);    
    }
}

public void Create(Email email)
{    
    using (new UnitOfWorkScope())
    {
        _emailRepository.Add(email);
    }
}
The default constructor of UnitOfWorkScope would set the scope option to Shared. That means it would do nothing but mark the UnitOfWork.IsDirty to true. In the end, we can base on that value to determine whether we should call commit change. I put this logic in the base controller, so after the ActionExecuted, the UnitOfWork will be invoked to do the saving job.
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    base.OnActionExecuted(filterContext);

    if (filterContext.Exception == null || filterContext.ExceptionHandled)
    {
        var unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
        if (unitOfWork != null && unitOfWork.IsDirty)
        {
            unitOfWork.CommitChange();
        }
    }    
}
I made a test project and looks like it run at least 2 times faster than the the original way when I use UnitOfWorkScope. There is a very cool implementation of UnitOfWork at NCommon. You should check it out. Finally, here is the test project. Cheers

Monday, May 30, 2011

Some issues with BDD Specflow & WatIn

    Last few weeks, our team is doing alot of experiments on BDD. We read several books trying to find out the proper way to apply BDD in .NET project using Specflow and WatIn. There are some good articles out there indeed but for me, they're quite basic. These articles do the good works to help me start the first steps but after walking several steps, some issues comes up: - What is the good way to initialize some test data before a scenario? - What is the good way to write feature files? - When would we run these UI tests? - What is the good way to implement step definitions that faciliate reusable and easy maintenance? - What to do if the feature requires some interaction to external sources?     Honestly, we've not had the best answers in 3 weeks. Some other issues would come up in the future and we might have better answers for what we want. However, I'm quite happy with following solutions:

1/ What is the good way to initialize some test data before a scenario?

This question came up when we met some feature that requite existing data. Let's look at following scenario:
Scenario: Log-In
Given I enter a previously registered email address / password combination
When I click log-in
Then the site will authenticate me and remember me for the remainder of my session
    In order to execute this test, it assumes that we have a registered user in the system. I tried to google a lot to find the answer for this question. Fortunately, I found it while reading the cuke4ninja document. Basicaly, there are 3 methods to do this: - Direct access to the database - Call to the business layer API - Creating records using the UI     Our team had a big argument about it for 2 weeks. The reason is we devided developers into Presentation and Service teams. These 2 teams will work parallel together, one to build the services include backend code like repositories, services, etc; one to implement the web and UI test that utilises the service made by the other team. The thing is presentation team will not know and should not know anything about what the other team would do. For example, the presentation team should not know anything about database schema. We should have agrement on the a service the presentation team needs. So the presentation team will mock the service to be able to run the web while the required service is being implemented by the other team. And thus, the presentation team could call this service to make some initialisation for the test. Personally, I don't like this idea because of following reasons: - Ultimately, the UI tests will talk to the database, not the mocked service because we want the end to end task, so writing some codes that access to the database to clean up/ initalise is not a big deal. - For me, The UI test code should only be methods that are called to interact with user interface rather than depending on some Service API - The UI test would be run multiple times against the real database, we definitely need some way to clean up the records made be previous test run. It could be difficult to delete a record since it could require cascade delete. If I still keen to use this way, I have to modify the service and add method Delete for some related services. And I think it is not a best practice because we should not change the code design just for the test run.     Therefore, a small script to empty tables, re-insert data to tables is quite good for me. This script will be made by the service team and they need to ensure that the script is up to date to make the whole test pass. The presentation team should use UI to create records they need for the test. Everything will be much simpler if the developer can get involve in implementing everything from service code to presentation code.

2/ What is the good way to write feature files?

    The solution above leads to this question. For some complex scenarios that require complex exisiting data, writing UI code for it is not a proper way. Even though the script to initialise data could do this perfectly, but let me say again, the presentation team will not know what the service team would do include that scripts. So if i don't want to call the service api to do the job, I must spend effort to write the UI code for initialisation stuff.     So, why don't we re-write the scenario in a way that reduces the need for data initialisation. Let's say we have this scenario:
Given I am a new user
And I am on the Registration page
When I enter the following basic details
 | Forename | Surname | Email                | Password |
 | Van      | Nguyen  | van.nguyen@email.com | 123456   |
And I submit the form
Then the site will save my details
And show a message saying 'Registration successful'
Should we write:
Given I am a new user
And I am on the Registration page
When I enter the my basic details with an email that's never been used
And I submit the form
Then the site will save my details
And show a message saying 'Registration successful'
    Then we could implement the "Never been used" by using an email that contains a GUID or DateTime.Now.Ticks. So that test won't need any database clean up or initialisation. It's easier to create something that is unique rather than making sure something that does not exist in the system. Positive way is always better than negative way, isn't it :D. In the sample solution, I utilised the @tag feature of Specflow, so any scenarios with the tag @requireCleanDb would have the step hook to clean and initialise the database to the original state.

3/ When would we run these UI tests?

    I must say that I'm a bad developer. I hate to fix bugs, expecially bugs that caused by others. In our project, I implemented a feature and later on, any bugs on that feature were assigned to me. That was fine but the thing is when fixing these bugs I found that the root reason was some changes in javascript or some guys just removed the property of a View Model without fixing the View. Unit test does the very good work that can make sure any checkin will not break the existing code, but it cannot prevents bug caused by a small change in javascript. UI automation test could do this, but it has a problem with running time so It could not be executed for every checkin. Then my pain will still be there as my team agrees to run the tests every day at night. And in the morning, developers will receive the test run report. However, every developer could run the tests before checkin to make sure he would not ruin something.

4/ What is the good way to implement step definitions that facilitate reusable and easy maintenance?

    This is my most interesting question. I couldn't resist the temptation to use WatIn classes to implement the test when I read the how to document. But when I read the other book cuke4ninja and I watched this mvcConf: BDD in ASP.NET MVC using SpecFlow, WatiN and WatiN Test Helpers, I knew I need some kind of abstraction. I need wrappers for WatIn classes. I implemented some "technical" classes like Page, Form and "Workflow" classes for the flow of the UI tests. I decided to split these things into separated class libraries, most of the classes were internal except the "workflow" classes which will be used in the step definition implementation. So in the end, my integration test project just know about workflow classes without knowing anything about Page, Form or WatIn. Actually, at first, I let the UI test methods utilise Page, Form objects along with WorkFlow classes but then I thought it would be better to make Page and Form internal. So instead of doing like this:
[Then(@"the site will authenticate me and remember me for the remainder of my session")]
public void ThenTheSiteWillAuthenticateMeAndRememberMeForTheRemainderOfMySession()
{
      Assert.IsTrue(Page.Current.ContainsText("Log Off"));
      Assert.IsTrue(Page.Current.Url == "/Home/Index" || Page.Current.Url == "/");
}
I prefer doing like this:
[Then(@"the site will authenticate me and remember me for the remainder of my session")]
public void ThenTheSiteWillAuthenticateMeAndRememberMeForTheRemainderOfMySession()
{
      Assert.IsTrue(LoggedInUser.Current.IsLoggin());
      Assert.IsTrue(LoggedInUser.Current.IsAtHomePage());
}
    It makes the test method easy to read and certainly, the method is reusable. Please checkout the example project in the end of this post for more detail. Please note that I implement it based on my perspective and I don't say It the best way. So please correct me.

5/ What to do if the feature requires some interaction to external sources?

Or should we test if the feature requires interation to external resources? Let's say we have following scenarios:
Scenario: Forgot Password - Password Retrieval
Given I enter an email address that was previously registered with the site
When I click 'send email'
Then I receive a message telling 'An email was sent to you to reset your password!'
And I receive an email containing a link to reset my password
    If we want to write the test for this, we might need a mail box such as Gmail. We'll simulate user interaction on the mail box to click on the forget password link, etc. So what happens if the Gmail change their UI which could make our test fail. Personally, I still like to test this scenario as a normal user instead of creating some kind of mocks to have the reset link. Because I think the "end to end test" is testing the appliation just like the normal user use it. And these kind of scenarios would just be a few so It will not be a big deal. Okey, what's if they are alot? Well, I think abstraction of the external interaction activities could be an option. Talking to external resources is the common thing of a software. We use ORM to talk to database, they could change the library anytime, so what we do? We update the library. We call to 3rd API like Facebook API, Twitter API, Credit card processing service, etc. They could change their API anytime, so what we do? We make the wrapper to these API then we could switch to better service anytime. That's it. So I believe abstraction would be a good solution for these kinds of thing. However, it's still a debate in my team whether it could add much value to write UI test for these kinds of scenario. Perhaps the time will find the answer for this question.     Hmmm, so far so good. Please checkout the demo project. It contains my basic idea above. Again, I don't say It's the best way of using WatIn or implementation of BDD. There must be other issues because these features are very basic. Everything here is based on my personal perspective and I'm very gratefull if any readers of this post would give me your idea to make it better, or just tell me I was wrong at some points. Thanks for reading. Code: Download

Thursday, May 12, 2011

Defining Custom Functions in Entity Framework

Well, I's been 1 month since my last post. Today, I'm gonna write something about the well-known not supported issue of Entity Framework. I intended to write a LinQ to Entity query that would require a comparison and conversion between uniqueidentifier and string value in SQL database. The query looks like this:
var items = from x in XRepository.GetAll()
            join y in YRepository.GetAll() on x.UniqueId.ToString() equals y.StringValue into xy
            from z in xy.DefaultIfEmpty()
            select x;
I wrote the unit test and the test run through the code without any problem. But when I run the application against the real SQL database, there would be a following exception: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. After a while googling, I realized that there are not any built-in support to achive what I want. However, in EF4, there is a class named: SqlFunctions.StringConvert that can convert from double to string. Moreover, it can be used in LinQ to Entity queries. That class led me to the EdmFunctionAttribute and then I decided to create something similar to convert from uniqueidentifier to nvarchar for my need. I definitely need a custom SQL Function for conversion:
CREATE FUNCTION [dbo].[GuidToString]
(
    @guid as uniqueidentifier
)
RETURNS varchar(50)
AS
BEGIN
    return CONVERT(varchar(50), @guid)
END");
What I do next is to modify the EDMX file and add a small xml code:
<Function Name="GuidToString" 
          Aggregate="false" 
          BuiltIn="false" 
          NiladicFunction="false" 
          IsComposable="true" 
          ParameterTypeSemantics="AllowImplicitConversion" 
          Schema="dbo" ReturnType="varchar">
    <Parameter Name="guid" Type="uniqueidentifier" Mode="In" />
</Function>
I just find </Schema></edmx:StorageModels> and add the xml code before it. Because the function would return something, so the IsComposable must be set to true and we must provide the ReturnType value for the function. After that, I create a static class like below. It can be put anywhere in your solution.
[EdmFunction("My.Namespace.Store", "GuidToString")]
public static string GuidToString(Guid guid)
{
    // This code will never been run against real SQL database
    // This will help any test requires this method pass
    return guid.ToString().ToUpper();
}
The namespace I provide to EdmFunctionAttribute is really important. It must match the namespace in the EDMX file. You can easily find the namespace as an attribute of the xml node "Schema" when viewing the EDMX file using any XML editor. Finally, I can change the code to use the method like below:
var items = from x in XRepository.GetAll()
            join y in YRepository.GetAll() on GuidToString(x.UniqueId) equals y.StringValue into xy
            from z in xy.DefaultIfEmpty()
            select x;
Cheers

Friday, April 22, 2011

MVC RouteConstraint is the culprite

Today, several of our live product sites got a serious issue. The page that renders a list of businesses took over 1 minutes to render. We couldn't find out the problem in serveral hours. After a while keep investigating, we found that the call of below method took about 6 seconds to run:
 
UrlHelper.Action("action", "controller", RouteValueCollection)  
That means on a page that contains 10 links to the business detail would take about 1 minutes to render. It was really strange because this method is the built-in method of ASP.NET MVC so we didn't think it's our database issus. Another reason helped us beleave in our assumtion was that we tried to debug the service to get the list of businesses from database and saw that it was fast although we have over 1 mil of records in database, just the rendering step was so slow. I kept trying and stepped into MVC source code. It led me to this method which took 6 seconds to run:
 
RouteCollection.GetVirtualPath(RequestContext, RouteValueDictionary)  
RouteCollection is a class in namespace System.Web.Routing and it was very strange because I couldn't continue to step into this method to debug. But anyways, it gave me another hint, there could be something wrong with our Routing table. So I opened the route registration code in our project and realized that we were using a RouteConstraint for the business route:
routes.MapRoute(
  "Business",
  "Business/{slug}",
  new { controller = "Business", action = "Detail" },
  new { slug = new BusinessSlugConstraint()}
);
That means whenever it renders the link like "/Business/business-slug" it will check whether the slug "business-slug" exists. We have over 1 mil of businesses in database so it could took a lot of time for this check. For now, what we can do to make a quick fix is rendering the detail links manually without using UrlHelper. It could save us some time and we'll address this issue later. Anyway, from now on, we should be careful when using the RouteConstraint cos it would never jump into it's code when we press F11 unless we put a break point in the RouteConstraint implementation. Cheers

Tuesday, April 19, 2011

Optional Parameter Is Ugly

Optional Parameter has been introduced since C# 4.0. It's a nice feature indeed. Let's say we wanted to create a Html Helper method that can generate a Html markup of a link by providing the inner Html, controller and action name. The method would look like: HtmlLink(string innerHtml, string action, stirng controller) Definetily, there would be a requirement to provide a routeValue object to make the link in some cases like we want to have a link to edit a blog post, we would need something like: HtmlLink("Edit post", "Edit", "Blog", new {id = 1000}) Before C# 4.0, we had to create another overload for the above method. But with optional parameter we can easily archive this by adding the routeValue param as optional: HtmlLink(string innerHtml, string action, string controller, object routeValue = null) Cool, but the requirement would have a small change. We wanted to provide a css class name for the link so we had to change the method again: HtmlLink(string innerHtml, string action, string controller, object routeValue = null, object htmlAttribute = null) Hmmm, It's becoming more complicated but still okey because "everything is working fine" and "there is nothing wrong with it". Believe me, there are 2 common sentences that I hear everyday. Some days later, a guy in our team felt like it's time to make the method more handy by providing another optional parameter for area name. He wanted to make the change quickly without affecting the existing codes that have been using this method. So the method would be modified again: HtmlLink(string innerHtml, string action, string controller, string area = null, object routeValue = null, object htmlAttribute = null) Now, It's really a problem. The method had too many optional parameters. So when you want to use default area as null and provide route value object and htmlAttribute, then you have to use named arguments when call this method: HtmlLink("Edit post", "Edit", "Blog", routeValue: new {id = 1000}, htmlAttribute: new {@class = "edit-post"}) Given that we would use this powerful method to generate the links for some menu items. And there was a new requirement like if current page comes from the "menu item A" then this menu item should have css class "active". A guy who implemented that change could make this method even more "powerful" by adding another optional parameter: HtmlLink(string innerHtml, string action, string controller, string area = null, object routeValue = null, object htmlAttribute = null, bool isActive = false) It's not difficult to realize that this method's signature is so complicated due to the amount of parameters. It was cool at the first time but rapidly becomes ugly when being added more parameters. You would easily meet this kind of sittuation when you were in a team of many developers because there must be a time people want to have something done as quick as possible without thinking about clean code. I think optional parameter is good as soon as we use it in the reasonable way. For me, one optional parameter in a method is enough and 3 parameters should be the maximum of parameters in any methods. If we need more parameters, it's time to think about a DTO class.