Help me to implement DI with unity without config file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
i am familiar with how to implement Di with unity with config file. so here i am posting my code where config file has been used. so anyone just have a look at my code and implement the same functionality without config file like register type and resolve
instance based on condition.
Lets define an interface
<pre class="prettyprint prettyprinted" style=" <span class="kwd public<span class="pln <span class="kwd interface<span class="pln <span class="typ ILogger<span class="pln
<span class="pun {<span class="pln
<span class="kwd void<span class="pln <span class="typ Write<span class="pun (<span class="kwd string<span class="pln message<span class="pun );<span class="pln
<span class="pun }[/code]
We have define three classes as follows.
<pre class="prettyprint prettyprinted" style=" <span class="kwd public<span class="pln <span class="kwd class<span class="pln <span class="typ FileLogger<span class="pln <span class="pun :<span class="pln <span class="typ ILogger<span class="pln
<span class="pun {<span class="pln
<span class="kwd public<span class="pln <span class="kwd void<span class="pln <span class="typ Write<span class="pun (<span class="kwd string<span class="pln message<span class="pun )<span class="pln
<span class="pun {<span class="pln
<span class="com //Do somthing<span class="pln
<span class="pun }<span class="pln
<span class="pun }<span class="pln

<span class="kwd public<span class="pln <span class="kwd class<span class="pln <span class="typ SQLLogger<span class="pln <span class="pun :<span class="pln <span class="typ ILogger<span class="pln
<span class="pun {<span class="pln
<span class="kwd public<span class="pln <span class="kwd void<span class="pln <span class="typ Write<span class="pun (<span class="kwd string<span class="pln message<span class="pun )<span class="pln
<span class="pun {<span class="pln
<span class="com //Do somthing<span class="pln
<span class="pun }<span class="pln
<span class="pun }<span class="pln

<span class="kwd public<span class="pln <span class="kwd class<span class="pln <span class="typ WindowsEventLogger<span class="pln <span class="pun :<span class="pln <span class="typ ILogger<span class="pln
<span class="pun {<span class="pln
<span class="kwd public<span class="pln <span class="kwd void<span class="pln <span class="typ Write<span class="pun (<span class="kwd string<span class="pln message<span class="pun )<span class="pln
<span class="pun {<span class="pln
<span class="com //Do somthing<span class="pln
<span class="pun }<span class="pln
<span class="pun }[/code]

Need to register and map these classes with interface in configuration file (i.e. app.config).
<pre class="prettyprint prettyprinted" style=" <span class="tag <configSections><span class="pln
<span class="tag <section<span class="pln <span class="atn name<span class="pun =<span class="atv "unity"<span class="pln <span class="atn type<span class="pun =<span class="atv "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"<span class="tag /><span class="pln
<span class="tag </configSections><span class="pln

<span class="tag <unity<span class="pln <span class="atn xmlns<span class="pun =<span class="atv "http://schemas.microsoft.com/practices/2010/unity"<span class="tag ><span class="pln
<span class="tag <alias<span class="pln <span class="atn type<span class="pun =<span class="atv "UnityTest.ILogger, UnityTest"<span class="pln <span class="atn alias<span class="pun =<span class="atv "ILogger"<span class="pln <span class="tag /><span class="pln
<span class="tag <namespace<span class="pln <span class="atn name<span class="pun =<span class="atv "UnityTest"<span class="tag /><span class="pln

<span class="tag <container><span class="pln
<span class="tag <register<span class="pln <span class="atn mapTo<span class="pun =<span class="atv "UnityTest.FileLogger, UnityTest"<span class="pln <span class="atn name<span class="pun =<span class="atv "MyFileLogger"<span class="pln <span class="atn type<span class="pun =<span class="atv "ILogger"<span class="tag /><span class="pln
<span class="tag <register<span class="pln <span class="atn mapTo<span class="pun =<span class="atv "UnityTest.SQLLogger, UnityTest"<span class="pln <span class="atn name<span class="pun =<span class="atv "MySQLLogger"<span class="pln <span class="atn type<span class="pun =<span class="atv "ILogger"<span class="tag /><span class="pln
<span class="tag <register<span class="pln <span class="atn mapTo<span class="pun =<span class="atv "UnityTest.WindowsEventLogger, UnityTest"<span class="pln <span class="atn name<span class="pun =<span class="atv "MyWindowsEventLogger"<span class="pln <span class="atn type<span class="pun =<span class="atv "ILogger"<span class="tag /><span class="pln
<span class="tag </container><span class="pln
<span class="tag </unity>[/code]
Note: name attribute is important in register tag.
Finally we have to use this map into our code. So, we have to know that for which one is preferable for specific country.
A dictionary object can be use to keep this mapping as follows.
<pre class="prettyprint prettyprinted" style=" <span class="typ IDictionary<span class="pun <<span class="kwd string<span class="pun ,<span class="pln <span class="kwd string<span class="pun ><span class="pln loggers <span class="pun =<span class="pln <span class="kwd new<span class="pln <span class="typ Dictionary<span class="pun <<span class="kwd string<span class="pun ,<span class="pln <span class="kwd string<span class="pun >();<span class="pln
loggers<span class="pun .<span class="typ Add<span class="pun (<span class="str "USA"<span class="pun ,<span class="pln <span class="str "MyFileLogger"<span class="pun );<span class="pln
loggers<span class="pun .<span class="typ Add<span class="pun (<span class="str "GBR"<span class="pun ,<span class="pln <span class="str "MySQLLogger"<span class="pun );<span class="pln
loggers<span class="pun .<span class="typ Add<span class="pun (<span class="str "IN"<span class="pun ,<span class="pln <span class="str "MyWindowsEventLogger"<span class="pun );[/code]

You can populate it from database, xml or another source, and now its time to call the Write method.
<pre class="prettyprint prettyprinted" style=" <span class="typ IUnityContainer<span class="pln container <span class="pun =<span class="pln <span class="kwd new<span class="pln <span class="typ UnityContainer<span class="pun ();<span class="pln
container<span class="pun .<span class="typ LoadConfiguration<span class="pun ();<span class="pln

<span class="typ ILogger<span class="pln logger <span class="pun =<span class="pln container<span class="pun .<span class="typ Resolve<span class="pun <<span class="typ ILogger<span class="pun >(<span class="pln loggers<span class="pun [<span class="pln objUser<span class="pun .<span class="pln countryCode<span class="pun ]);<span class="pln
logger<span class="pun .<span class="typ Write<span class="pun (<span class="str "Hello World"<span class="pun );[/code]


View the full article
 
Back
Top