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 :
builder.Resolve<ISystemSetting>
as a parameter everytime I want to use SessionFactory?Thank you.
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" ;)
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With