Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Autofac does allow you to inject properties on component activation. For your purposes, there are a couple of ways to do it.</p> <p>First, you have to figure out how you want to construct the DataContext. You can register the construction of it in a lambda, you can register a specific instance, or you can register the type with parameters.</p> <pre><code>var builder = new ContainerBuilder(); // You can register an instance... var context = new DataContext(connectionString); builder.RegisterInstance(context).As&lt;DataContext&gt;(); // OR you can register construction in a lambda... builder.Register(c =&gt; new DataContext(connectionString)).As&lt;DataContext&gt;(); // OR you can register the type with constructor parameters. builder.RegisterType&lt;DataContext&gt;() .WithParameter( new NamedParameter("fileOrServerOrConnection", connectionString)); </code></pre> <p>Once you have that, you can register your types and set the properties to be autowired:</p> <pre><code>builder.RegisterType&lt;Person&gt;().PropertiesAutowired(); </code></pre> <p>What that will do is after the Person object is created, any properties that can be set by Autofac will be. In this case, the DataContext will be resolved and set. Just make sure when you register your types deriving from "Entity" (and you do need to register all of them or Autofac won't have them in its component registry to resolve) that you add "PropertiesAutowired" at the end.</p> <p>Something similar to what was posted earlier by @Chingiz:</p> <pre><code>builder.RegisterAssemblyTypes(typeof(Person).Assembly) .Where(t =&gt; t.IsAssignableFrom(typeof(Entity))) .PropertiesAutowired(); </code></pre> <p>If you need more control over it, you may need to check out the Autofac wiki on <a href="http://code.google.com/p/autofac/wiki/PropertyInjection">property injection</a> and <a href="http://code.google.com/p/autofac/wiki/OnActivatingActivated">component activation events</a> for some ideas/reference.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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