データ アクセスに C# / MVC 4 と Entity Framework 5 を使用して、新しい Web プロジェクトを開始しています。プロジェクトの構造に n 層のアプローチを採用することにしました。設計に関する決定についてフィードバックをお願いします。
ソリューションの構造は次のとおりです。
Project.Model (クラス ライブラリ): EF .edmx、エンティティ モデル、およびビュー モデルが含まれています
Project.DAL (クラス ライブラリ): EF DbContext およびリポジトリ クラスが含まれています
Project.BLL (クラス ライブラリ): ビジネス ロジック クラスが含まれています。
プロジェクト (MVC プロジェクト)
ダル
データ アクセス レイヤーは、単純な CRUD のような操作にのみ関係します。私はリポジトリアプローチを採用することにしました。リポジトリ インターフェイスは次のとおりです。
public interface IRepository
{
}
public interface IRepository<T> : IRepository, IDisposable 
    where T : class, new()
{
    T Add(T item);
    T Get(object id);
    T Get(Expression<Func<T, bool>> predicate);
    IQueryable<T> GetAll();
    IQueryable<T> GetAll(Expression<Func<T, bool>> predicate);
    void Update(T item);
    void Delete(T item);
}
Web プロジェクトでの Entity Framework の使用について調査した結果、一般的なコンセンサスは、要求ごとに DbContext/ObjectContext を 1 つだけにする必要があるということです。そこで、リクエストごとに 1 つのコンテキストを作成して破棄するために、DbContext を HttpContext に挿入する HttpModule を作成しました。
public class DbContextModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
        context.EndRequest += context_EndRequest; 
    }
    public void Dispose()
    {
    }
    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext httpContext = application.Context;
        httpContext.Items.Add(Repository.ContextKey, new ProjectEntities());
    }
    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext httpContext = application.Context;
        var entities = (ProjectEntities)httpContext.Items[Repository.ContextKey];
        entities.Dispose();
        entities = null;
        application.Context.Items.Remove(Repository.ContextKey);
    }
}
次はリポジトリ基本クラスです。コンストラクターは、上記の HttpModule から挿入された DbContext を利用することに注意してください。
public abstract class Repository<T> : IRepository<T> where T : class, new()
{
    protected Repository()
    {
        if (HttpContext.Current == null)
        {
            throw new Exception("Cannot create repository - current HttpContext is null.");
        }
        _entities = (ProjectEntities)HttpContext.Current.Items[Repository.ContextKey];
        if (_entities == null)
        {
            throw new Exception("Cannot create repository - no DBContext in the current HttpContext.");
        }
    }
    private ProjectEntities _entities;
    public T Add(T item)
    {
        _entities.Set<T>().Add(item);
        _entities.SaveChanges();
        return item;
    }
    public T Get(object id)
    {
        return _entities.Set<T>().Find(id);
    }
    public T Get(Expression<Func<T, bool>> predicate)
    {
        return _entities.Set<T>().AsQueryable().FirstOrDefault(predicate);
    }
    public IQueryable<T> GetAll()
    {
        return _entities.Set<T>().AsQueryable();
    }
    public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate)
    {
        return _entities.Set<T>().AsQueryable().Where(predicate);
    }
    public void Update(T item)
    {
        _entities.Entry(item).State = EntityState.Modified;
        _entities.SaveChanges();
    }
    public void Delete(T item)
    {
        _entities.Set<T>().Remove(item);
        _entities.SaveChanges();
    }
}
そして、実装の簡単な例...
public class AdminRepository : Repository<Admin>
{
    public Admin GetByEmail(string email)
    {
        return Get(x => x.Email == email);
    }
}
BLL
ビジネス ロジック層は、すべてのビジネス ロジックをカプセル化します。制約を維持するために、次のような基本「ロジック」クラスを作成しました。
public abstract class Logic<TRepository> where TRepository : class, IRepository, new()
{
    private static Expression<Func<TRepository>> _x = () => new TRepository();
    private static Func<TRepository> _compiled = _x.Compile(); 
    protected Logic()
    {
        Repository = _compiled();
    }
    protected internal TRepository Repository { get; private set; }
}
コンストラクターは必要な Repository クラスを自動的に作成するため、リポジトリをインスタンス化するために子クラスに追加のコードは必要ありません。これは実装の簡単な例です
public class AdminLogic : Logic<AdminRepository>
{
    public Admin Add(Admin admin)
    {
        return Repository.Add(admin);
    }
    public Admin Get(object id)
    {
        return Repository.Get(id);
    }
    public Admin GetByEmail(string email)
    {
        return Repository.GetByEmail(email);
    }
    public IQueryable<Admin> GetAll()
    {
        return Repository.GetAll();
    }
    public void Update(Admin admin)
    {
        Repository.Update(admin);
    }
}
この例は、DAL リポジトリのパススルーに近いものですが、後でビジネス ロジックを追加しても問題ありません。遅延実行のために IQueryable を必要とするいくつかのサード パーティ ツールを使用しているため、BLL から IQueryable を返すことを選択しています。
プロジェクト (MVC プロジェクト)
最後に、単純なコントローラー アクションは次のようになります。
public ActionResult Index(int? page)
{
    // Instantiate logic object
    AdminLogic logic = new AdminLogic();
    // Call GetAll() and use AutoMapper to project the results to the viewmodel
    IQueryable<AdminModel> admins = logic.GetAll().Project().To<AdminModel>();
    // Paging (using PagedList https://github.com/TroyGoode/PagedList)
    IPagedList<AdminModel> paged = admins.ToPagedList(page ?? 1, 25);
    return View(paged);
}
すべてが期待どおりに機能し、テストでは EF コンテキストが適切に破棄され、全体的な速度が良好であることが示されています。
これはこれを行うためのかなり良い方法ですか?
お時間をいただきありがとうございます。