serviceprovider, cache etc. finished generics but cast
i'm articulate c# 3.5 3.0.
i know cache serviceprovider have wholly instance whole application. box serviceprovider feeling this
public stationary category service<t>
{
open stationary t value {get; set;}
}
and used opposite forms this:
service<idbconnection>.value = new sqlconnection("...");
service<idesigner>.value = ...;
//...
idbcommand cmd = service<idbconnection>.value.createcommand();
static cache also easy:
public stationary category cache<t>
{
private stationary dictionary<int, t> cache = new dictionary<int, t>();
open stationary vacant add(int key, t value)
{
cache.add(key, value);
}
open stationary t find(int key)
{
relapse cache[key];
}
}
and used this:
cache<string>.add(1, "test");
cache<datetime>.add(2, datetime.now);
//...
string found = cache<string>.find(1);
my doubt is: i emanate similiar cache use provider i wish have 2 some-more opposite instances each. here instance code, i wish use provider:
serviceprovider provider = new serviceprovider();
provider.add<idbconnection>(new sqlconnection("..."));
provider.add<idesigner>(...);
//...
serviceprovider provider1 = new serviceprovider();
provider1.add<idbconnection>(new sqlconnection("..."));
//...
//...
idbcommand cmd1 = provider.getservice<idbconnection>().createcommand();
idbcommand cmd2 = provider1.getservice<idbconnection>().createcommand();
the wholly doing i have conduct controlling casting i wish avoid.
public category serviceprovider
{
private dictionary<type, object> services = new dictionary<type, object>();
open vacant add<t>(t value)
{
services.add(typeof(t), value);
}
open t getservice<t>()
{
relapse (t) services[typeof (t)];
}
}
Comments
Post a Comment