Display Unity Mappings from an XML File

I would like to be able to load the following into Unity:

UnityContainer.RegisterType<ClientRegistrationVM, ClientRegistrationVMDesign>();

By downloading it via an XML file.

That would be some kind of psudo code for what I would like to have.

FileStream unityMappings = new FileStream(@".\UnityMappings.xml", FileMode.Open)
UnityContainer.CreateFromXML(unityMappings);
unityMappings.Dispose();

And the UnityMappings.xml file will contain the mappings and DLLs in which the types are located.

Is that what I want even with Unity? Has anyone done this before?

+5
source share
1 answer

You can use XML configuration with Unity, you can even mix configuration time configuration (XML) and runtime (code) configuration.

UnityConfigurationSection, , , (, app.config web.config).

, : :

using System.Configuration;

var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "unity.config" };

Configuration configuration =
    ConfigurationManager.OpenMappedExeConfiguration(fileMap,                  
                                                    ConfigurationUserLevel.None);

var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

var container = new UnityContainer().LoadConfiguration(unitySection);
+8

All Articles