0

私は現在、コントローラーがリポジトリーを使用して Entity Framework ORM を介してデータにアクセスする ASP.NET MVC アプリケーションに取り組んでいます。

以下は、ASP.NET MVC コントローラーがデータにアクセスするために使用するインターフェイスとリポジトリの基本的な例です。

GC Gen2 メモリに多数のリポジトリが残っているのを経験していますが、これは私の設計パターンの結果なのだろうか?

これに関するアドバイスをいただければ幸いです。アーキテクチャが改善される可能性があることは理解しており、そのようなコメントも歓迎されますが、私の主な焦点は、高いメモリ使用量です。

コントローラー

[SessionState(SessionStateBehavior.ReadOnly)]
public class GridCustomerServiceController : Controller
{
    private ICustomerServiceRepository _customerServiceRepository { get; set; }

    #region Constructor 

    public GridCustomerServiceController()
    {
        _customerServiceRepository = new CustomerServiceRepository();
    }

    #endregion Constructor

    #region Overrides
    protected override void Dispose(bool disposing)
    {
        this._customerServiceRepository.Dispose();

        base.Dispose(disposing);
    }
    #endregion Overrides

    [GridAction]
    [Authorize(Roles = "user")]
    public ActionResult _CustomerServicesSelect()
    {
            return View(new GridModel  
                {
                    Data =
                        (_customerServiceRepository.GetServicesByCustomerId(1))
                });

    }

インターフェイス

    using System.Linq;
    public interface ICustomerProductRepository
    {
        void Dispose();
        IQueryable<CustomerProduct> GetProductObjectsByCustomerId(int cid);
        void Add(Customer b);
        void Delete(Customer c);
        void Save();
    }

リポジトリ

    public class CustomerProductRepository : ICustomerProductRepository
    {
        private myEntities db = new myEntities();

          #region Dispose Methods

        ~CustomerProductRepository()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }

         #endregion Dispose Methods

        public void Delete(CustomerProduct c)
        {
            db.CustomerProducts.DeleteObject(c);
        }
        public void Save()
        {
            db.SaveChanges();
        }
        public void AddCustomerProduct(CustomerProduct b)
        {
            db.AddToCustomerProducts(b);
            db.SaveChanges();
        }
...
4

2 に答える 2

2

インターフェイスはインターフェイスから継承してメソッドを持つことができIDisposableますDispose。サンプル:

public class CustomerProductRepository : ICustomerProductRepository, IDisposable 
{
   // the same code here...
}

これにより、次の構文も使用できます。

using (ICustomerProductRepository repo = new CustomerProductRepository())
{
   // use repository here...

} // auto dispose occurs here
于 2013-08-02T16:51:03.777 に答える