使用静态方法时,使用 Unity 集成依赖注入的最佳方法是什么?
我面临着即将升级到 ASP.NET 站点,我正在考虑使用 Unity 引入 DI。我研究了 ASP.NET DI 方面的问题,并有 2 个选项(Global.asax 或 IHttpModule)。我很高兴使用其中任何一个。
与往常一样,我想升级/更改一个旧的对象模型。我想沿着构造函数注入(传递 DAO)的路线走下去,但是,每个对象都有静态方法和实例方法的混合,这些方法当前都使用 SqlCommand 对象本身调用数据库。我希望删除它以提供数据库独立性,因此任何人都可以建议在这种情况下执行 DI 的最佳方法吗?如果需要的话,我愿意接受巨大的改变。
public class ExampleClass
{
public ExampleClass(int test)
{
TestProperty = test;
}
public int TestProperty {get; set;}
public int Save()
{
// Call DB and Save
return 1;
}
public static ExampleClass Load(int id)
{
// Call DB and Get object from DB
return new ExampleClass(1);
}
}
感谢您的任何建议
马特
Im faced with an impending upgrade to an ASP.NET site and I am thinking of introducing DI using Unity. I have researched the ASP.NET DI side of things and have 2 options (Global.asax or IHttpModule). Im happy to use either.
As always, there is a legacy object model in place that I would like to upgrade/change. I would like to go down the route of Constructor injection (passing in the DAO) however, each object has a mix of static and instance methods that both currently call into the DB using SqlCommand objects themselves. I want this removed to provide DB independence, therefore can anyone suggest the best way to do the DI in this case? Im open to drastic changes if they are needed.
public class ExampleClass
{
public ExampleClass(int test)
{
TestProperty = test;
}
public int TestProperty {get; set;}
public int Save()
{
// Call DB and Save
return 1;
}
public static ExampleClass Load(int id)
{
// Call DB and Get object from DB
return new ExampleClass(1);
}
}
Thanks for any suggestions
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您删除所有静态方法并引入一些抽象,您应该可以开始:
在您的 ASP 页面中:
If you remove all static methods and introduce some abstractions you should be good to go:
and in your ASP page: