原文: Applied Domain-Driven Design (DDD), Part 5 - Domain Service
Domain Service is not be confused with Application Service or Web Service. Domain Service lives in the Domain Model Layer. Unlike Application or Web Service, Domain Service should not be called each time to access Domain Model Layer. You can call your repository interface in the Application Layer and get the Domain Entity directly.
不要将领域服务与应用服务, Web Service 弄混淆了. 领域服务位于领域模型层. 与应用服务或者 Web Service 不同, 不应该每次都通过调用领域服务去访问领域模型层. 你可以在应用层中调用数据仓储接口, 直接获取领域实体.
Your business is responsible for collecting and paying Value Added Tax (VAT) based off your business location, your customer‘s location, and the type of product you are selling.
你的企业通过 营业地点, 客户所在的地点 以及 要销售的产品类型 来负责收取和支付增值税 (VAT).
public class TaxDomainService : ITaxDomainService
{
readonly IRepository<ProductTax> productTax; /** 产品类型数款. */
readonly IRepository<CountryTax> countryTax; /** 客户所在的地点税款. */
readonly Settings settings; /** 营业地点税款. */
public TaxDomainService(Settings settings,
IRepository<ProductTax> productTax,
IRepository<CountryTax> countryTax)
{
this.productTax = productTax;
this.countryTax = countryTax;
this.settings = settings;
}
public decimal Calculate(Customer customer, Product product)
{
CountryTax customerCountryTax = this.countryTax.FindById(customer.Country.Id);
CountryTax businessCountryTax = this.countryTax.FindById(settings.BusinessCountry.Id);
ProductTax productTax = this.productTax.FindById(product.Code.Id);
return (product.Cost * customerCountryTax.Percentage) +
(product.Cost * businessCountryTax.Percentage) +
(product.Cost * productTax.Percentage);
}
}
Customer customer = this.repositoryCustomer.FindById(customerId);
Product product = this.repositoryProduct.FindById(productId);
decimal tax = this.taxDomainService.Calculate(customer, product);
customer.Cart.Add(CartProduct.Create(customer.Cart, product, productQuantity, tax));
- Domain Service allows you to capture logic that doesn‘t belong in the Domain Entity.
- Domain Service allows you to orchestrate between different Domain Entities.
- Don‘t create too many Domain Services, most of the logic should reside in the domain entities, event handlers, etc.
- It‘s a great place for calculation and validation as it can access entities, and other kind of objects (e.g. Settings) that are not available via the entity graph.
Settings
.
- Methods should return primitive types, custom enums are fine too.
*Note: Code in this article is not production ready and is used for prototyping purposes only. If you have suggestions or feedback please do comment.
*注意: 本文中的代码尚未准备好投入生产, 仅用于原型设计. 如果有建议和反馈, 请发表评论.
原文:https://www.cnblogs.com/xixixiao/p/applied-domain-driven-design-ddd-part-5.html