Note that there are some explanatory texts on larger screens.

plurals
  1. POinject behaviour on manual created objects
    text
    copied!<p>I'm working with autofac. So far i resolve all my dependencies with constructor injection.</p> <p>There is a case where i get stuck:</p> <p>Considering the given customer class:</p> <pre><code>public class Customer : ICustomer { public string Name { get; set; } private int ExternId { get; set; } public IExternalIdProvider externalIdProvider { get; set; } public Customer() { this.externalIdProvider = new ConcreteIdProvider(this); } public BevorSave() { this.ExternId = externalIdProvider.GetNextId(); } } </code></pre> <p>In Order to create a new customer object based on a request or gui action. I use the new Operator. However - There is an IdProvider within the CustomerClass i want to inject. (as property).</p> <p>If the Customer would be resolved by the ioC Container i would use a configuration like:</p> <pre><code>builder.RegisterType&lt;ConcreteIdProvider&gt;().As&lt;IExternalIdProvider&gt;(); builder.RegisterType&lt;Customer&gt;().As&lt;ICustomer&gt;() .OnActivated(ae =&gt; { IExternalIdProvider idProvider = ae.Context.Resolve&lt;IExternalIdProvider&gt;(TypedParameter.From(ae.Instance)); ae.Instance.externalIdProvider = idProvider; }); </code></pre> <p>My Question is: How can I inject the behaviour of the ExternalIdProvider in the Customer? (using autofac)</p> <p>This article shows a sample, how this would be done with a service locator: <a href="http://blogs.msdn.com/b/simonince/archive/2008/06/30/dependency-injection-is-dead.aspx" rel="nofollow">http://blogs.msdn.com/b/simonince/archive/2008/06/30/dependency-injection-is-dead.aspx</a> </p> <p>Thanks for your help.</p>
 

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