3
votes

I am using Unity for dependency injection and have the instance of a class Config that implements IConfig registered with the Unity Container.

IConfiguration config = new Configuration();

unityContainer.RegisterInstance<IConfiguration>(config);

When the app is running and the user loads a saved configuration, I would like to update the instance stored in Unity with the configuration object loaded. E.g. something like the psuedo code below

IConfiguration loadedConfig = FileLoadService.Load(filepath);
unityContainer.Instance = loadedConfig;

Is this possible?

1

1 Answers

4
votes

You could just re-register the instance by calling

unityContainer.RegisterInstance<IConfiguration>(config);

But a better solution would be to change your FileLoadServce.Load(filepath) logic so that it updates the values of your existing configuration object. The benefit of this solution would be that already resolved references (by calling unityContainer.Resolve<IConfiguration>()) to this config object are updated, too.