I had code that violated the Single Responsibility Principle in a question on Stack Overflow.
In order to overcome that problem, I changed the code as follows.
- Is this a good practice in order to overcome the problem?
- Is this a pattern?
- Is there a better way?
- Does it satisfy the Unit Of Work pattern using LINQ-to-SQL?
namespace DomainObjectsForBank
{
public interface IBankAccount
{
int BankAccountID { get; set; }
string AccountStatus { get; set; }
void FreezeAccount();
RepositoryLayer.IRepository<RepositoryLayer.BankAccount> AccountRepository { get; set; }
}
public class FixedBankAccount : IBankAccount
{
public int BankAccountID { get; set; }
public string AccountStatus { get; set; }
public void FreezeAccount()
{
//ChangeAccountStatus();
AccountStatus = "Frozen";
}
//private void ChangeAccountStatus()
//{
// AccountStatus = "Frozen";
// RepositoryLayer.BankAccount repositoryBankAccEntity = new RepositoryLayer.BankAccount();
// repositoryBankAccEntity.BankAccountID = this.BankAccountID;
// accountRepository.UpdateChangesByAttach(repositoryBankAccEntity);
// repositoryBankAccEntity.Status = "Frozen";
// accountRepository.SubmitChanges();
//}
private RepositoryLayer.IRepository<RepositoryLayer.BankAccount> accountRepository;
public RepositoryLayer.IRepository<RepositoryLayer.BankAccount> AccountRepository
{
get
{
return accountRepository;
}
set
{
accountRepository = value;
}
}
}
}
using System.Collections.Generic;
using System;
namespace ApplicationServiceForBank
{
public class BankAccountService
{
RepositoryLayer.IRepository<RepositoryLayer.BankAccount> accountRepository;
ApplicationServiceForBank.IBankAccountFactory bankFactory;
public BankAccountService(RepositoryLayer.IRepository<RepositoryLayer.BankAccount> repo, IBankAccountFactory bankFact)
{
accountRepository = repo;
bankFactory = bankFact;
}
public void FreezeAllAccountsForUser(int userId)
{
IEnumerable<RepositoryLayer.BankAccount> accountsForUser = accountRepository.FindAll(p => p.BankUser.UserID == userId);
foreach (RepositoryLayer.BankAccount oneOfRepositroyAccounts in accountsForUser)
{
DomainObjectsForBank.IBankAccount domainBankAccountObj = bankFactory.CreateAccount(oneOfRepositroyAccounts);
if (domainBankAccountObj != null)
{
domainBankAccountObj.BankAccountID = oneOfRepositroyAccounts.BankAccountID;
domainBankAccountObj.FreezeAccount();
this.accountRepository.UpdateChangesByAttach(oneOfRepositroyAccounts);
oneOfRepositroyAccounts.Status = domainBankAccountObj.AccountStatus;
this.accountRepository.SubmitChanges();
}
}
}
}
public interface IBankAccountFactory
{
DomainObjectsForBank.IBankAccount CreateAccount(RepositoryLayer.BankAccount repositroyAccount);
}
public class MySimpleBankAccountFactory : IBankAccountFactory
{
//Is it correct to accept repository inside factory?
public DomainObjectsForBank.IBankAccount CreateAccount(RepositoryLayer.BankAccount repositroyAccount)
{
DomainObjectsForBank.IBankAccount acc = null;
if (String.Equals(repositroyAccount.AccountType, "Fixed"))
{
acc = new DomainObjectsForBank.FixedBankAccount();
}
if (String.Equals(repositroyAccount.AccountType, "Savings"))
{
//acc = new DomainObjectsForBank.SavingsBankAccount();
}
return acc;
}
}
}