使用依赖注入(Autofac)在 MVC 应用程序中实现仓库模式






4.79/5 (8投票s)
在本文中,我将解释如何在 MVC 应用程序中实现存储库模式以及如何启用依赖注入功能。
引言
本文讨论了在 MVC 应用程序中使用 Autofac 实现存储库模式,并展示了如何将其与 Autofac 结合使用。
每个应用程序都需要访问一个或另一个数据源,因此,最佳方法是使用存储库模式在主应用程序中编写数据访问代码,该模式充当业务逻辑层和数据访问层之间的层。换句话说,存储库将所有数据访问代码与应用程序的其余部分隔离开来。
在图 A 中
正如你所见,所有控制器方法都直接与 EntityFramework 交互,而 EntityFramework 又与数据库交互。
在这种方法中,
1. 代码会有更多重复
例如:如果应用程序正在从两个不同的控制器修改一个实体,那么这两个控制器方法中将会有相同的代码重复。
2. 未来修改
例如:如果数据库操作将来需要修改。我们需要在所有其他地方修改代码,无论相同的操作在哪里发生。
3. 设计不当
将业务逻辑与数据访问结合起来不是一个好方法。
在图 B 中。
控制器方法与客户存储库交互,因此,没有任何控制器方法会直接与 EntityFramework 数据上下文直接交互。
在这种方法中,
1. 单点更改
例如:如果将来有任何更改,您只需在一个地方进行更改,即存储库,因为所有业务逻辑将根据要求编写在相应的方法中。
先决条件
1. Visual Studio 2013
2. NorthwindDatabase,如果您没有,请从 (http://northwinddatabase.codeplex.com/releases/view/71634) 下载 .bak 文件并还原它。观看此视频了解如何还原 (https://www.youtube.com/watch?v=lAvU0QT-OT0)。
3. SQL Server: 2012。
4. MVC-5
5. Autofac 库 (通过 Nuget 在线安装)
6. Autofac Integration MVC 库 (通过 Nuget 在线安装)
7. Entity Framework 库,因为我们将使用存储库模式存储数据 (通过 Nuget 在线安装)
使用代码
让我们先创建项目结构
我在解决方案中有两个文件夹
1. Data Access Layer (数据访问层)
2. Web (Web)
Data Access Layer - 在这里有三个项目
Web - 这是 MVC 项目
让我们来看看 Data Access Layer 文件夹。它下面有 3 个项目,它们都是类库类型。
1. Repository_Database - 这个项目将包含我们的 Entity Framework。
2. Repository_RepoClasses - 这个项目将是我们的存储库类,包含与数据库交互的逻辑。
3. Repository_RepoInterfaces - 这个项目将包含 Repository_RepoClasses 项目中提到的存储库类的接口。
在此之前,请确保您已还原 NORTHWIND 数据库。
Repository_Database - 右键单击 -> 添加 -> 新建项 -> 在左侧单击 Data -> 选择 ADO.NET 实体数据模型 -> 选择 从数据库生成 () -> 然后选择您还原的 northwind 数据库()
从单选按钮选项中,选择“是”以在连接字符串中包含路径。
您可以为整个数据库创建 edmx,也可以只为我们需要的表创建 edmx。在本例中,我们将使用客户表,但会为整个 northwind 数据库创建 edmx。
如果您只选择客户表,您的 edmx 将如下所示。
为 Northwind_DBEntities 类 (这是在 .context.tt 下自动生成的) 创建接口,方法是右键单击 -> 选择重构 -> 提取接口。
重写 DbContext 的 Save 方法。
第二个项目
RepositoryPattern_RepoClasses
添加一个类并命名为 CustomerRepository.cs
public class CustomerRepository : ICustomerRepository
{
INorthwind_DBEntities NorthwindContext;
public CustomerRepository(INorthwind_DBEntities db)
{
NorthwindContext = db;
}
public IEnumerable<Customer> SelectAll()
{
return NorthwindContext.Customers.ToList();
}
public Customer SelectByID(string id)
{
return NorthwindContext.Customers.Find(id);
}
public void Insert(Customer obj)
{
NorthwindContext.Customers.Add(obj);
}
public void Delete(string id)
{
var value = NorthwindContext.Customers.Where(i => i.CustomerID == id).FirstOrDefault();
NorthwindContext.Customers.Remove(value);
}
public void Save()
{
NorthwindContext.SaveChanges();
}
}
}
第三个项目
RepositoryPattern_RepoInterfaces
在此,我们将为第二个项目中构建的所有类创建接口。
public interface ICustomerRepository
{
IEnumerable<Customer> SelectAll();
Customer SelectByID(string id);
void Insert(Customer obj);
void Delete(string id);
void Save();
}
让我们来看看 Web 文件夹。
右键单击 -> 选择添加新项目 -> 从左侧窗格选择 Web -> 选择 ASP.NET 应用程序,为其命名 ->
然后选中 MVC 并选择 Empty,如下图所示。
它将为您创建一个 mvc 应用程序。
在该项目中,通过 nuget 添加 autofac 和 entityframework dll,如下图所示。
通过右键单击 -> 添加 MVC 控制器,向控制器文件夹添加一个控制器。
然后为其创建一个视图。
并在您的控制器中编写此代码。
public class CustomerController : Controller
{
ICustomerRepository _CustomerRepo;
public CustomerController(ICustomerRepository customerRepo)
{
_CustomerRepo = customerRepo;
}
//
// GET: /Customer/
public ActionResult Index()
{
List<Customer> model = (List<Customer>)_CustomerRepo.SelectAll();
return View(model);
}
public ActionResult Insert(Customer obj)
{
_CustomerRepo.Insert(obj);
_CustomerRepo.Save();
return View();
}
public ActionResult Edit(string id)
{
Customer existing = _CustomerRepo.SelectByID(id);
return View(existing);
}
public ActionResult ConfirmDelete(string id)
{
Customer existing = _CustomerRepo.SelectByID(id);
return View(existing);
}
public ActionResult Delete(string id)
{
_CustomerRepo.Delete(id);
_CustomerRepo.Save();
return View();
}
}
在 AppStart 中添加一个名为 AutofacConfig.cs 的文件,将其设为一个静态类。
public static class AutofacConfig
{
public static void RegisterComponents()
{
var builder = new ContainerBuilder();
builder.RegisterType<CustomerRepository>().As<ICustomerRepository>();
builder.RegisterType<CustomerController>();
builder.RegisterType<Northwind_DBEntities>().As<INorthwind_DBEntities>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
并在 Global.asax 中调用此类的该方法。
在 AutofacConfig 中,我们做了什么。
我们正在注册我们的类及其服务解析器,正如您所见,我们将客户存储库类注册到 IcustomerRepository。
这意味着每当我们使用 ICustomerRepository 时,它都会引用 CustomerRepository.cs。正如您在上面的客户控制器代码中看到的,我们使用了它。
ICustomerRepository _CustomerRepo;
public CustomerController(ICustomerRepository customerRepo)
{
_CustomerRepo = customerRepo;
}
如果我们没有在 Global.asax 中调用 autofac.Registercomponents 方法,它将抛出错误,提示 cutomercontroller 需要无参数构造函数。
要了解更多关于 autofac 的信息,您可以参考 (http://autofac.org/)
public class Application : System.Web.HttpApplication
{
protected void Application_Start()
{
AutofacConfig.RegisterComponents();
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
现在您可以构建解决方案,为客户控制器方法创建视图并使用它们。
您可以参考整个源代码。