简体   繁体   English

如何在C#存储库上正确实现接口

[英]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. 我目前正在研究ASP.NET MVC应用程序,其中的控制器使用存储库通过实体框架ORM访问数据。

Below is a basic example of interfaces and repositories used by my ASP.NET MVC controllers to access data. 下面是我的ASP.NET MVC控制器用来访问数据的接口和存储库的基本示例。

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? 我正在将大量存储库留在GC Gen2内存中,我想知道这是否是我的设计模式的结果?

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. 您的接口可以继承自IDisposable接口以具有Dispose方法。 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. 正如Felipe提到的,这里的关键是使用IDisposable。 GC can invoke the IDisposable.Dispose automtically when it runs, so you need not worry. GC可以在运行时自动调用IDisposable.Dispose,因此您不必担心。 You can also refer here: 1 您也可以在这里参考: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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