Last active
December 16, 2015 08:39
-
-
Save andriniaina/5407537 to your computer and use it in GitHub Desktop.
Unity Injection by factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<configuration> | |
<configSections> | |
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> | |
</configSections> | |
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> | |
<sectionExtension type="Unity.FactoryConfig.FactoryConfigExtension, Unity.FactoryConfig"/> | |
<alias alias="IDbConnection" type="System.Data.IDbConnection, System.Data"/> | |
<container> | |
<register type="IDbConnection" name="conn123456789"> | |
<factory type="UnityTestInjection.Program, UnityTestInjection" method="GetConnection"/> | |
</register> | |
</container> | |
</unity> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Practices.Unity; | |
using Microsoft.Practices.Unity.Configuration; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Configuration; | |
using System.Data; | |
namespace UnityTestInjection | |
{ | |
public class Program | |
{ | |
public static System.Data.IDbConnection GetConnection() | |
{ | |
return new System.Data.SqlClient.SqlConnection(); | |
} | |
static void Main(string[] args) | |
{ | |
var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); | |
var container = new UnityContainer().LoadConfiguration(unitySection); | |
var connection = container.Resolve<IDbConnection>("conn123456789"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install-Package Unity -Version 2.1.505.2 | |
https://bitbucket.org/ctavares/unityfactoryconfig or https://github.com/andriniaina/Unity.FactoryConfig | |
http://stackoverflow.com/questions/9943216/unity-factory-via-xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment