Thursday, 15 March 2012

c# - Generics and Property binding in Ninject -



c# - Generics and Property binding in Ninject -

i'm looking way incorporate settings within webapp (asp mvc). i've came across nice implementation in nopcommerce. nopcommerce stores values in database table, name , value. name derived class , property name (e.g. customersettings.settingname1)

the exact way nopcommerce works settings can found in question: understanding how nop commerce settings loaded database

nopcommerce uses autofac di framework bind settings configurationprovider follows (as i'm correct).

return registrationbuilder .fordelegate((c, p) => c.resolve<iconfigurationprovider<tsettings>>().settings) .instanceperhttprequest() .createregistration();

in appropriate classes, can utilize clientsettings parameter, , it's automatically filled info database.

i implementation, because flexible. problem i'm using ninject. i've tried several things right bindings, can't seem find right implementation. have thought how working?

edit:

i found way bind clientsettings directly:

kernel.bind<clientsettings>() .tomethod(ctx => ctx.kernel.get<iconfigurationprovider<clientsettings>>().settings) .inrequestscope();

but there way accomplish this?

kernel.bind<isettings>() .tomethod(ctx => ctx.kernel.get<iconfigurationprovider<isettings>>().settings) .inrequestscope();

edit 2

i think i'm getting close, still run problems. create custom binding generator:

public class settingsbindgenerator : ibindinggenerator { static readonly methodinfo buildmethod = typeof(settingsbindgenerator).getmethod( "buildregistration", bindingflags.static | bindingflags.nonpublic); public ienumerable<ibindingwheninnamedwithoronsyntax<object>> createbindings(type type, ibindingroot bindingroot) { var obj = typeof (object).isassignablefrom(type); if (type != null && typeof(isettings).isassignablefrom(type)) { var buildmethod = buildmethod.makegenericmethod(type); var methodresult = buildmethod.invoke(null, new object[]{bindingroot}); var castedresult = methodresult ibindingwheninnamedwithoronsyntax<object>; yield homecoming castedresult; } } static ibindingwheninnamedwithoronsyntax<tsettings> buildregistration<tsettings>(ibindingroot bindingroot) tsettings : isettings, new() { homecoming bindingroot.bind<tsettings>().tomethod( ctx => ctx.kernel.get<iconfigurationprovider<tsettings>>().settings); } }

this works 99%. however, reason, buildmethod.invoke returns bindingconfigurationbuilder, , not ibindingwheninnamedwithoronsyntax. therefor, castedresult null. got thought how right this?

last edit

i don't know why, works! glad i've figured out. thanx remo!

you have several options:

do nopcommerce , scan settings classes , phone call registration method first edit using reflection. use conventions extension register classes implementing isettings using custom binding generator https://github.com/ninject/ninject.extensions.conventions/wiki/projecting-services-to-bind use conventions extension register classes implementing isettings bind interfaces. , add together custom iactivationstrategy ninject assigns properties nopcommerce does.

c# asp.net-mvc-3 ninject nopcommerce

No comments:

Post a Comment