티스토리 뷰

ETC

[개발/도메인] 도메인 모델 종류

주인장 진빼이

# anemic domain model

빈약한 도메인 모델 => 클래스 내용이 부실하다.비즈니스 로직(유효성 검사, 연산 로직 등)이 포함되어 있지 않고

멤버변수와 게터 세터만 있는 객체를 의미한다.반대로 의미되는 개념으로 rich domain model 이 존재한다.

 

이 모델은 안티패턴 중 하나로 볼 수 있다.

 

클래스 간단한 예시 (코드는 C#으로 작성되었다)

public class Customer : Person
{
  public Customer()
  {
    Orders = new List<Order>();
  }
  public ICollection<Order> Orders { get; set; }
  public string SalesPersonId { get; set; }
  public ShippingAddress ShippingAddress { get; set; }
}
public abstract class Person
{
  public int Id { get; set; }
  public string Title { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string CompanyName { get; set; }
  public string EmailAddress { get; set; }
  public string Phone { get; set; }
}

 

 

 

# rich domain model

빈약하지 않은 도메인 모델 => 클래스 내용이 풍부하다.

비즈니스 로직(유효성 검사, 연산 로직 등)을 포함하고 있으며, 게터, 세터를 포함하고 있다

반대로 의미되는 개념으로 anemic domain model 이 존재한다.

 

프로젝트 사이즈가 클 수록 rich domain model을 권장하며

rich domain model에선 일급 컬렉션 사용하여 코드관리에 조금 더 신경쓸 수 있다.

 

public class Customer : Contact
{
  public Customer(string firstName, string lastName, string email)
  {
    FullName = new FullName(firstName, lastName);
    EmailAddress = email;
    Status = CustomerStatus.Silver;
  }
  internal Customer()
  {
  }
  public void UseBillingAddressForShippingAddress()
  {
    ShippingAddress = new Address(
      BillingAddress.Street1, BillingAddress.Street2,
      BillingAddress.City, BillingAddress.Region,
      BillingAddress.Country, BillingAddress.PostalCode);
  }
  public void CreateNewShippingAddress(string street1, string street2,
   string city, string region, string country, string postalCode)
  {
    ShippingAddress = new Address(
      street1,street2,
      city,region,
      country,postalCode)
  }
  public void CreateBillingInformation(string street1,string street2,
   string city,string region,string country, string postalCode,
   string creditcardNumber, string bankName)
  {
    BillingAddress = new Address      (street1,street2, city,region,country,postalCode );
    CreditCard = new CustomerCreditCard (bankName, creditcardNumber );
  }
  public void SetCustomerContactDetails
   (string email, string phone, string companyName)
  {
    EmailAddress = email;
    Phone = phone;
    CompanyName = companyName;
  }
  public string SalesPersonId { get; private set; }
  public CustomerStatus Status { get; private set; }
  public Address ShippingAddress { get; private set; }
  public Address BillingAddress { get; private set; }
  public CustomerCreditCard CreditCard { get; private set; }
}

 

모델에 로직이 포함된다고 해서 부르는 용어가 있을 줄은 추어도 몰랐다.

C# WPF 프로그램 개발할 때 Model에 비즈니스 로직을 넣어도 되냐는 질문은 여러번 했었다.

 

Rich Domain Model 이 정답은 아니다. Anemic Domain Model을 지지하는 사람도 있다.

하지만 아직까지는 Rich Domain Model이 좋아보인다.

 

참고: https://stackoverflow.com/questions/23314330/rich-vs-anemic-domain-model

 

Rich vs Anemic Domain Model

I am deciding if I should use a Rich Domain Model over an Anemic Domain Model, and looking for good examples of the two. I have been building web applications using an Anemic Domain Model, backed ...

stackoverflow.com

 

댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함