Unity Configuration from Separate Configuration File
2011-11-22 09:42 无名365 阅读(735) 评论(0) 编辑 收藏 举报A few people asked me for an example of configuring Unity in a separate configuration file which is the motivation for this post. The Unity Documentation is pretty good on this topic. In fact, I pretty much just copy and pasted the code for reading from a separate file in this post, but this post might be a bit easier to find the information.
Reading Unity Configuration from Separate Configuration File
In this example, I am using a simple Console Application. The configuration information is in a separate configuration file, called unity.config, which has one container, called container, that maps a UserRepository to IUserRepository. The UserRepository constructor requires a connection string so I specified the connection string in the constructor parameters in the type configuration.
Program.cs
The GetContainer Method is pretty much boiler-plate code responsible for reading the unity.config file and populating a new container with the configuration information. You could make it more generic by passing in parameters for the name of the file, name of the container, etc. This is basically straight out of the documentation:
private static void Main(string[] args)
{
IUnityContainer container = GetContainer();
var repository = container.Resolve<IUserRepository>();
}
private static IUnityContainer GetContainer()
{
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "unity.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection) config.GetSection("unity");
var container = new UnityContainer();
section.Containers["container"].Configure(container);
return container;
}
Unity.config
The configuration file sets up some aliases, specifies the mapping between IUserRepository and UserRepository, provides an example of using a singleton lifetime manager, and contains type configuration that specifies how to construct the UserRepository:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<typeAliases>
<typeAlias alias="string" type="System.String, mscorlib" />
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="IUserRepository" type="ConsoleApplication3.IUserRepository, ConsoleApplication3" />
<typeAlias alias="UserRepository" type="ConsoleApplication3.UserRepository, ConsoleApplication3" />
</typeAliases>
<containers>
<container name="container">
<types>
<type type="IUserRepository" mapTo="UserRepository" >
<lifetime type="singleton" />
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<constructor>
<param name="connectionString" parameterType="string">
<value value="[YourConnectionString]"/>
</param>
</constructor>
</typeConfig>
</type>
</types>
</container>
</containers>
</unity>
</configuration>
IUserRepository and UserRepository
And last, a barebones illustration of the interface and concrete implementations of the types just to illustrate the use of the configuration file:
public interface IUserRepository {}
public class UserRepository : IUserRepository
{
public string ConnectionString { get; private set; }
public UserRepository(string connectionString) {
ConnectionString = connectionString;
}
}
Conclusion
Hopefully this helps you configure Unity in a separate configuration file. I think this is always a good idea as it keep your IoC / Dependency Injection configuration information separate from application settings in your app.config or web.config.
From http://www.pnpguidance.net/post/UnityConfigurationSeparateConfigurationFile.aspx