Saturday, March 17, 2012

Dependency injection – Auto registration in Unity


Unity Auto Registration extends Unity IoC container and provides fluent syntax to configure rules for automatic type registration. You can add Unity Auto Registration package using NuGet package manager.



Using few code lines you can scan specified assemblies and register all types that satisfy the rules. Rules for determining whether to include/exclude type/assembly are predicates so you can use lambda syntax to specify them or direct method name. For e.g. you can register interfaces, using AsFirstInterfaceOfType() which will throw an exception if more than one interface is found on a type.  You can also use decorated attributes type to register types to the container.
For e.g. All my repositories that implement the type
public interface IRepository
{
    IEnumerable All();
    T One(Funcbool> condition);
    IEnumerable All(Funcbool> condition);
    void Add(T entity);
    void Save(T entity);
    void Remove(T entity);

    HashSet Context { get; }
}
public class CustomerRepository : BaseRepository<Customer>
{
    //Implementation methods
}
Can be registered as
_container = new UnityContainer();
_container.ConfigureAutoRegistration()
    .LoadAssembliesFrom(RegistrationAssemblies.AssemblyNames)
    .Include(r => r.ImplementsOpenGeneric(typeof(IRepository<>)), Then.Register().AsFirstInterfaceOfType())
    .ApplyAutoRegistration();
Where RegistrationAssemblies.AssemblyNames is a string array of the assemblies to look for types to register.
You can also decorate the types with a common attribute to register types. For e.g. the business logic components in this sample is decorated with the attribute [Logic] as given below

[Logic]
public class CustomerLogic : ICustomerLogic
{
    public IEnumerable<Customer> GetAllByName(string name)
    {
        var repository = IoC.Container.Resolve<IRepository<Customer>>();
        return repository.All(c => c.Name.StartsWith(name));
    }
}
These types can be registered using the DecoratedWith method as given below.
_container = new UnityContainer();
_container.ConfigureAutoRegistration()
    .LoadAssembliesFrom(RegistrationAssemblies.AssemblyNames)
    .Include(r => r.ImplementsOpenGeneric(typeof(IRepository<>)), Then.Register().AsFirstInterfaceOfType())
    .Include(If.DecoratedWith<Logic>, Then.Register().AsFirstInterfaceOfType())
    .ApplyAutoRegistration();

Thursday, March 15, 2012

Code duplication analysis in VS2011


Visual studio 2011 introduces the code clone analysis feature that allows developers for identification of similar code sections. The new feature enables developers to identify duplicate code in the solution which makes it difficult for making changes in the code, as the same change must be made in multiple places in the code base. Code clone analysis, by identifying these similar sections of code, makes it easier to refactor them.
You can launch the code clone analysis from the Analyze menu item, which shows the two exact matches for a code clone.



Code clone window will display the from – to line number along with the files name. You can click on that line to navigate to the code block or just mouse hover to see the cloned code.