Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created May 15, 2011 04:54

Revisions

  1. cammerman revised this gist May 15, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LifetimeScopeSyntaxSample.cs
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@ enum EContainerContext
    MultiInstanceWindow
    }

    // Registration code for an object that owns a lifetime scope
    builder.RegisterType<MyDbContext>()
    .As<DbContext>();

    // Registration code for an object that owns a lifetime scope
    builder.RegisterType<UnitOfWork>() // Has dependency on DbContext.
    .As<IUnitOfWork>()
    .EstablishesLifetimeScope() // Desired syntax. Indicates each new UnitOfWork should have a new lifetime scope bound to the life of the object. Also implies the UnitOfWork itself has per-dependency lifetime scope. Each time requested outside the bound lifetime scope, a new one is created.
  2. cammerman created this gist May 15, 2011.
    24 changes: 24 additions & 0 deletions LifetimeScopeSyntaxSample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Container context tags
    enum EContainerContext
    {
    UnitOfWork,
    Plugin,
    MultiInstanceWindow
    }

    builder.RegisterType<MyDbContext>()
    .As<DbContext>();

    // Registration code for an object that owns a lifetime scope
    builder.RegisterType<UnitOfWork>() // Has dependency on DbContext.
    .As<IUnitOfWork>()
    .EstablishesLifetimeScope() // Desired syntax. Indicates each new UnitOfWork should have a new lifetime scope bound to the life of the object. Also implies the UnitOfWork itself has per-dependency lifetime scope. Each time requested outside the bound lifetime scope, a new one is created.
    .WithTag(EContainerContext.UnitOfWork); // Desired syntax. Specifies the tag to attach to the generated lifetime scope.

    builder.RegisterType<DomainType1Repository>() // Has dependency on IUnitOfWork.
    .As<IRepository<DomainType1>>()
    .InstancePerMatchingLifetimeScope(EContainerContext.UnitOfWork);

    builder.RegisterType<DomainType2Repository>() // Has dependency on IUnitOfWork.
    .As<IRepository<DomainType2>>()
    .InstancePerMatchingLifetimeScope(EContainerContext.UnitOfWork);