Tuesday, March 29, 2011

Dependency Injection (DI) Registration (using StructureMap) used is non-IDependencyResolver scenarios

Bootstrapping code for dependency injection (DI) registration (using StructureMap) used is non-IDependencyResolver scenarios - put this into a separate file:

public static class BootStrapper
{
    public static void Bootstrap()
    {
        ObjectFactory.Initialize(x =>
        {
            x.AddRegistry(new EFRepositoryRegistry());
            x.AddRegistry(new ServicesRegistry());
        });
 
        DependencyResolver.SetResolver(
            t =>
            {
                try
                {
                    return ObjectFactory.GetInstance(t);
                }
                catch
                {
                    return null;
                }
            },
            t => ObjectFactory.GetAllInstances<object>().Where(s => s.GetType() == t)
            );
    }
}

Invoke the DI registration in the Application_Start() method in the Global.asax:

protected void Application_Start()
{
    BootStrapper.Bootstrap();
}

No comments:

Post a Comment