简体   繁体   中英

How to correctly implement an interface on a C# repository

I am currently working on an ASP.NET MVC application in which controllers use repositories to access data via the Entity Framework ORM.

Below is a basic example of interfaces and repositories used by my ASP.NET MVC controllers to access data.

I am experiencing high numbers of my repositories being left in GC Gen2 memory and I wondered if it was as a result of my design pattern?

Any advice on this would be appreciated. I understand that the architecture could be improved and such comments would also be appreciated but my main focus surrounds my high memory usage.

The Controller

[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))
                });

    }

The Interface

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

The Repository

    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();
        }
...

Your interface could inherit from the IDisposable interface to have the Dispose method. For sample:

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

With this, you also could use the following syntax:

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

} // auto dispose occurs here

As Felipe mentioned, The key here is to use IDisposable. GC can invoke the IDisposable.Dispose automtically when it runs, so you need not worry. You can also refer here: 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM