.net - Unity BuildUp without DependencyAttribute -
i've got third party library returning class instace. it's out of cotrol want use unity
inyect in public properties. can not add dependecyattribute
properties because class not partial nor inheritalbe, wondering if can use unity
xml configuration inyection.
is possible use unity's
xml configuration configure build dependencies? if answer yes. how configure xml it?
i've tried:
<register type="iservice" mapto="service"> <!--service has public constructor--> </register> <!--thirdpartyobj has no interface , no public constructor--> <register type="thirdpartyobj " mapto="thirdpartyobj"> <!-- inject property "service" --> <property name="service" /> //type of property iservice </register>
this config work if unity resolve
thidpartyobj
buy not working in builup
(null reference in service property) , can not resolve
thirdpartyobj
because has no public constructor.
simple example of want archieve:
iunitycontainer uc = new unitycontainer() //create container container.loadconfiguration() // load xml config thirdpartyobj foo = thirdpartylibrary.getfooinstace() //get third party instance container.buildup(foo.gettype, foo) //inyect dependencies console.writeline(foo.service.getservicemessage)
based on posted code, there shouldn't problem.
i created 2 projects: console application , class library called thirdpartylibrary.
the thirdpartylibrary consists of following (extrapolated posted code):
namespace thirdpartylibrary { public interface iservice { string getservicemessage { get; } } public class service : iservice { public service() { } public string getservicemessage { { return "the message!"; } } } public static class thirdpartylibrary { public static thirdpartyobj getfooinstance() { return new thirdpartyobj(); } } public interface ithirdpartyobj { } public class thirdpartyobj : ithirdpartyobj { internal thirdpartyobj() { } public iservice service { get; set; } } }
the xml configuration looks this:
<?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"> <container> <register type="thirdpartylibrary.iservice, thirdpartylibrary" mapto="thirdpartylibrary.service, thirdpartylibrary"> </register> <register type="thirdpartylibrary.ithirdpartyobj, thirdpartylibrary" mapto="thirdpartylibrary.thirdpartyobj, thirdpartylibrary"> <!-- inject property "service" --> <property name="service" /> </register> </container> </unity> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5"/> </startup> </configuration>
and console application is:
class program { static void main(string[] args) { iunitycontainer container = new unitycontainer(); //create container container.loadconfiguration(); // load xml config thirdpartyobj foo = thirdpartylibrary.thirdpartylibrary.getfooinstance(); //get third party instance container.buildup(foo.gettype(), foo); //inyect dependencies console.writeline(foo.service.getservicemessage); } }
this works fine , outputs the message!
expected. perhaps, exact scenario not same posted (i assume fictitious) example or else not configured properly?
Comments
Post a Comment