2011-10-18 7 views
0

私はEntityフレームワークが初めてで、C#も比較的新しいです。私はリポジトリパターンでEntity Frameworkを使用しています。私は、DALプロジェクト、ビジネスレイヤープロジェクト、およびObjectDataSourceとともにaspxページを持つWebプロジェクトを持っています。今、私はすべてのエンティティに対して別々のリポジトリを作成しています。基本リポジトリを使用してすべての基本的なCRUD機能を処理したいと思います。コードサンプルで以下のようなすべてのエンティティのGenericエンティティDALクラスを作成し、それを汎用リポジトリで継承するが、オブジェクトデータを継承する方法は?ジェネリックスとオブジェクトデータを含むリポジトリパターンソース

1)ObjectDataSourceのTypeNameをジェネリック型にマップしますか? TypeNameと
ObjectDataTypeNameを割り当てますか?ビジネスレイヤジェネリッククラスは、汎用IDALEntityクラスを継承します。

<asp:ObjectDataSource ID="ODSCustomers" runat="server" 
     TypeName="SampleProject.BLL. " how do i access the Customer instance of BL 
     DataObjectTypeName="SampleProject.DAL. " how do i access the instance of 
               Customer entity from the generic DAL 
               class? 
     SelectMethod="GetCustomers" > 
     <SelectParameters> 
     <asp:SessionParameter Name="client_id" SessionField="ClientID" /> 
     </SelectParameters> 

2)どのように関連しているエンティティまたはナビゲーションプロパティは、この方法で処理しましたか?たとえば、CustomerエンティティやCustomer.CustomerAddress Entityなどの複数のエンティティの列を表示する場合は、DataFied = "Customer.CustomerAddress.City"のようなグリッド列をバインドしますか?

public class DALEntityRepository<T> : IDisposable, IDALEntityRepository<T> where T : class 
{ 
    private PFOEntities _context; 
    private ObjectSet<T> _objectSet; 

    public DALEntityRepository() 
    { 
     _context = new Entities(ConnectionStringHelper.GetConnectionString()); 
     _objectSet = (ObjectSet<T>)GetObjectSet(); 
    } 


    public void Insert(T entity) 
    { 
     _context.AddObject(_objectSet.EntitySet.Name, entity); 
     _context.SaveChanges(); 
    } 

    public void Update(T newVersion, T origVersion) 
    { 
     _objectSet.Attach(origVersion); 
     _context.ApplyCurrentValues(_objectSet.EntitySet.Name, newVersion); 
     _context.SaveChanges(); 
    } 

    public void Delete(T entity) 
    { 
     _context.AttachTo(_objectSet.EntitySet.Name, entity); 
     _objectSet.DeleteObject(entity); 
     _context.SaveChanges(); 
    } 

    public IQueryable<T> GetEntities() 
    { 
     return _objectSet; 
    } 

    public IQueryable<T> GetEntitiesByClientId(int clientId) 
    { 
     Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)GetPredicate(clientId); 
     return GetEntities().Where(predicate); 
    } 


    private object GetPredicate(int clientId) 
    { 
     object retVal = null; 
     Type type = GetType(); 

     //Use similar if's to check for Different Entities 
     if (type == typeof(DataEntityRepository<Customers>)) 
     { 
      Expression<Func<Customers, bool>> predicate = (c) => c.client_id ==  
      clientId; 
      retVal = predicate; 
     } 

       return retVal; 
    } 

    private object GetObjectSet() 
    { 
     object retVal = null; 
     Type type = GetType(); 

     if(type == typeof(DataEntityRepository<Customers>)) 
     { 
      retVal = _context.Customers; 
     } 
       return retVal; 
    } 

私が明確に説明されているか、ご質問ありがとうございましたか?あなたの最初の質問については

+0

あなたは、通常は分離コードではなくビューでデータバインディングを行うリポジトリパターンで、ビューを覚えています(シンプルなロジックは大丈夫です)、コードビハインドは、データバインドのようなビューのロジックをほとんどの人が入れ、データを取得するためだけにデータアクセスを使用する場所です。 – Joakim

答えて

0

、を参照してください。あなたの2番目の質問についてはhttp://www.manuelabadia.com/blog/PermaLink,guid,91a1d00f-197e-4148-b4e1-cea324029dc6.aspx

Using generic classes with ObjectDataSourceまたは溶液のようなよりASP.NET

はい、あなたはナビゲーションに従うことによって、関連するエンティティを参照することができますプロパティがキャッチです。ナビゲーションプロパティがロードされていないので、出力にナビゲーションプロパティを含めないでください(それらの上をナビゲートする、それらを選択する、またはIncludeステートメントを使用する)、遅延読み込みが無効になっていると、NullReferenceExceptionが発生します。

私の推薦は、強制的に、あなたのクエリでナビゲーションプロパティを含めるを確認することです:ダムとだけ表示データでなければなりませんhttp://msdn.microsoft.com/en-us/library/bb738708.aspx

関連する問題