How to specify an instance of the class that my controller constructor should get (when working with the web API, DI, and Castle Windsor)?

Perhaps my questions here and here are not clear enough, so I will try to reduce verbosity without diminishing clarity.

Let's say my controller uses DI (you can verbalize it "in your head" - you do not need to pronounce it out loud); so it might look like this:

private readonly IDepartmentRepository _deptsRepository;

    public DepartmentsController(IDepartmentRepository deptsRepository)
    {
        if (deptsRepository == null)
        {
            throw new ArgumentNullException("deptsRepository is null");
        }
        _deptsRepository = deptsRepository;
    }

That way, I can intercept the web API routing mechanism by passing it a specific class that implements the IDepartmentRepository for the Controller constructor. So if my interface is:

public interface IDepartmentRepository
{
    int Get();
    IEnumerable<Department> Get(int ID, int CountToFetch);
    Department Add(Department item);
    void Post(Department dept);
    void PostDepartment(int accountid, string name);
    void Put(Department dept);
    void Delete(int Id);
}

... I could have classes like this:

public class DepartmentRepositoryProductionData : IDepartmentRepository
{
    private readonly List<Department> departments = new List<Department>();

    public DepartmentRepository()
    {
        using (var conn = new OleDbConnection(
            @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=BlaBlaBla..."))
        {
...                        

... and this:

public class DepartmentRepositoryTestData : IDepartmentRepository
{
    private readonly List<Department> departments = new List<Department>();

    public DepartmentRepository()
    {
        // Load test data from an XML file (or text file, or whatever)
...                        

, dandy, / , , DepartmentRepositoryTestData ( DepartmentRepositoryProductionData) , Controller? , DI, , , ( )

, IOW , URI? , , " ".

UPDATE

, :

( arg Controller) - , , , ? " " " ", "California Pizza" "New York Pizza", , -, , IPizzaPie.

- - , : " " " -" ..

: / ( ) , ?

2

, , , , - ( "WindsorCompositionRoot.cs" ) :

public IHttpController Create(
    HttpRequestMessage request,
    HttpControllerDescriptor controllerDescriptor,
    Type controllerType)
{
    var controller =
        (IHttpController)this.container.Resolve(controllerType);

    request.RegisterForDispose(
        new Release(
            () => this.container.Release(controller)));

    return controller;
}

... , (IHttpController)...

3

, this

0
1

, - TDD , . .

, , - :

var sut = new DepartmentsController(new DepartmentRepositoryTestData());

, IDepartmentRepository - .

, , . , .

Edit

, : , DI. DI.

:

( arg Controller) , , , , ?

, / , ? , , , DI, , () . .

" " " ", " " "- ", , -, , IPizzaPie.

- - , : " " " -", .

: / ( decider), , ?

, , , Factory. , factory, .

: http://blog.ploeh.dk/2012/03/15/ImplementinganAbstractFactory/

# 2

, , ASP.Net MVC. , MVC DI.

- . factory, . factory , :

IDepartmentRepository repo = _departmentRepoFactory.Create(myContext);

, - - , - (, , ..), factory.

+1

All Articles