【发布时间】:2016-06-06 21:48:38
【问题描述】:
我们正在开发一个 Mvc 应用程序,我们希望使用 nInject 来使用依赖注入。目前我们在不同的类库“ShopEntities”中维护实体,在我们的 mvc 应用程序中我们正在使用这个实体。 让我们考虑 ShopEntities 中的一个类。
namespace ShopEntities
{
public class Customers
{
public int custId {get;set;}
public string custName {get;set;}
public string Address {get;set;}
public string ShippingAddress {get;set;}
}
}
现在当我们想在我们的 mvc 应用程序中使用它时,我们创建一个实例并设置如下属性,
public ActionResult Index()
{
ShopEntities.Customers cust = new ShopEntities.Customers();
cust.CustName = "Sam";
cust.IAddress = "xyz";
cust.ShippingAddress = "xyz xyx xyz";
}
这里如何使用nInject来避免依赖?此外,我们不想创建接口,因为它的范围有限。提前致谢。
【问题讨论】:
-
Dependency-injection anti-pattern: Injecting runtime data into components。您不应该为实体创建接口或使用 DI - 它们是运行时数据。 DI 用于组合应用程序组件。
标签: c# asp.net-mvc dependency-injection ninject