Sunday, January 30, 2011

IoC / Dependency Injection


The basic idea behind Inversion of Control in context is dependency injection; you want to remove dependencies from your application and that dictates need of a tool to plug in these dependencies.
Other terms you might have heard floating around in this context is Contructor, Setter and Interface Injection; which is way of programming where and how dependecies are injected.
In example below; your car listing service depends on car repository whcih connects to db and gets you data.

public class CarLister
      ICarRepository _repository;
      public CarLister(ICarRepository repository){
            _repository = repository;

There are numerous dependency injection frameworks available; I have used structure map and find is sufficient for needs of application. Idea is to scan assemblies and generate plug in graph for all objects; this allows structure map to contruct objects on demand.

Below is how to configure structure map; configuration below would allow you to scan all assemblies and register all declared registries.

 public class Bootstrapper
        public static void Bootstrap()
            //Initialize StructureMap
            ObjectFactory.Initialize(r =>
                r.Scan(assembly =>
                    if (assembly == null)
                    assembly.AddAllTypesOf(typeof (IStartupTask));                                                            


       /// <summary>
       /// Helper method to determine the project's assemblies to scan
       /// </summary>
       internal static void ScanProjectAssemblies(IAssemblyScanner assembly)

You can declare registries as per your need:

public class DomianRegistry: Registry
                  public DomianRegistry()

C# - Elegant Sorting and Paging Solution

Paging is so simple that it doesn't warrants a discussion anymore:


Sorting is still tricky; I have a simple utility for it. Utility would allow to sort any object collection on any property defined in type safe way.


1. Sorting:
var orderedResults = SorterUtility.Sort(viewModel, sortColumn, sortDirection);

2. Generating sort column delegate type safely:
sortColumn = SorterUtility.GetPropertyName<ViewModel>(x => x.Description)

Utility Code:

public class SorterUtility
        public static IEnumerable<T> Sort<T>(IEnumerable<T> list, string sortColumn, SortDirection direction)
            return direction == SortDirection.Ascending
                       ? list.ToList().OrderBy(CreateSortField<T>(sortColumn))
                       : list.ToList().OrderByDescending(CreateSortField<T>(sortColumn));

        public static string GetPropertyName<T>(Expression<Func<T, object>> expression)
            MemberExpression memberExpression = null;
            if (expression.Body.NodeType == ExpressionType.Convert)
                var convert = expression.Body as UnaryExpression;
                if (convert != null)
                    memberExpression = convert.Operand as MemberExpression;
            return memberExpression == null
                       ? ExpressionHelper.GetExpressionText(expression)
                       : memberExpression.Member.Name;

private static Func<T, object> CreateSortField<T>(string fieldName)
            //returns m => m.FieldName where M is the type & FieldName is a property name
            var classType = typeof (T);
            var param = Expression.Parameter(classType, "m");
            var memberInfo = classType.GetProperty(fieldName);
            var expr = Expression.MakeMemberAccess(param, memberInfo);
            var convert = Expression.Convert(expr, typeof (object));
            return Expression.Lambda<Func<T, object>>(convert, param).Compile();

Choosing your DB

A free one!

To begin developing an application its not important to pick the db; whats important is to make sure it doesn't matter. If you are working in well financed organization you would be able to buy good DBMS; DBMS concern should be to facilitate storing, backing up, recovering, secure access and such. These concerns are important; however does not matter as far as application development is concerned.

To build in anti corruption layer and decouple DB we require two things;

1. Bid "Goodbye!" to SQL programming; no more stored procedure for you.
2. Pick an ORM which publishes APIs capable of interacting with db the way your application need.
3. Any good ORM would let you switch our DB type; make sure yours does.

MVC for User Interface layer

Benefits : The biggest benefit has to be decoupling of presentation; MVC allows presentation (UX or UI) to work in isolation and establishes boundaries. Specialized developers can work on UI without having to worrying about how domain entities are structured and vice-versa.

There are of course lot of other benefits which come with ownership of UI artifacts by UI developers.

a. Device dependent presentation layer stays out of your code base.
b. Accelerates development and changes to presentation layer.
c. Facilitates automation testing of everything beneath presentation. UI based automated tests are harder to write and expensive to maintain; MVC would allow to write tests on view models, controllers and domain layer; giving you bigger bang of your buck.
d. Accelerates development of business logic by freeing developers from UI concerns.

Typical Microsoft MVC set up doesn't take much; just start by creating a MVC application in Visual studio.

1. Set up views - Html, javascript/jquery and style sheet. Utilizes view model to interact with domain. Views setup requires at least four folders in web application folder; set up Views, View models, styles and scripts folders.

View models concerns - Typical view model exposes properties required by view; houses logic for conversion of domain entities to view model. Look up auto mappers.

2. Set up Controllers - Controller should have there own folder. Controllers and view models do not need live in web application assembly; I would leave there unless there is compelling reason not to. This is where MVC ends.

Controller concerns - Interact with domain; pass on data back and forth between domain and view. Code layout of controller is typical facade pattern if anything. Controller commands domain to process view interactions and view model to process domain entities to formulate a view.

NOTE: I find its important to come up with clean folder organization; I have seen following same structure with in each artifact folder works very well.

Setting up enterprise level web application

A modern web application today can benefit from decoupling; setting up each layer as self contained and treating dependencies as such seems to be the key. Many of us have seen technology come and go with our years in development. Open source revolution is picking momentum; products are being published faster and competition is struggling to keep up. APIs are deprecated and new releases are being published with in months not years. You need to be free to take advantage of all the advancements without paying a big price for changing your mind.

My experience is strictly in Microsoft and open source products which work with Microsoft. I will like to talk about the technical stack and how to build infrastructure pieces with anti-corruption layers and start developing application fast!

MVC for User Interface layer

DDD for Domain layer

ORM for data access layer

IoC / Dependency Injection

Automated Testing

Automated Build and publishing Setup

Choosing your db