Injecting a instance of a class to a constructor
Is this the bes开发者_Python百科t practice for injecting a class dependency into a repository? Bear in mind that other repositories will need this PetaPoco.Database instance as I want each repository to use a shared database connection object.
public class ConfigRepository : IConfigRepository
{
private Database DB;
public ConfigRepository(PetaPoco.Database db)
{
DB = db;
}
}
//Here is how structuremap is configured
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});
x.Register<PetaPoco.Database>(new PetaPoco.Database("DBConnection"));
x.For<IConfigRepository>().Use<ConfigRepository>();
});
return ObjectFactory.Container;
I'm told this is what you need:
x.For<PetaPoco.Database>().Singleton().Use(()=>new PetaPoco.Database("connectionString"));
精彩评论