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?
.