问题
I am building a standard three tier app.
1 Console app for front end
2 Business logic layer
3 Data layer
The main purpose is to display some customer data from a database table.
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.
Let's say I have a service at the business layer called CustomerService and it has a GetCustomers()
method
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();
}
}
At the data layer I have
public class CustomerRepository : BLL.ICustomerRepository
{
public ICollection<Customer> GetCustomers()
{
// get the customers from the db
return customers;
}
}
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<??????????????>()
}
}
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?
回答1:
Bind<ICustomerRepository>().To<CustomerRepository>();
来源:https://stackoverflow.com/questions/14785855/ninject-in-a-three-tier-application