Lyn*_*nnw 6 asp.net asp.net-mvc dependency-injection repository-pattern
我试图理解依赖注入和存储库模式之间的区别.
根据我的理解,Repository Pattern会执行Dependency Injection正在执行的所有操作,除了在Dependency Injection中我们使用的是Constructor Injection.
好的,让我试着解释一下这个例子:(请注意我使用的是Unity Framework for DI)
创建产品类
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在模型上创建接口
public interface IProductRepository
{
IEnumerable<Product> GetAll();
Product Get(int id);
Product Add(Product item);
bool Update(Product item);
bool Delete(int id);
}
Run Code Online (Sandbox Code Playgroud)
实现接口
//ProductRepository.cs
public class ProductRepository : IProductRepository
{
private List<Product> products = new List<Product>();
private int _nextId = 1;
public ProductRepository()
{
// Add products for the Demonstration
Add(new Product { Name = "Computer", Category = "Electronics", Price = 23.54M });
Add(new Product { Name = "Laptop", Category = "Electronics", Price = 33.75M });
Add(new Product { Name = "iPhone4", Category = "Phone", Price = 16.99M });
}
public IEnumerable GetAll()
{
// TO DO : Code to get the list of all the records in database
return products;
}
public Product Get(int id)
{
// TO DO : Code to find a record in database
return products.Find(p => p.Id == id);
}
public Product Add(Product item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
// TO DO : Code to save record into database
item.Id = _nextId++;
products.Add(item);
return item;
}
public bool Update(Product item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
// TO DO : Code to update record into database
int index = products.FindIndex(p => p.Id == item.Id);
if (index == -1)
{
return false;
}
products.RemoveAt(index);
products.Add(item);
return true;
}
public bool Delete(int id)
{
// TO DO : Code to remove the records from database
products.RemoveAll(p => p.Id == id);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
对于Bootstrap.cs中的DI Initialize Dependency
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<IProductRepository, ProductRepository>();
// e.g. container.RegisterType<ITestService, TestService>();
RegisterTypes(container);
return container;
}
Run Code Online (Sandbox Code Playgroud)
调节器
public class ProductController : Controller
{
readonly IProductRepository repository;
//inject dependency
public ProductController(IProductRepository repository)
{
this.repository = repository;
}
public ActionResult Index()
{
var data = repository.GetAll();
return View(data);
}
//Other Code
}
Run Code Online (Sandbox Code Playgroud)
我的问题
代码来源:http://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-using-Unity-IoC-Container.html
它们实际上不具有可比性,存储库是您可以通过依赖注入注入的东西.DI的目的是使您的应用程序松散耦合.您可以指定一个接口来定义实现必须满足的合同,而不是指定具体的实现.这样你就可以更轻松地交换实现.
Martin Fowler定义的存储库模式将您的域与关注存储的实现方式隔离开来,因此可以将检索到的所有对象视为内存集合.您可以拥有基于数据库,XML文件,文本文档或任何内容的存储库.应用程序代码本身并不关心.这使得它非常适用于测试与TDD的连接.
您将(注入)依赖性传递给控制器.这些可能是存储库,服务或控制器所需的任何内容.您的IoC容器在运行时将所有这些连接在一起.这本身就非常强大,我们在SaaS应用程序中大量使用DI,客户有自己的实现,这些实现根据客户端有条件地注入.
我建议你阅读.NET中的依赖注入.Mark Seemann可以比我更好地解释这一点,并且是对你应该使用DI和各种IoC容器(例如Unity)的方法的精彩介绍
存储库模式和DI有什么区别
我认为您无法进行有意义的比较。它们都是编程技术,但是它们来自非常不同的考虑因素。(参见存储库模式,依赖注入)
我猜您可以说它们是以这种方式连接的:数据存储库是您的DI框架通常会注入需要它的对象中的外部依赖项的示例。
结构注入的优势是什么
与其他注入方式相反,您是说吗?然后:构造函数注入允许您在首次初始化对象时访问注入的依赖项。
还是您的意思是,通常使用DI有什么优势?这是一个非常广泛的问题,但我要说:您的应用程序的结构连贯,其组件之间的耦合性较差。
赞赏是否有人可以用尽可能多的详细信息解释Bootstrap.cs文件代码和Controller类代码。
与Ninject中的“内核”类相似,Unity中的“ Bootstrapper”类实现了DI模式的所谓“组成层”。在这里,您可以告诉DI容器应如何解决它将在整个应用程序中找到的所有依赖项。这基本上意味着将注入的接口与其实现进行匹配,并提供有关注入对象范围的指令(即,单例,每个请求或瞬态)。
| 归档时间: |
|
| 查看次数: |
8782 次 |
| 最近记录: |