Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created May 15, 2011 04:54
Show Gist options
  • Save cammerman/972896 to your computer and use it in GitHub Desktop.
Save cammerman/972896 to your computer and use it in GitHub Desktop.
Sample desired syntax for declarative definition of bound lifetime scopes.
// Container context tags
enum EContainerContext
{
UnitOfWork,
Plugin,
MultiInstanceWindow
}
// Registration code for an object that owns a lifetime scope
builder.RegisterType<MyDbContext>()
.As<DbContext>();
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment