Can a second parameter be passed to Controller constructors? -


castle windsor passes registered concrete type controller's constructors. 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 pass ctor second parameter, if possible, can pass val on repository constructor (i know: tramp data alert, don't know if there's 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 possible tweak castle windsor sends controller constructor way? if so, how?

and/but: of value (to me, anyway), need able int val (that passed controller) url client sends. iow, if client asks server data via:

http://locohost:4242/platypus/getall/1 

i need pass "1" second argument platypuscontroller.

if user asks server data via:

http://locohost:4242/platypus/getall/42 

i need pass "42" second argument platypuscontroller.

etc.

this did solve controller/repository data context dilemma:

0) added database context argument controller's routing attribute. iow, this:

[route("api/hhsusers/getall")] 

...got changed this:

[route("api/hhsusers/getall/{dbcontext=03}")] 

1) passed database context arg repository. wit, this:

return _hhsusersrepository.getall(); 

...got changed this:

return _hhsusersrepository.getall(dbcontext); 

...so controller method now:

[route("api/hhsusers/getall/{dbcontext=03}")] public ienumerable<hhsusers> getallhhsusersrecords(int dbcontext) {     return _hhsusersrepository.getall(dbcontext); } 

2) changed corresponding method in repository interface from:

ienumerable<hhsusers> getall(); 

...to this:

ienumerable<hhsusers> getall(string dbcontext); 

3) changed repository method this:

public hhsusersrepository() {     // data loaded here in 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);     // same point on, except this:     // using (var conn = new oledbconnection(@"foo bar phoo bar etc"...     // becomes:     // using (var conn = new oledbconnection(connstr)) 

4) tack dbcontext val end of url when calling method, this:

http://localhost:28642/api/hhsusers/getall/42 

...instead of this:

http://localhost:28642/api/hhsusers/getall 

if data context use "03" can omit dbcontext arg url, 03 default value set when appended "=03" controller's "dbcontext" routing attribute arg.

i know fancy-pants propeller-heads find fault reason (for 1 reason because of tramp data going here , there , everywhere hobo on steroids), response same of athlete getting trash-talked opposing player , yet team winning: point @ scoreboard. iow, works me, that's pretty care about. style points runway models and, again, fancy-pants propeller-heads (aka star-bellied sneeches (as opposed plain cats unstarred bellies)); see "the perfect enemy of good."

this simple way has self-same benefit -- of being (relatively) simple grok and, thus, modify/refactor necessary. inelegant? sure, joe kapp.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -