Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac dependency injection implementation

I started working on a new project and I come from a direct and 'naive' programming.

Right now I'm concerning about using IoC container, specifically on Dependency Injection patter using Autofac.

Let's say I have a simple session factory :

namespace Warehouse.Data
{
    public class SessionFactory
    {
        private static ISessionFactory _sessionFactory;
        private static ISystemSetting _systemSetting;

        SessionFactory(ISystemSetting systemSetting)
        {
            _systemSetting = systemSetting;

            InitializeSessionFactory();
        }

        private static void InitializeSessionFactory()
        {
            _sessionFactory = Fluently.Configure()
                .Database(DatabaseConfiguration)
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyMap>())
                .BuildSessionFactory();
        }

        public static ISession OpenSession()
        {
            return _sessionFactory.OpenSession();
        }
    }
}

And in Bootstrap.cs, I configure autofac like this :

namespace Warehouse.Infrastructure
{
    using Autofac;

    public class Bootstrap
    {
        public IContainer Configure()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<SystemSetting>().As<ISystemSetting>();
            builder.RegisterType<UserRepository>().As<IUserRepository>();

            return builder.Build();
        }
    }
}

My question is this :

  1. How do I use Autofac to resolve SessionFactory dependency to ISystemSetting? Do I need to use builder.Resolve<ISystemSetting> as a parameter everytime I want to use SessionFactory?
  2. Dependency Injection pattern, or maybe just Autofac, comes with lots of new word such as Service, Resolve, Singleton, etc. Where can I learn these things from scratch? Is it the same for every other DI Framework?
  3. I need to understand how IoC Container works in a project with multiple layer, do every layer needs a reference to Autofac?

Thank you.

like image 445
Samuel Adam Avatar asked Apr 26 '13 09:04

Samuel Adam


1 Answers

  1. you have done it already in your bootstrap.

    builder.RegisterType<SystemSetting>().As<ISystemSetting>();
    

    that means that every oject that has a dependency on ISystemSettings gets an instance of SystemSettings. so if you use

    var mySessionFactory = myContainer.Resolve<SessionFactory>();
    

    somewhere in your code (you really should do that in your composition root) the container will do the job for you. If you have a lot of objects, with multiple dependencies you will start to understand why IoC containers are "magic" ;)

  2. well .. a lot of IoC containers use the word resolve. it just means "give me an object with its dependencies". service and singleton are words from the object oriented design laguage. they are not specific to IoC containers. you can google them up. I think there no summary for stuff like this. you will get it in time by reading books, articles and going through tutorials.

  3. no. actually that would be a no go. there is a pattern for that called servicelocator which is considered by some people to be an antipattern. if you follow this pattern, all your objects have exactly one dependency, the container! and they themself get out of it what they need.

    public class MyClass
    {
        private DependencyOne dep1;
    
        public MyClass(WhatEverContainer container)
        {
            dep1 = container.Resolve<DependencyOne>();
        }
    }
    

    ... in that case your container would act as a servicelocator and every object that needs a dependency would ask the servicelocator to get that dependency. that would undermine the whole value of INVERSION of control and make your objects dependent on a container. inject what the objects actually need, not what they need in order to locate what they actually need ;D

    let your objects be container agnostic. and use your container in your composition root, thats where you glue the objects and the layers of your application together. here something to read: http://blog.ploeh.dk/2011/07/28/CompositionRoot/

like image 92
Egi Avatar answered Sep 21 '22 00:09

Egi