Настройте CTOR класса свойства с помощью StructureMap (4.7.1)
Мне нужен способ настроить конкретный CTOR для свойств ICompositeRepository<> в UnitOfWork?
// -------------------
// CONTAINER REGISTERY
public class ContainerRegistry : Registry
{
public ContainerRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.LookForRegistries();
scan.SingleImplementationsOfInterface();
});
// DbRepository: This maps the common-interface to a common-concrete
For(typeof(ICompositeRepository<>)).Use(typeof(DbRepository<>));
// Namespaced UnitOfWork's: These map concrete-DbContext's to a UoW property
ForConcreteType<Bar.UnitOfWork>().Configure.Setter(x => x.DbContext).Is<Bar.DbContext>();
ForConcreteType<Foo.UnitOfWork>().Configure.Setter(x => x.DbContext).Is<Foo.DbContext>();
// HERE IS THE ISSUE - ICompositeRepository: I need to "feed" the concrete DbRepository's CTOR the correct DbContext
ForConcreteType<Bar.UnitOfWork>().Configure.Setter<ICompositeRepository<>>...? //<-- This must be Bar.DbContext
ForConcreteType<Foo.UnitOfWork>().Configure.Setter<ICompositeRepository<>>... ? //<-- This must be Foo.DbContext
// Somethink "like" this. But I cant find the right combination of commands
//ForConcreteType<Bar.UnitOfWork>().Configure.Setter(DbRepository<>().Ctor<System.Data.Entity.DbContext>().Is<Bar.DbContext<;
//ForConcreteType<Foo.UnitOfWork>().Configure.Setter(DbRepository<>().Ctor<System.Data.Entity.DbContext>().Is<Foo.DbContext>;
}
}
// -----------------------
// BAR - NAMESPACE CLASSES
namespace Bar
{
public class DbContext : DbContextBase
{
[DefaultConstructor]
public DbContext() : base("ConnectionString.Database.MyAwesomeDb")
{
}
public DbSet<Truck> Trucks { get; set; }
}
public class UnitOfWork : Common.IUnitOfWork
{
[SetterProperty]
public DbContext DbContext { get; set; }
[SetterProperty]
public ICompositeRepository<Truck> Trucks { get; set; }
}
public class Truck { }
}
// -----------------------
// FOO - NAMESPACE CLASSES
namespace Foo
{
public class DbContext : DbContextBase
{
[DefaultConstructor]
public DbContext() : base("ConnectionString.Database.AnotherAwesomeDb")
{
}
public DbSet<Railcar> RailCars { get; set; }
}
public class UnitOfWork : Common.IUnitOfWork
{
[SetterProperty]
public DbContext DbContext { get; set; }
[SetterProperty]
public ICompositeRepository<Railcar> RailCar { get; set; }
}
public class Railcar {}
}
// COMMON - LIBRARY CLASSES
namespace common
{
public class DbContextBase : System.Data.Entity.DbContext
{
public DbContextBase(string nameOrConnectionString) : base(nameOrConnectionString)
{ }
protected virtual new void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
public class DbRepository<TEntity> : ICompositeRepository<TEntity> where TEntity : class
{
private System.Data.Entity.DbContext _dbContext;
private readonly DbSet<TEntity> _dbSet;
public DbRepository(System.Data.Entity.DbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<TEntity>();
}
// All Other proerties are unlisted (for simplicity)
}
}