问题描述
最近我已经使用过asp.net mvc,并且在示例项目中看到的是使用Database Factory类.您如何为我解释为什么使用它?
Recently I have worked with asp.net mvc and I have seen in sample project is using Database Factory class. How can you explain for me why use it ?
IDatabaseFactory类
IDatabaseFactory class
public interface IDatabaseFactory : IDisposable
{
EFMVCDataContex Get();
}
DatabaseFactory类
DatabaseFactory class
public class DatabaseFactory : Disposable, IDatabaseFactory
{
private EFMVCDataContex dataContext;
public EFMVCDataContex Get()
{
return dataContext ?? (dataContext = new EFMVCDataContex());
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
推荐答案
这是抽象工厂的示例一个>设计模式.想法是创建一个接缝,以在类之间提供松散的耦合,以便可以出于测试目的或扩展应用程序而交换另一种类型的上下文.
This is an example of an Abstract Factory design pattern. The idea is to create a seam to provide loose coupling between the classes so another type of context could be swapped, either for testing purposes or to extend the application.
一般而言,工厂是一种管理短期依赖项(例如数据库连接)的方法.通常,框架公开一种注入工厂实例的方法,然后该框架可以基于接口(在本例中为IDatabaseFactory)作为框架与框架用户之间的契约来使用它.该框架将具有类似于以下内容的代码:
Generally speaking, a factory is a way to manage short-lived dependencies, such as database connections. Typically, a framework exposes a way to inject an instance of the factory and then the framework can work with it based on an interface (in this case IDatabaseFactory) as a contract between the framework, and the framework user. The framework will have code that looks something like this:
public interface ISomeService
{
void DoSomething();
}
public class SomeService()
{
private readonly IDatabaseFactory factory;
// The factory is injected through the constructor
public SomeService(IDatabaseFactory factory)
{
this.factory = factory;
}
public void DoSomething()
{
using (EFMVCDataContex context = this.factory.Get())
{
// Run a LINQ query here using the context
} // This bracket disposes the context
}
}
与服务端创建的上下文相比,服务实例化的寿命更长.更重要的是,在这种情况下,上下文始终可以正确处理.
The service can then be instantiated for a much longer lifetime than the context that is created by the factory. What's more is that the context is always properly disposed in this scenario.
现在,这样做的主要好处是您可以将DatabaseFactory与替代实现互换(通常称为利斯科夫换人原则):
Now, the main benefit from doing this is that you can swap the DatabaseFactory with an alternate implementation (commonly referred to as the Liskov Substitution Principle):
public class MyDatabaseFactory : Disposable, IDatabaseFactory
{
private EFMVCDataContex dataContext;
public EFMVCDataContex Get()
{
return dataContext ?? (dataContext = new AlternateDataContext());
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
假设AlternateDataContext继承(或实现)EFMVCDataContex,可以将MyDatabaseFactory与DatabaseFactory进行苹果换苹果,而无需对SomeService进行任何更改.
Assuming that AlternateDataContext inherits (or implements) EFMVCDataContex, MyDatabaseFactory can be swapped apples-for-apples with DatabaseFactory without making any changes to SomeService.
MyDatabaseFactory可以在构造函数中使用连接字符串进行编码,例如,为您提供了一种连接备用数据库的方法.
MyDatabaseFactory could be coded with a connection string in the constructor, giving you a way to connect to alternate databases, for example.
当然,这样做的另一个好处是创建了IDatabaseFactory的模拟实现,该实现可用于测试DoSomething方法.在单元测试中,SomeService(被测类)应该是唯一使用的真实类,IDatabaseFactory应该是模拟类(可以通过手动编码一个类或使用模拟框架来完成).
Of course, another great benefit of doing this is to create a mock implementation of IDatabaseFactory that can be used to test the DoSomething method. In a unit test, SomeService (the class under test) should be the only real class being used, IDatabaseFactory should be a mock (which could either be done by hand coding a class, or using a mocking framework).
这篇关于为什么要在ASP.NET MVC中使用数据库工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!