Note that there are some explanatory texts on larger screens.

plurals
  1. POResolve externally created object instance with no public constructor
    primarykey
    data
    text
    <p>Using the adapter pattern, combined with IoC (specificly Unity), I would like to create a new instance of a object of which the properties point back to the adaptee's propeties (basicly mapping the adaptee to a target object).</p> <p>As a example I have the following class structures:</p> <pre><code>public class Adaptee { private Adaptee() { } public int MyProperty { get; set; } public static Adaptee New() { return new Adaptee(); } } public class Target { public int MyProperty { get; set; } } public class Adapter : Target { public Adapter(Adaptee adaptee) { this.MyProperty = adaptee.MyProperty; } } public class MyTestClass { public Target MyTarget { get; set; } } </code></pre> <p>The problem is that the Adaptee is not under my control and it has no public constructor, hence the use of the adapter. So Adaptee gets created as follow</p> <pre><code>Adaptee adaptee = Adaptee.New(); adaptee.MyProperty = 5; </code></pre> <p>In the actual code the above code would be executed in a externally controlled assembly and then passed to the following code:</p> <pre><code>using (UnityContainer container = new UnityContainer()) { container .RegisterType&lt;MyTestClass&gt;( new InjectionProperty("MyTarget")); container.RegisterInstance&lt;Adaptee&gt;(adaptee, new ExternallyControlledLifetimeManager()); MyTestClass myTestClass = container.Resolve&lt;MyTestClass&gt;(); } </code></pre> <p>I would like the execution of the code to result in the following:</p> <pre><code>Debug.Assert(myTestClass != null); Debug.Assert(myTestClass.MyTarget != null); Debug.Assert(myTestClass.MyTarget.MyProperty == adaptee.MyProperty); </code></pre> <p>This means that a resolved instance of Adapter should be injected into the MyTarget property of the myTestClass instance. The instance of Adapter should have been created with externally created instance of Adaptee.</p> <p>The following code:</p> <pre><code>container .RegisterType&lt;MyTestClass&gt;( new InjectionProperty("MyTarget")); </code></pre> <p>should actually result in this:</p> <pre><code>container .RegisterType&lt;MyTestClass&gt;( new InjectionProperty("MyTarget", container.Resolve&lt;Adapter&gt;())); </code></pre> <p>This does not seem to be possible, since no instance of Adapter exist yet and it has a private constructor.</p> <p>Should I use interception or something to achive this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload