简体   繁体   English

使用Unity和asp.net mvc对数据库操作进行依赖注入

[英]dependency injenction insert to database operation with unity and asp.net mvc

I'm trying to integrate dependency injection by employing unity container in an ASP.NET MVC app. 我正在尝试通过在ASP.NET MVC应用程序中使用统一容器来集成依赖项注入。 In the spirit of loose coupling, I apply constructor injection on my controller and my product data access class. 本着松散耦合的精神,我将构造函数注入应用于控制器和产品数据访问类。 This is a glimpse of the code : 这是代码的一瞥:

public interface IProduct
{
   IEnumerable<Product> findAll();
   Product find(Guid id);
   void create();
}
public class Product()
{
    public Guid id { get; set; }
    public string name { get; set; }
    public decimal price { get; set; }

    //dependency
    private IDBEngine _engine = null;

    public Product(IDBEngine engine) {
        this._engine = engine;
    }

    public IEnumerable<Product> findAll() {
        using(IDBconnection connection = this._engine.getConnection()) 
        {
            connection.open();

            //implementation of fetching all datas from db

            return datas;
        }
    }

    public Product find(Guid id) {
        using(IDBconnection connection = this._engine.getConnection()) 
        {
            connection.open();

            //implementation of fetching single data given id from db

            return data;
        }
    }

    public void create(Product product) 
    {
        using(IDBconnection connection = this._engine.getConnection()) 
        {
            connection.open();

            //implementation of insert data to db
        }
    }
}

well this is my implementation on my controller : 好吧,这是我在控制器上的实现:

public class HomeController : Controller 
{
    private readonly IProduct _product;

    public HomeController(IProduct product)
    {
        this._product = product;
    }

    public ActionResult index() {

        //this is OK
        var products = this._product.findAll();

        //this is also OK
        var product = this._product.find(Guid.Parse("some_guid"));

        //this is not OK, cause it made the controller depend on concrete class Product
        this._product.create(new Product {
            id = Guid.newGuid(),
            name = "Vader Shirt",
            price = 99.5
        });

        return View();
    }
}

I inject the Product instance to the controller via container with unity. 我通过container将Product实例注入到控制器中。 When I call the findAll() and find() method, it was totally fine cause it still let me decouple the controller and the concrete class Product using the IProduct interface. 当我调用findAll()find()方法时,完全没问题,因为它仍然允许我使用IProduct接口将控制器和具体的类Product分离。 But when I had to do insert operation via create(Product product) method, I had to create an instance of product to do so, hence coupled my controller to a concrete class : Product. 但是当我不得不通过create(Product product)方法执行插入操作时,我不得不创建一个product实例来执行此操作,因此将我的控制器耦合到一个具体的类:Product。

My question is how do I decoupled my controller and at the same time perform the create method from my controller? 我的问题是如何解耦控制器,同时执行控制器的create方法? And I think it goes the same for the update operation. 而且我认为更新操作也一样。

Stephen Muecke gave a good advice there, try this: Stephen Muecke在那里提供了很好的建议,请尝试以下操作:

public interface IProductService{
   void CreateProduct(Guid id, string name, double price);
   IEnumerable<Product> FindAll();
   IProduct Find(Guid id);
  }

then your controller: 然后您的控制器:

public class HomeController : Controller 
{
    private readonly IProductService _productService;

    public HomeController(IProductService service)
    {
        this._productService = service;
    }

 public ActionResult index() {

        //this is OK
        var products = this._productService.FindAll();

        //this is also OK
        var product = this._productService.Find(Guid.Parse("some_guid"));

        this._productService.CreateProduct(
            id = Guid.newGuid(),
            name = "Vader Shirt",
            price = 99.5
        );

        return this.View();
    }
}

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

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