i'll expose i'm doing now, , later, i'd using ninject.
classes:
- kernel,
- apiclient
- iidentity
kernel exposes several methods as:
object method(iidentity identity) ----------------------- public list<domain.channel> getchannels(domain.identity.iidentity identity) {...} kernel contains several identities stored, , according each context need thing or another. in case, context inside method , context key identity parameter value. according one, need instance of apiclient.
actually, i'm doing using dictionary property of kernel class:
private dictionary<domain.identity.iidentity, lest.client.apiclient> lests; and initialize.
foreach (domain.identity.iidentity identity in this.identities) { lest.client.apiclient lest = new lest.client.apiclient(); lest.configure(identity.clientid, identity.username, identity.password); this.lests.add(identity, lest); } so, when need concrete apiclient object according identity:
public list<domain.channel> getchannels(domain.identity.iidentity identity) { lest.client.apiclient current_lest; this.lests.trygetvalue(identity, out current_lest); if (current_lest != null) { ... } i'd using ninject. i've created module in order create bindings itselfs. problem i've no idea how create custom context this.
i'll appreciate lot help.
thanks all.
edit
i've abstracted basic core class summarizes explanation.
public class core { private ilist<iidentity> identities; private ninject.ikernel kernel; public core() { this.identities = new list<iidentity>(); } public void initialize() { this.kernel = new ninject.standardkernel(new lestmodule()); } public void opensession(iidentity identity) { apiclient client = this.kernel.get<apiclient>(); } public void dosomething(iidentity identity) { apiclient client = this.kernel.get<apiclient>(); client... } public void closesession(iidentity identity) { apiclient client = this.kernel.get<apiclient>(); this.kernel.release(client); } } and module is:
public class lestmodule : ninject.modules.ninjectmodule { public override void load() { this.bind<apiclient>().toprovider(new customprovider()); this.bind<iidentity>().to<identity>(); } } public class customprovider : iprovider<apiclient> { public object create(icontext context) { //context data??? // how obtain current iidentity on here? object d = context; return new apiclient("", "", ""); } public system.type type { { return typeof(apiclient); } } } and abstraction of apiclient
public class apiclient { private string username; public string username { { return username; } set { username = value; } } private string client_id; public string clientid { { return client_id; } set { client_id = value; } } private string passwd; public string passwd { { return passwd; } set { passwd = value; } } public apiclient(string client_id, string username, string password) { this.client_id = client_id; this.username = username; this.passwd = password; } } so, i'd that:
- in opensession: create apiclient instance (this.kernel.get) initializing instance according iidentity param of method.
- in dosomething: apiclient instance according iidentity param of method.
- in closesession: realise instance according iidentity param of method.
i've implemented this.
it's working now.
public class lestmodule : ninject.modules.ninjectmodule { public override void load() { this.bind<lest.client.apiclient>().toprovider<lest.client.apiclient>(new apiclientprovider()); } } public class apiclientprovider : ninject.activation.iprovider<lest.client.apiclient> { private system.collections.generic.dictionary<domain.identity.lestidentity, lest.client.apiclient> lests; public apiclientprovider() { this.lests = new system.collections.generic.dictionary<domain.identity.lestidentity, lest.client.apiclient>(); } public object create(ninject.activation.icontext context) { ninject.parameters.iparameter identity_parameter = context.parameters.firstordefault(p => p.name == "identity"); domain.identity.lestidentity identity = (domain.identity.lestidentity)identity_parameter.getvalue(context, null); lest.client.apiclient current_client = null; this.lests.trygetvalue(identity, out current_client); if (current_client == null) { current_client = new lest.client.apiclient(); current_client.configure(...); this.lests.add(identity, current_client); } return current_client; } ... } in order instance according identity:
public void createchannel(domain.identity.clientidentity client_identity, domain.identity.useridentity user_identity, domain.channel channel) { lest.client.apiclient client = ninjectservicelocator.instance.kernel.get<lest.client.apiclient>(new ninject.parameters.parameter("identity", new domain.identity.lestidentity(user_identity.id, user_identity.name, client_identity.id, client_identity.name), true)); client.channelsapip.create(automapper.mapper.map<domain.channel, lest.model.channeldto>(channel)); } what thing about?
Comments
Post a Comment