2011-07-27 12 views
0

私は、システム全体で使用されるビジネスオブジェクト(つまりアカウント)を使用してライブラリを作成したエンティティフレームワークを使用しています。彼らは要求されたかEntity Framework - アダプタの照会

public interface EntityAdapter<T> { 
    T Materialize(long id); 

    long Dematerialize(T business); 
    void Dispose(T business); 
} 

public abstract class EFEntityAdapter<T> : EntityAdapter<T> { 
    private static MyModel.MyEntities __ctx = null; 
    protected MyModel.MyEntities _context 
    { 
     get 
     { 
      if (__ctx == null) 
      { 
       __ctx = new MyModel.MyEntities(); 
      } 
      return __ctx; 
     } 
    } 

    public abstract T Materialize(long id); 
    public abstract long Dematerialize(T business); 
    public abstract void Dispose(T business); 
} 
public class AccountEntityAdapter : EFEntityAdapter<CommonLib.BusinessModels.Account> 
{ 
    public override CommonLib.BusinessModels.Account Materialize(long id) 
    { 
     Account entity = (from account in _context.Accounts 
          where account.AccountId == id 
          select account).FirstOrDefault(); 

     if (entity == null) 
      return null; 

     CommonLib.BusinessModels.Account business = new CommonLib.BusinessModels.Account(); 

     business.AccountId = entity.AccountId; 
     business.AccountText = entity.AccountText; 

     return business; 
    } 

    public override long Dematerialize(CommonLib.BusinessModels.Account business) 
    { 
     long id = business.AccountId; 

     Account entity = (from account in _context.Accounts 
          where account.AccountId == id 
          select account).FirstOrDefault(); 

     if (entity == null) 
     { 
      if (id > 0) 
      { 
       throw new Exception("Account with id: " + id + " does not exists"); 
      } 
      else 
      { 
       entity = new Account(); 
       _context.Accounts.AddObject(entity); 
      } 
     } 

     entity.AccountId = business.AccountId; 
     entity.AccountText = business.AccountText; 
     _context.SaveChanges(); 
     business.AccountId = entity.AccountId; 
     return entity.AccountId; 
    } 

    public override void Dispose(CommonLib.BusinessModels.Account business) 
    { 
     long id = business.AccountId; 

     Account entity = (from account in _context.Accounts 
          where account.AccountId == id 
          select account).FirstOrDefault(); 

     if (entity == null) 
     { 
      throw new Exception("Account with id: " + id + " was not found, but an attempt to delete it was done"); 
     } 

     _context.DeleteObject(entity); 
     _context.SaveChanges(); 
    } 
} 

を保存するために必要なしかし、今、私は私がすることができるよようにLINQとアダプタを使用したいと考えているとき、エンティティフレームワークは、これらの前後に変換され

public class Account 
{ 
    public long AccountId { get; set; } 
    public string AccountText { get; set; } 
} 

...私は、エンティティコンテキストの自由だと

AccountEntityAdapter a = new AccountEntityAdapter(); 
List<Commonlib.BusinessModels.Account> list = (from account in a 
               where account.AccountId > 6 
               select account).ToList(); 

このようなような何かを

どうしたらいいですか?

+0

あなたは車輪を再発明しています。ビジネスオブジェクトとして、エンティティを放棄して使用します。そのような広範な複雑さのポイントは何ですか? –

+0

私が知る限り、エンティティオブジェクトをアプリケーションドメインから削除することはできません。情報を別の場所に転送してから、いくつかの変更を加えて返す場合は、他のオブジェクトを使用する必要がありますか? –

+0

これは当てはまりますが、クエリはDTOではなく元のオブジェクトで実行する必要があります。 –

答えて

1

はどちらかである:

  • は、LINQツーエンティティクエリで直接投影を行う代わりに、マッパー
  • クエリあなたのエンティティを使用して、.ToListを呼び出し、LINQツーを実行しますマッパーを使って投影した結果のオブジェクトクエリ
0

データベースに接続するにはコンテキストが必要です。しかし、通常、これはリポジトリにカプセル化され、アクションごとに1つのコンテキストを作成するのではなく、使用のために中央の場所に格納されます。あなたのコメントあなたのためのオプションに基づいて

+0

私は知っていますが、EntityAdapterを使用している時点で、エンティティオブジェクトの代わりにビジネスオブジェクトを返すことを望みます。文脈はそこにありますが、私は変換が自動的に起こるようにしたい –

関連する問題