초기 경고 : 긴 포스트와 나는 어쨌든 완전히 잘못된 패턴을 가지고 있습니다이 도메인 데이터 모델링 시나리오에서 참조 데이터에 액세스하는 올바른 방법은 무엇입니까?
고객 집계의 시작 다음 클래스 감안할를 :
public class Customer : KeyedObject
{
public Customer(int customerId)
{
_customerRepository.Load(this);
}
private ICustomerRepository _customerRepository = IoC.Resolve(..);
private ICustomerTypeRepository = _customerTypeRepository = IoC.Resolve(..);
public virtual string CustomerName {get;set;}
public virtual int CustomerTypeId [get;set;}
public virtual string CustomerType
{
get
{
return _customerTypeRepository.Get(CustomerTypeId);
}
}
}
그리고 CustomerType은로 표현된다 값 개체 :
public class CustomerType : ValueObject
{
public virtual int CustomerTypeId {get;set;}
public virtual string Description {get;set;}
}
CustomerTypeId를 사용하여 고객 개체를 만들 때 유용합니다. 그러나 MVC 뷰 내에서 DropDownList를 채울 때 ICustomerTypeRepostory에서 CustomerType 값 목록을 올바르게 가져 오는 방법에 대한 개념에 어려움을 겪고 있습니다.
ICustomerTypeRepository
은 매우 간단합니다 : 기본적으로
public interface ICustomerTypeRepository
{
public CustomerType Get(int customerTypeId);
public IEnumerable<CustomerType> GetList();
}
, 내가 원하는 것은 내 컨트롤러에서 제대로 ICustomerTypeRepository
를 호출 할 수있다, 그러나 나는 그것을에서 DAL (저장소) 층을 분리하는 가장 좋은 것이라고 생각 컨트롤러. 지금, 나는 단지 것들을 overcomplicating? 나는 컨트롤러 및 고객의 저장소를 가지고로
public class CustomerController : ControllerBase
{
private ICustomerTypeRepository _customerTypeRepository = IoC.Resolve(..);
public ActionResult Index()
{
Customer customer = new Customer(customerId);
IEnumerable<CustomerType> customerTypeList =
_customerTypeRepository.GetList();
CustomerFormModel model = new CustomerFormModel(customer);
model.AddCustomerTypes(customerTypeList);
}
}
이 나에게 잘못 보인다
이 내 컨트롤러가 현재의 약자 방법이다. CustomerType에 대해 단편화 된 액세스 레이어가 있어야한다는 것이 내게 논리적 인 것처럼 보입니다. 나는. CustomerType.GetList()
:
CustomerController
에
ICustomerTypeRepository
을 통해
CustomerType
오브젝트를하는 방식에 노출되어야한다 그래서
public class CustomerType : ValueObject
{
// ... Previous Code
private static ICustomerTypeRepository _customerTypeRepository = IoC.Resolve(..);
public static IEnumerable<CustomerType> GetList()
{
_customerTypeRepository.GetList();
}
}
?
'고객'유형의 고정 목록이 있습니까? 아니면 추가되는 새로운 사용자 생성 고객 유형이 있습니까? – Jay
@ 제이 - 사실, 이것은 단지 예일뿐입니다. 그러나 사용자가 생성 한 고객 유형이 추가되어 열거 형 등을 사용할 수 없습니다. – GenericTypeTea