将类的实例注入构造函数
这是将类依赖项注入存储库的最佳实践吗?请记住,其他存储库将需要此 PetaPoco.Database 实例,因为我希望每个存储库都使用共享数据库连接对象。
public class ConfigRepository : IConfigRepository
{
private Database DB;
public ConfigRepository(PetaPoco.Database db)
{
DB = db;
}
}
//这里是结构图的配置方式
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;
Is this the best 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我听说这就是你所需要的:
I'm told this is what you need: