I have an application in which a lot of classes ultimately depend on repository objects. These repositories are constructed by passing in a server name which they use to build the URI for the web services they talk to. I've added the server name to the project's
user properties, so my repositories are registered like this:
c => new FooRepository(Properties.Settings.Default.Server);
It's a bit clunky, but it works.
The problem I have is that the user could decide to change the server at any time, and when they do so, any existing repositories are invalid. To get around this problem I've set the DefaultReuse on the container to "None" so that everything's
constructed new every time it's requested.
Can someone suggest a way that I could reuse my repository classes (and therefore all the classes that have them as dependencies) as long as the Server setting doesn't change? Once the user changes the server, I need the next call to container.Resolve<IFooRepository>()
to return a new instance using the new server name.
I was thinking of registering the default settings as an instance in a "parent" container, and giving my application a way to spawn off a child container with all the repositories registered in it. Whenever the server name changed, the app would
ditch that child container and create another one. This doesn't seem practical, though, because it feels like my app would have to have too much "knowledge" about containers (so that it could re-create one when the options dialog closes).