在三层应用程序中注入 [英] Ninject in a three tier application
问题描述
我正在构建一个标准的三层应用程序.
I am building a standard three tier app.
1个用于前端的控制台应用程序
1 Console app for front end
2业务逻辑层
3数据层
主要目的是显示数据库表中的一些客户数据.
The main purpose is to display some customer data from a database table.
我试图遵循《 .NET中的依赖注入》一书中的指导方针,从控制台到数据层没有引用,从业务层到数据层也没有引用.如果需要,可以轻松交换前端和数据层.
I'm trying to follow the guide lines in the book "Dependency Injection in .NET" by having no reference from the console to the data layer, and none from the business layer to the data layer. Allowing for easy swapping of the front end and the data layers if needed.
假设我在业务层有一个名为CustomerService的服务,它有一个GetCustomers()
方法
Let's say I have a service at the business layer called CustomerService and it has a GetCustomers()
method
CustomerService
的构造函数采用这样的ICustomerRepository
the constructor of CustomerService
takes an ICustomerRepository
like so
public class CustomerService
{
ICustomerRepository repository;
public CustomerService(ICustomerRepository repository)
{
this.repository = repository;
}
public ICollection<Customer> GetCustomers()
{
return repository.GetCustomers();
}
}
在数据层,我有
public class CustomerRepository : BLL.ICustomerRepository
{
public ICollection<Customer> GetCustomers()
{
// get the customers from the db
return customers;
}
}
在控制台应用程序中,我想使用Ninject调用创建CustomerService对象来满足ICustomerRepository依赖性.
In the console app I want to call the create a CustomerService object using Ninject to fulfil the ICustomerRepository dependency.
class DIModule : NinjectModule
{
public override void Load()
{
Bind<>(ICustomerRepository).To<??????????????>()
}
}
如何绑定到数据层CustomerRepository类?我必须将控制台应用程序的引用添加到数据层才能正常工作? 我在做什么错了?
How can I bind to the data layers CustomerRepository class? I would have to add a reference from the console app to the data layer for this to work? What am I doing wrong?
推荐答案
Bind<ICustomerRepository>().To<CustomerRepository>();
这篇关于在三层应用程序中注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!