6

Can someone please explain in plain English what the lines of code where I put the question marks do? Or maybe point me to an article that puts light on this. This code is for registering dependencies in an autofac container

var builder = new Autofac.ContainerBuilder();


builder.Register<NHibernateInstance>(c => 
    new NHibernateInstance(ConnString, false))
       .InstancePerDependency();//?????

builder.Register(c => c.Resolve<NHibernateInstance>()
    .GetFactory().OpenSession())
    .As<ISession>()
    .InstancePerLifetimeScope(); //-----?????
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
Foo
  • 4,206
  • 10
  • 39
  • 54

1 Answers1

3

This is a dependency injection container. The Autofac.ContainerBuilder gets a new container, or registrar you might say.

The builder.Register<NHibernateInstance> is stating that when constructing an NHibernateInstance during the recovery phase (i.e. getting an instance out of the container) this is how it should be built.

The last line is indicating that when resolving an NHibernateInstance the OpenSession method should be called once per the lifetime of the object.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232