DDD Approach to Access External Information

前端 未结 4 1258
遇见更好的自我
遇见更好的自我 2020-11-30 11:58

I have an existing bank application classes as shown below. The banks account can be of SavingsBankAccount or FixedBankAccount. There is an operation called IssueLumpSumInte

相关标签:
4条回答
  • 2020-11-30 12:43

    From reading your requirement, here is how I would do it:

    //Application Service - consumed by UI
    public class AccountService : IAccountService
    {
        private readonly IAccountRepository _accountRepository;
        private readonly ICustomerRepository _customerRepository;
    
        public ApplicationService(IAccountRepository accountRepository, ICustomerRepository customerRepository)
        {
            _accountRepository = accountRepository;
            _customerRepository = customerRepository;
        }
    
        public void IssueLumpSumInterestToAccount(Guid accountId)
        {
            using (IUnitOfWork unitOfWork = UnitOfWorkFactory.Create())
            {
                Account account = _accountRepository.GetById(accountId);
                Customer customer = _customerRepository.GetById(account.CustomerId);
    
                account.IssueLumpSumOfInterest(customer);
    
                _accountRepository.Save(account);
            }
        }
    }
    
    public class Customer
    {
        private List<Guid> _accountIds;
    
        public IEnumerable<Guid> AccountIds
        {
            get { return _accountIds.AsReadOnly();}
        }
    }
    
    public abstract class Account
    {
        public abstract void IssueLumpSumOfInterest(Customer customer);
    }
    
    public class FixedAccount : Account
    {
        public override void  IssueLumpSumOfInterest(Customer customer)
        {
            if (customer.AccountIds.Any(id => id != this._accountId))
                throw new Exception("Lump Sum cannot be issued to fixed accounts where the customer has other accounts");
    
            //Code to issue interest here
        }
    }   
    
    public class SavingsAccount : Account
    {
        public override void  IssueLumpSumOfInterest(Customer customer)
        {
            //Code to issue interest here
        }
    }
    
    1. The IssueLumpSumOfInterest method on the Account aggregate requires the Customer aggregate to help decide whether interest should be issued.
    2. The customer aggregate contains a list of account IDs - NOT a list of account aggregates.
    3. The base class 'Account' has a polymorphic method - the FixedAccount checks that the customer doesn't have any other accounts - the SavingsAccount doesn't do this check.
    0 讨论(0)
  • 2020-11-30 12:49

    The first thing I noticed was the improper use of the bank account factory. The factory, pretty much as you have it, should be used by the repository to create the instance based on the data retrieved from the data store. As such, your call to accountRepository.FindByID will return either a FixedBankAccount or SavingsBankAccount object depending on the AccountType returned from the data store.

    If the interest only applies to FixedBankAccount instances, then you can perform a type check to ensure you are working with the correct account type.

    public void IssueLumpSumInterest(int accountId)
    {
        var account = _accountRepository.FindById(accountId) as FixedBankAccount;
    
        if (account == null)
        {
            throw new InvalidOperationException("Cannot add interest to Savings account.");
        }
    
        var ownerId = account.OwnerId;
    
        if (_accountRepository.Any(a => (a.BankUser.UserId == ownerId) && (a.AccountId != accountId)))
        {
            throw new InvalidOperationException("Cannot add interest when user own multiple accounts.");
        }
    
        account.AddInterest();
    
        // Persist the changes
    }
    

    NOTE: FindById should only accept the ID parameter and not a lambda/Func. You've indicated by the name "FindById" how the search will be performed. The fact that the 'accountId' value is compared to the BankAccountId property is an implementation detail hidden within the method. Name the method "FindBy" if you want a generic approach that uses a lambda.

    I would also NOT put AddInterest on the IBankAccount interface if all implementations do not support that behavior. Consider a separate IInterestEarningBankAccount interface that exposes the AddInterest method. I would also consider using that interface instead of FixedBankAccount in the above code to make the code easier to maintain and extend should you add another account type in the future that supports this behavior.

    0 讨论(0)
  • 2020-11-30 12:52

    2 min scan answer..

    • Not sure why there is a need for 2 representations of a BankAccount RepositoryLayer.BankAccount and DomainObjectsForBank.IBankAccount. Hide the persistence layer coupled one.. deal with just the domain object in the service.
    • Do not pass/return Nulls - I think is good advice.
    • The finder methods look like the LINQ methods which select items from a list of collection. Your methods look like they want to get the first match and exit..in which case your parameters can be simple primitives (Ids) vs lambdas.

    The general idea seems right. The service encapsulates the logic for this transaction - not the domain objects. If this changes, only one place to update.

    public void IssueLumpSumInterest(int acccountID)
    {
        var customerId = accountRepository.GetAccount(accountId).CustomerId;
    
        var accounts = accountRepository.GetAccountsForCustomer(customerId);
        if ((accounts.First() is FixedAccount) && accounts.Count() == 1)
        {
           // update interest    
        }
    }
    
    0 讨论(0)
  • 2020-11-30 12:55

    Things that strike me as weird:

    • Your IBankAccount has a method FreezeAccount, but I presume that all accounts would have quite similar behavior? Perhaps a BankAccount class is warranted that implements some of the interface?
    • AccountStatus should probably be an enum? What should happen if an account is "Forzen"?
    0 讨论(0)
提交回复
热议问题