Showing posts with label Dependency Injection. Show all posts
Showing posts with label Dependency Injection. Show all posts

Monday, 29 August 2016

Choosing an IoC Container talk at Israeli .NET Developers User Group

Last week I had the honor to hold a talk at the Israeli .NET Developers User Group. The subject was "Choosing an IoC Container", but the main goal was not to choose the best container ever, rather to present different features of IoC Containers and provide the tools to compare between them. Based on the feedback I received from the survey, it achieved the goal.

I would like to share the slides and the code samples presented during the talk.



Sunday, 4 August 2013

Type safe configuration mapper

Almost every project needs some sort of configuration, that is external of the source code. Sometimes we spend time to find the best library that fits our needs and sometimes we give up and write something in-house. I would like to save your time in both these tasks and suggest what I have found and I use in my projects.

I was looking for a library that would allow me to represent all parameters as an interface or class. The parameters would be strongly typed with minimum overhead in mapping parameter to property. Providing default values would be also nice, but not necessarily. The best library that exactly fits these needs is ConfigReader. Here are a few simple steps how to use it in your project:

1. Put your configuration parameters in the regular app.config or web.config file. Parameters may be of any primitive data types:
 

 <appsettings>
    <add key="ApplicationConfiguration.Duration" value="30" />
    <add key="ApplicationConfiguration.Distance" value="3" />
    <add key="ApplicationConfiguration.Author" value="Boris Modylevsky" />
 </appsettings>

2. Create an interface with properties to represent the configuration in code. Give properties the same names as parameters names. Properties may be read-only - having only getter without setter. We don't need to specify no special mapping attributes, nothing. Just an interface with properties. Here is a corresponding interface:
 

public interface IApplicationConfiguration
{
    int Duration { get; }
    double Distance { get; }
    string Author { get; }
}
3. Next step we need to initialize our configuration interface from the configuration file. If you are using Inversion of Control (IoC) containers, it is better to to in the bootstrapper - where all the classes are registered in the container. I will show how this could be done in Autofac. First of all we create a ConfigurationModule class, which defines configuration registration in Autofac:
 
using System.Diagnostics.Contracts;
using Autofac;
using ConfigReader;

public class ConfigurationModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        Contract.Assert(builder != null, "Builder container is null");

        var configReader = new ConfigurationReader().SetupConfigOf<IApplicationConfiguration>();
        var configuration = configReader.ConfigBrowser.Get<IApplicationConfiguration>();
        builder.RegisterInstance(configuration).As<IApplicationConfiguration>();
    }
}
4. Then we just register the ConfigurationModule in our container builder and that's all!
 

using Autofac;

public static class Bootstrapper
{
    public static IContainer Container { get; private set; }

    public static IContainer Initialize()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterModule<ConfigurationModule>();
        Container = containerBuilder.Build();
        return Container;
    }
}
5. Use it by defining as constructor parameter. IoC container will take care to transfer the correct instance into your constructor:
 
public class SomeClassThatUsesConfiguration
{
    private readonly IApplicationConfiguration _configuration;

    public SomeClassThatUsesConfiguration(IApplicationConfiguration configuration)
    {
        _configuration = configuration;
    }

    public double GetVelocity()
    {
        return _configuration.Distance/_configuration.Duration;
    }
}
If you like the above and would like to use it in your project, download it from nuget.org:

PM> Install-Package ConfigReader




Tuesday, 3 July 2012

How to avoid "is never used" ReSharper warning when class is used in IoC container?

Those of us who use ReSharper and some IoC container, must be familiar with the situation. You write a class, then register it in your container and everything works fine. However, ReSharper marks the class as "is never instantiated" or "is never used". Here a new plugin from Igal Tabachnik comes to our help. It's called Agent Multer and here a short video on how it works.

At the time of writing these lines, the plugin support Ninject, Windsor, Unity and Autofac containers. More details at: http://hmemcpy.github.com/AgentMulder/