Reputation: 8422
I have the following unit of work. It's composed to allow me to add repositorie via my IOC, and then access those repositories in my services based on type - without having to rewrite the unit of work each time a new repository comes on the scene. From the research I've done, using Activator for this is not enough of a performance impact to worry about.
public class DbContextUnitOfWork : IDisposable
{
public DbContextUnitOfWork(DbContext dbContext))
{
// Set DbContext
}
public void AddRepository<T>() where T : IRepository
{
// Activate T and inject DbContext
// Add to singleton list
}
public T GetRepository<T>() where T : IRepository
{
// If T exists on list return T
}
// Other methods such as save, dispose, etc.
}
My question is, how do I configure Windsor so that when it finishes resolving DBContextUnitOfWork I can explicitly call the AddRepository method on it for every repository I need to add?
Upvotes: 2
Views: 368
Reputation: 8422
In the end I worked out I could use Component.For().UsingFactory(), and I could register a custom factory which built the UnitOfWork for me.
Upvotes: 1
Reputation: 43046
Presumably you're adding the repositories because they'll be needed at some later point. The usual approach is to register the repository types with Windsor, and ask Windsor to resolve the repository at that point, rather than using the activator to create them up front.
Upvotes: 1