1

In my app I ask the user which database they want to connect to and I was writing it back into EL5.0 like this:

    var builder = new ConfigurationSourceBuilder();

    builder.ConfigureData()
           .ForDatabaseNamed("UserDatabase")
             .ThatIs.ASqlDatabase()
             .WithConnectionString(sqlConnectionStringBuilder.ConnectionString)
             .AsDefault();

    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current
        = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

Which was then used whenever I called GetInstance like this:

    TestSQLConnection testSQLConnection = 
        EnterpriseLibraryContainer.Current.GetInstance<TestSQLConnection>();

Now I'm trying to use Unity in my programs main class

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        IUnityContainer unityContainer = new UnityContainer().AddNewExtension<EnterpriseLibraryCoreExtension>();
        Application.Run(unityContainer.Resolve<MainForm>());
    }

and I use unityContainer which is a one of MainForm's dependencies instead of GetInstance():

    TestSQLConnection testSQLConnection = unityContainer.Resolve<TestSQLConnection>(); 

But this doesn't use the updated configuration.

How do I merge the updated configuration in Unity like I did with the EL static class?

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
Stephen Turner
  • 7,125
  • 4
  • 51
  • 68

2 Answers2

4

How is unityContainer getting passed into MainForm? It looks like you are resolving one container with another, since you resolve MainForm like this, after creating a new UnityContainer:

Application.Run(unityContainer.Resolve<MainForm>());

. . . and then you say unityContainer is a dependency of MainForm.

It looks to me like you have at least two, and possibly three different instances of the container. Can you simply use EnterpriseLibraryContainer.Current in all cases?

As an aside, in most cases you don't want to pass the container into your implementations or call Resolve() directly from your implementations. This is the Service Locator Anti-Pattern.

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
2

In addition to Phil's answer (which I second), containers should always be used according to the Register Resolve Release pattern. This implies that once you start Resolving (and Releasing) instances from the container, you should not modify its configuration. I can't really tell from the question whether that's what is being asked for, but the title seems to imply it.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I did almost have an "(Is this right?)" about half way through my question... The first thing the app does is get the connection/logon details, from that point on the config is fixed. With this change I'm making the config happen half way through. I see that's not right, but I'm also starting to understand there is a lot more that I'm getting wrong. – Stephen Turner Jul 19 '11 at 10:03