asp.net mvc - Web API, Light Inject and Passing a Static Dictionary to the data layer -


we have multi-database solution , passing connection string factory function so:

container.register<idbcontextfactory>(     f => new dynamicdbcontextfactory(clientconfig.getconnectionstring()),     new perscopelifetime()); 

clientconfig contains static dictionary gets populated on app start maps sub domain connection string. seems approach causing memory leak (not 100% sure causing leak there leak).

public class clientconfig {     private static concurrentdictionary<string, string> connectionstringmanager      {          get;         set;      }      // etc. } 

my question in mvc best way hold list of connection strings can looked on each request in order pass down chain.

edit : question tagged autofac


with autofac don't have use dictionary , want. can use custom parameter :

public class connectionstringparameter : parameter {     public override boolean cansupplyvalue(parameterinfo pi,                                             icomponentcontext context,                                             out func<object> valueprovider)     {         valueprovider = null;          if (pi.parametertype == typeof(string)             && string.equals(pi.name, "connectionstring",                               stringcomparison.ordinalignorecase))         {             valueprovider = () =>             {                 // connectionstring based on httpcontext.current.request.url.host                  return string.empty;             };         }          return valueprovider != null;     } } 

then register parameter using module

public class connectionstringmodule : autofac.module {     protected override void attachtocomponentregistration(         icomponentregistry componentregistry, icomponentregistration registration)     {         registration.preparing += registration_preparing;     }      private void registration_preparing(object sender, preparingeventargs e)     {         parameter[] parameters = new parameter[] { new connectionstringparameter() };          e.parameters = e.parameters.concat(parameters);     } } 

module have register inside container using

containerbuilder builder = new containerbuilder(); builder.registermodule(new connectionstringmodule()); 

each time autofac have resolve parameter of type string named connectionstring used custom parameter , connectionstring based on want.


by way code sample use httpcontext.current. in case of multithreaded process may return null. don't recommend using httpcontext.current such things. can use intermediate class instead of accessing it, example iconnectionstringprovider interface.

public interface iconnectionstringprovider {     string connectionstring { get; } } public class connectionstringprovider : iconnectionstringprovider {     public connectionstringprovider(strong host)     {         // connectionstring based on host         this._connectionstring = string.empty;     }      private readonly string _connectionstring;      public string connectionstring     {         { return this._connectionstring; }     } } 

inside parameter have change valueprovider

valueprovider = () => {     return context.resolve<iconnectionstringprovider>().connectionstring;  }; 

and have register iconnectionstringprovider @ beginning of request lifetimescope :

class program {     static void main(string[] args)     {         containerbuilder builder = new containerbuilder();         builder.registermodule(new connectionstringmodule());          icontainer container = builder.build();         container.childlifetimescopebeginning += container_childlifetimescopebeginning;     }      private static void container_childlifetimescopebeginning(         object sender, lifetimescopebeginningeventargs e)     {         string host = httpcontext.current.request.url.host;          containerbuilder childlifetimescopebuilder = new containerbuilder();         childlifetimescopebuilder.registerinstance(new connectionstringprovider(host))                                  .as<iconnectionstringprovider>()                                  .singleinstance();         childlifetimescopebuilder.update(e.lifetimescope.componentregistry);     } } 

of course there many way have idea


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -