How does RegisterInstance works?

Aug 1, 2014 at 4:45 PM
Edited Aug 1, 2014 at 4:47 PM
I am creating an object of the interface through registerinstance in unity.config file in my MVC project which runs when the application starts.
 ConnectionRepository connectionRepository = new ConnectionRepository();
 unityContainer.RegisterInstance(typeof (IConnectionRepository), connectionRepository);
And in my other classes I getting the instance of the object as this and this class is called for each user that logs in:
IConnectionRepository connectionRepository =
                    DependencyResolver.Current.GetService(typeof (IConnectionRepository)) as ConnectionRepository;
ConnectionRepository has a connectionstring parameter whose value is user specific.
I mean when user 'A' logs in the connection string should be 'x' and user 'B' it should be 'y'.

My question is whenever a call to 'DependencyResolver.Current.GetService(typeof (IConnectionRepository))' for each user creates a new instance of connection repository class or its going to be a single instance for the whole application?

Because if it is single instance for the whole application its going to be a problem because the connection string value changes between users i.e when the code accessing connectionstring for user 'A' it will have user 'B' connection string and cause an unexpected scenarios.
Aug 2, 2014 at 12:14 AM
By default, RegisterInstance registers the object as a singleton so there will be one instance of the object. See Understanding Lifetime Managers for more information.

There are many ways to achieve what you want depending on the specifics of the scenario. For example, you could inject a factory that knows how to return the correct IConnectionRepository based on the specific user.

It sounds like you might want to look at a per request lifetime where when a user makes a request the same connectionRepository (with specific information) is used for the duration of that request. PerRequestLifetimeManager is available in the Unity bootstrapper for ASP.NET MVC.

Randy Levy
Enterprise Library support engineer
Support How-to
Aug 2, 2014 at 2:48 AM
Edited Aug 2, 2014 at 3:26 AM
Hi Andy,

Yeah I am looking for a IConnectionRepository object creation whose scope is for that complete session (uses log in to log off) and that class instance should be created uniquely one for each user when logged in and that instance should be retrieved when ever the resolve method calls and that object instance should live till the user signs off .


In our project we have separate databases for eachclients and we store all the connection strings of those in a common db table .
When user logs in to the application we pull the corresponding client db conn string info from common db table and we want to store the connstring in the IconnectionRepository class that has just one property of type string to store the connection string(client connectionstring for the user who logged in) and use this Conn String for rest of that session interaction between user and corresponding client db.

So we are injecting the concrete class of the IconnectionRespository at each controller,Repository classes and binding the client connection string in the helper classes through
                     IConnectionRepository connectionRepository =
                    DependencyResolver.Current.GetService(typeof (IConnectionRepository))
                    connectionRepository.clientConnectionString= userObj.currentClient.ConnString;
and to retrieve the connstring in the repository classes where the IConnectionRepo is injected via constructor and connect to client related DB.