jeudi 10 décembre 2015

IoC container mappings: singleton vs each-call creation

Lets assume we have a very simple IConfiguration interface that responsible for returning a proper connection string

interface IConfiguration
{
   string ConnectionString {get;}
}

and lets assume only one instance of type that implemented such interface can be used (because it just returns a string, not manage a state, etc.)

So, there are, at least, two ways of how the interface could be registered within a container: as usual - new object per each type requesting, or as a singleton - one object for all type requests. Are there any differences between approaches (maybe performance reasons, lifetime management tricks, etc.)

container.For<IConfiguration>().Use<ConfigurationImpl>();

vs

container.For<IConfiguration>().Singleton().Use<ConfigurationImpl>();

Aucun commentaire:

Enregistrer un commentaire