I'm trying to integrate dependency injection by employing unity container in an ASP.NET MVC app. 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 :
我试图通过在ASP.NET MVC应用程序中使用统一容器来集成依赖注入。本着松散耦合的精神,我在我的控制器和产品数据访问类上应用构造函数注入。这是代码的一瞥:
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. 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. 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.
我通过具有统一性的容器将Product实例注入控制器。当我调用findAll()和find()方法时,它完全没问题,因为它仍然允许我使用IProduct接口将控制器和具体类Product分离。但是当我不得不通过create(产品产品)方法进行插入操作时,我必须创建一个产品实例,因此将我的控制器耦合到一个具体的类:Product。
My question is how do I decoupled my controller and at the same time perform the create method from my controller? And I think it goes the same for the update operation.
我的问题是如何解耦我的控制器,同时从我的控制器执行创建方法?我认为更新操作也是如此。
1 个解决方案
#1
0
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();
}
}
#1
0
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();
}
}