如何通过领域驱动设计来实现服务?
在遵循领域驱动设计时,实际实现服务类的最佳方法是什么?
例如,假设我想要一个可用于将资金从一个帐户转移到另一个帐户的 AccountService
?以下哪一项(如果有)是实现这一目标的最佳方法?
public class AccountService1
{
private IAccountRepository _accountRepo;
public AccountService1(IAccountRepository accountRepo)
{
_accountRepo = accountRepo;
}
public void TransferFunds(double ammount, int sourceAccountNumber, int targetAccountNumber)
{
//FUNDS TRANSFER CODE
}
}
public class AccountService2
{
public void TransferFunds(double ammount, Account sourceAccount, Account targetAccount)
{
//FUNDS TRANSFER CODE
}
}
public static class AccountService3
{
public static void TransferFunds(double amount, Account sourceAccount, Account targetAccount)
{
//FUNDS TRANSFER CODE
}
}
public static class AccountService4
{
public static void TransferFunds(double amount, int sourceAccountNumber, int targetAccountNumber, IAccountRepository repository)
{
//FUNDS TRANSFER CODE
}
}
What is the best way to actually implement service classes when following domain driven design?
For example say I want to have an AccountService
which can be used to transfer funds from one account to another? Which of the following, if any, would be the best way to implement this?
public class AccountService1
{
private IAccountRepository _accountRepo;
public AccountService1(IAccountRepository accountRepo)
{
_accountRepo = accountRepo;
}
public void TransferFunds(double ammount, int sourceAccountNumber, int targetAccountNumber)
{
//FUNDS TRANSFER CODE
}
}
public class AccountService2
{
public void TransferFunds(double ammount, Account sourceAccount, Account targetAccount)
{
//FUNDS TRANSFER CODE
}
}
public static class AccountService3
{
public static void TransferFunds(double amount, Account sourceAccount, Account targetAccount)
{
//FUNDS TRANSFER CODE
}
}
public static class AccountService4
{
public static void TransferFunds(double amount, int sourceAccountNumber, int targetAccountNumber, IAccountRepository repository)
{
//FUNDS TRANSFER CODE
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你列出的每一条路都会通向贫血领域。
是否有某种原因它不能成为您的帐户实体上的实例方法?
Every way you listed leads down the road to an anemic domain.
Is there some reason it can't be an instance method on your account entity?
尚不清楚您的示例是应用程序服务还是域服务。示例 1 是应用程序服务的样子,示例 2 是域服务的样子。放在一起,它看起来像这样:
It's not clear if your examples are Application Services or Domain services. Example 1 is what the Application service should look like, and example 2 is what a Domain service might look like. Put together, it would look something like this:
您的第一个实施是最好的选择。但是,您还应该在 Account 类本身上实现 Transfer 方法,该方法也接受金额和目标 Account 实例。在这种情况下,服务的作用是使用存储库加载帐户实例,保存它们,工作单元管理,以及提供对可能需要的任何外部服务的访问。 Account 类上的 Transfer 方法的作用是保护 Account 类本身内数据的完整性并确保一致性、抛出异常等。
因此,Account 类定义了可能的操作,而服务层是对域进行操作的层,因此涉及所有操作职责。
Your first implementation is the best option. However, you should also implement a Transfer method on the Account class itself which accepts an amount and a target Account instance as well. The role of the service in this case is to load Account instances using the repository, save them, unit of work management, as well as providing access to any external services that may be needed. The role of the Transfer method on the Account class is to protect the integrity of the data within the Account class itself and ensure consistency, throw exceptions etc.
So the Account class defines what operations are possible and the service layer is what operates upon the domain layer and is thus concerned with all operational responsibilities.