2016-06-01 6 views
0

우리는 nvject를 사용하여 종속성 주입을 사용하려는 Mvc 응용 프로그램을 작성 중입니다. 현재 우리는 다른 클래스 라이브러리 "ShopEntities"에있는 엔터티를 유지 관리하고 있으며 우리의 mvc 응용 프로그램에서이 엔터티를 사용하고 있습니다. ShopEntities의 수업을 생각해 봅시다. 우리는 우리의 MVC 응용 프로그램에서 사용할 때인터페이스없이 Ninject를 사용하는 종속성 삽입

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;} 

    } 
} 

지금, 우리는

public ActionResult Index() 
{ 
    ShopEntities.Customers cust = new ShopEntities.Customers(); 
    cust.CustName = "Sam"; 
    cust.IAddress = "xyz"; 
    cust.ShippingAddress = "xyz xyx xyz"; 

} 

어떻게 의존성을 피하기 위해 여기 Ninject에를 사용하여, 인스턴스를 생성하고 아래와 같이 속성을 설정? 인터페이스 범위를 제한하기 때문에 인터페이스를 생성하고 싶지 않습니다. 미리 감사드립니다. 내가 당신에게 질문을 이해하면

+3

[의존성 삽입 방지 패턴 : 런타임 데이터를 구성 요소에 주입] (https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99). 엔티티에 DI를 사용하거나 인터페이스를 생성하면 안됩니다.이 인터페이스는 런타임 데이터입니다. DI는 응용 프로그램 구성 요소를 작성하기위한 것입니다. – NightOwl888

답변

5

방법을 추상화로 표현 계층에서 Customer 엔티티를 사용하지 숨기는 것입니다 엔티티 자체가 어떤 종류의 ICustomer 뒤에 있으며, DI 컨테이너가이를 빌드하도록 허용하지 않습니다. 인터페이스 뒤에서 데이터 객체를 숨기는 것은 일반적으로 유용하지 않습니다. 인터페이스는 데이터가 아니라 동작을 추상화합니다.

이미 NightOwl stated으로, 당신의 Customer 개체는 런타임 데이터이고 당신은 하지 런타임 데이터를 포함하는 객체 그래프를 구축하기 위해 컨테이너를 사용해야합니다.

대신 추상화를 통해 특정 비즈니스 작업을 숨겨야합니다. 이러한 추상화는 표현 계층에 의해 소비되고 비즈니스 계층에 의해 구현 될 수 있습니다. 예를 들어 :

public interface ICustomerServices 
{ 
    void CreateCustomer(string customerName, string homeAddress, 
     string shippingAddress); 

    void ChangeShippingAddress(Guid customerId, string shippingAddress); 
} 

이 추상화에 따라 달라질 수 있습니다 컨트롤러 :

public class CustomerServices : ICustomerServices 
{ 
    private readonly EntitiesContext context; 

    public CustomerServices(EntitiesContext context) { 
     this.context = context; 
    } 

    public void CreateCustomer(string customerName, string homeAddress, 
     string shippingAddress) 
    { 
     // NOTE that I renamed 'Customers' to 'Customer', since it holds information 
     // to only one customer. 'Customers' implies a collection. 
     Customer cust = new ShopEntities.Customer(); 
     cust.CustName = "Sam"; 
     cust.IAddress = "xyz"; 
     cust.ShippingAddress = "xyz xyx xyz"; 

     this.context.Customers.Add(cust); 

     this.context.SubmitChanges(); 
    } 

    public void ChangeShippingAddress(...) { ... } 
} 

수행 :

private readonly ICustomerServices customerServices; 

public CustomerController(ICustomerServices customerServices) { 
    this.customerServices = customerServices; 
} 

public ActionResult Index() 
{ 
    this.customerServices.CreateCustomer("Sam", "xyz", "xyz xyz xyz"); 
} 

는 이제 비즈니스 계층 내부 엔티티를 사용하여이 추상화에 대한 구현을 만들 수 있습니다 이것은 당신이 당신의 프리젠 테이션 레이어를 얇게 유지할 수 있다는 장점을 가지고 있습니다, 그러나 대안들에 비해 제시된 접근 방식에 여전히 상당한 단점이 있습니다. 그러한 대안 중 하나는 here으로 설명되는 SOLID 설계의 메시지 기반 접근 방식을 사용하는 것입니다.

0

, 당신은 당신의 자신의 엔티티에 ShopEntities를 변환하는 중간 비즈니스 계층을 만들어야합니다

namespace MyShopEntities 
{ 
    public class MyCustomers 
    { 
     public int custId {get;set;} 

     public string custName {get;set;} 
     public string Address {get;set;} 
     public string ShippingAddress {get;set;} 

    } 
} 

public ActionResult Index() 
{ 
    ShopEntities.Customers cust = new MyShopEntities.MyCustomers(); 
    cust.CustName = "Sam"; 
    cust.IAddress = "xyz"; 
    cust.ShippingAddress = "xyz xyx xyz"; 

} 

class BussinesModel 
{ 
    void Insert(ShopEntities.Customer customer) 
    { 
    // use ShopEntities.Customer only in wrapper 
    // if you later switch to another Customer dependency, 
    // you just change this  wrapper 

    MyShopEntities.MyCustomers cust = new MyShopEntities.MyCustomers(); 
    cust.CustName = customer.CustName; 
    cust.IAddress = customerIAddress; 
    cust.ShippingAddress = customer.ShippingAddress; 
    InsertInternal(cust); 
    } 

    void InsertInternal(MyShopEntities.MyCustomer customer) 
    { 
     // use MyCustomer for all your bussines logic 
    } 
} 
관련 문제