Autofac Dependency Deployment Implementation

I started working on a new project, and I came from direct and “naive” programming.

I'm currently talking about using an IoC container, specifically in the Infusion Injection patter program using Autofac.

Say I have a simple factory session:

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 as follows:

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:

  • How to use Autofac to resolve SessionFactory dependencies with ISystemSetting? Do I need to use builder.Resolve<ISystemSetting>as a parameter every time I want to use SessionFactory?
  • The dependency injection pattern, or maybe just Autofac, comes with a lot of new words like Service, Resolve, Singleton, etc. Where can I learn these things from scratch? Is this the same for all other DI schemes?
  • , IoC Container , Autofac?

.

+5
1
  • .

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

    , , ISystemSettings, SystemSettings. ,

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

    - ( ), . , , , IoC "";)

  • .. IoC "". " ". service singleton - - laguage. IoC. google . , . , , .

  • . . servicelocator, . , , ! , .

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

    ... servicelocator, , , servicelocator . . , , , , , , D

    . , . - : http://blog.ploeh.dk/2011/07/28/CompositionRoot/

+8

All Articles