Question

Castle Windsor passes the registered concrete type to Controller's constructors. A typical implementation (no pun intended) is:

private readonly IDepartmentRepository _deptsRepository;

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

I need to pass the ctor a second parameter, if possible, so that I can pass that val on to the Repository constructor (I know: tramp data alert, but I don't know if there's a straightforward way around it:

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

REPOSITORY

public DepartmentRepository(int dbInst)
{
    string connStr = string.Format("Phoo{0}Bar", dbInst);
    using (var conn = new OleDbConnection(connStr))
    {
        using (var cmd = conn.CreateCommand())
        {
             . . .

Is it possible to tweak what Castle Windsor sends to the Controller constructor this way? If so, how?

AND/BUT: For this to be of any value (to me, anyway), I need to be able to get the int val (that will be passed to the Controller) from the URL the client sends. IOW, if the client asks the server for data via:

http://locohost:4242/Platypus/GetAll/1

I need to pass a "1" as the second argument to PlatypusController.

If the user asks the server for data via:

http://locohost:4242/Platypus/GetAll/42

I need to pass a "42" as the second argument to PlatypusController.

etc.

Was it helpful?

Solution

This is what I did to solve my Controller/Repository data context Dilemma:

0) Added a database context argument to the Controller's routing attribute. IOW, this:

[Route("api/HHSUsers/GetAll")]

...got changed to this:

[Route("api/HHSUsers/GetAll/{dbContext=03}")]

1) Passed that database context arg to the Repository. To wit, this:

return _hhsusersrepository.GetAll();

...got changed to this:

return _hhsusersrepository.GetAll(dbContext);

...so that the Controller method is now:

[Route("api/HHSUsers/GetAll/{dbContext=03}")]
public IEnumerable<HHSUsers> GetAllHHSUsersRecords(int dbContext)
{
    return _hhsusersrepository.GetAll(dbContext);
}

2) Changed the corresponding method in the Repository interface from:

IEnumerable<HHSUsers> GetAll();

...to this:

IEnumerable<HHSUsers> GetAll(string dbContext);

3) Changed the Repository method from this:

public HHSUsersRepository()
{
    // All the data is loaded here in the ctor
}

public IEnumerable<HHSUsers> GetAll()
{
    return hhsusers;
}

....to this:

public IEnumerable<HHSUsers> GetAll(string dbContext)
{
    LoadHHSUsers(dbContext);
    return hhsusers;
}

private void LoadHHSUsers(int dbContext)
{
    string connStr = string.Format("Foo{0}Bar", dbContext);
    // The same as previously from this point on, except that this:
    // using (var conn = new OleDbConnection(@"Foo Bar Phoo Bar etc"...
    // becomes:
    // using (var conn = new OleDbConnection(connStr))

4) Tack the dbcontext val to the end of the URL when calling the method, so that it is this:

http://localhost:28642/api/HHSUsers/GetAll/42

...instead of this:

http://localhost:28642/api/HHSUsers/GetAll

If the data context to use is "03" I can omit the dbcontext arg from the URL, as 03 is the default value I set when I appended "=03" to the Controller's "dbContext" routing attribute arg.

I know some fancy-pants propeller-heads will find fault with this for some reason (for one reason because of the tramp data going here and there and everywhere like a hobo on steroids), but my response is the same as that of an athlete who is getting trash-talked by an opposing player and yet whose team is winning: just point at the scoreboard. IOW, this works for me, so that's pretty much all I care about. Style points are for runway models and, again, fancy-pants propeller-heads (AKA Star-Bellied Sneeches (as opposed to us plain cats with the unstarred bellies)); see "The perfect is the enemy of the good."

This simple way has that self-same benefit -- of being (relatively) simple to grok and, thus, modify/refactor as necessary. Inelegant? Sure, but so was Joe Kapp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top