0

次のリポジトリ パターンがあります。要件は、「所有者の名前が Lijo であるアカウントをすべて検索する」ことです。そのため、FindAll 関数を作成する必要があります。この関数の書き方

制約は次のとおりです。

1) クライアント「BankAccountService」は「DBML_Project」のクラスを使用しないでください。

2) GetAll メソッドを使用してアカウントの完全なリストを取得してからフィルター処理を行うべきではありません。

注:ポリモーフィズム: ORM エンティティはドメイン エンティティまたはデータ エンティティですか?という質問に取り組んでいるときに、この問題に直面しました。

コード

namespace ApplicationService_Bank
{
public class BankAccountService
{
    RepositoryLayer.ILijosBankRepository accountRepository = new RepositoryLayer.LijosSimpleBankRepository();

    public void FreezeAllAccountsForUser(string userName)
    {
        //Should not use assembly 'DBML_Project'.

        IEnumerable<DomainEntitiesForBank.IBankAccount> accountsForUserWithNameLIJO = null;
        //accountsForUserWithNameLIJO = accountRepository.FindAll(p => p.BankUser.Name == "Lijo");
    }

}

}


namespace RepositoryLayer
{
public interface ILijosBankRepository
{
    List<DomainEntitiesForBank.IBankAccount> GetAll();
    IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate);
    void SubmitChanges();
        }

public class LijosSimpleBankRepository : ILijosBankRepository
{

    private IBankAccountFactory bankFactory = new MySimpleBankAccountFactory();
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }

    public virtual List<DomainEntitiesForBank.IBankAccount> GetAll()
    {

        List<DBML_Project.BankAccount> allItems = Context.GetTable<DBML_Project.BankAccount>().ToList();
        List<DomainEntitiesForBank.IBankAccount> bankAccounts = new List<DomainEntitiesForBank.IBankAccount>();
        foreach (DBML_Project.BankAccount acc in allItems)
        {
            DomainEntitiesForBank.IBankAccount theAccount = bankFactory.CreateAccount(acc.AccountType, acc.BankAccountID, acc.Status, acc.OpenedDate, acc.AccountOwnerID);
            bankAccounts.Add(theAccount);
        }
        return bankAccounts;
    }


    public IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate)
    {
        //Where
        var results = Context.GetTable<DBML_Project.BankAccount>().Where(predicate);
        return results;
    }

    public virtual void SubmitChanges()
    {
        Context.SubmitChanges();
    }

}

}

読む:

  1. IEnumerable<T> と IQueryable<T> を返す

  2. 後で別のORMに簡単に切り替えられるようにリポジトリパターンを設計する方法は?

4

1 に答える 1

3

簡単なアプローチは、クエリを手動で作成することです。

public class SearchCriteria
{
    public string Name { get; set; }
    // ...more
}

public IEnumerable<Entity> FindAll(SearchCriteria criteria)
{
    IQueryable<Entity> entities = _datasource.Entities; // replace with your L2S equivalent

    if (criteria.Name != null)
        entities = entities.Where(e => e.Name == criteria.Name);

    // ...more

    return entities;
}

生成されたオブジェクトを直接返したくない場合は、返す前に他の何かにマップしてください。

return Map(entities); // IEnumerable<CustomObject> Map(IEnumerable<Entity> entities)
于 2012-06-29T12:30:45.267 に答える