방법을 추상화로 표현 계층에서 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 설계의 메시지 기반 접근 방식을 사용하는 것입니다.
[의존성 삽입 방지 패턴 : 런타임 데이터를 구성 요소에 주입] (https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99). 엔티티에 DI를 사용하거나 인터페이스를 생성하면 안됩니다.이 인터페이스는 런타임 데이터입니다. DI는 응용 프로그램 구성 요소를 작성하기위한 것입니다. – NightOwl888