简体   繁体   English

在ASP.NET MVC应用程序中使用存储库

[英]Using repositories in a ASP.NET MVC Application

I'm building a web application using ASP.NET MVC and i would like use Repository pattern with Unit Of Work pattern, but i have some questions. 我正在使用ASP.NET MVC构建Web应用程序,并且我想将Repository模式与Unit Of Work模式结合使用,但是我有一些疑问。

First, Who should inherit the IDisposable interface? 首先,谁应该继承IDisposable接口? The repository interface, or the class that implements the repository. 存储库接口或实现存储库的类。 Ex: 例如:

Repository interface: 仓库接口:

public interface IRepository<T> : IDisposable where T : class
{
}

Repository: 仓库:

public class Repository<T> : IRepository<T> where T : class
{
}

Or 要么

Repository interface: 仓库接口:

public interface IRepository<T> : where T : class
{
}

Repository: 仓库:

public class Repository<T> : IDisposable , IRepository<T> where T : class
{
}

What is the difference between these two methods of creating repository. 这两种创建存储库的方法有何区别?

Who should inherit the IDisposable interface? 谁应该继承IDisposable接口? The interface or class that implements the repository? 实现存储库的接口或类?

I would use the IDisposable interface on the Repository class, not it's interface. 我会在Repository类上使用IDisposable接口,而不是它的接口。 From a code perspective, both would work, but disposing the Repository instance is part of the work the implementation of the class has to do. 从代码的角度来看,两者都可行,但是处理Repository实例是实现类所要做的工作的一部分。 Since IDisposable should be internal to the Repository Class, there is no need to call methods of this interface from other parts of the code. 由于IDisposable应该在存储库类的内部,因此无需从代码的其他部分调用此接口的方法。

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

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