configure constructor param injection without decorators?

Nov 25, 2008 at 2:27 PM
How would I add constructor injection to the xml config to remove [InjectionConstructor] from classes?
Nov 25, 2008 at 2:53 PM
Add a <constructor> element under the type you want to configure a constructor for.  

<type type="IPerson" mapTo="Employee" name="BusinessObjects">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                     Microsoft.Practices.Unity.Configuration">
            <constructor>
              <param name="name" parameterType="string">
                <value value="EmployeeOne"/>
              </param>
              <param name="address" parameterType="string">
                <dependency />
              </param>
            </constructor>
    </typeConfig>
</type>


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 25, 2008 at 3:09 PM
Thanks. I also have a basepage<T>  used by the .aspx pages. The basepage has properties defined currently using [Dependency] attributes. Can I use the xml to inject into the basepage<T>? How would I do this?
Nov 25, 2008 at 3:20 PM
Edited Nov 25, 2008 at 3:26 PM
Yes, check out this post by David Hayden for a complete sample - http://blogs.microsoft.co.il/blogs/gilf/archive/2008/07/25/working-with-generic-types-in-unity-configuration-section.aspx



Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 25, 2008 at 3:57 PM
Edited Nov 25, 2008 at 3:59 PM
Thanks.

So if I don't have an interface for BasePage<T>, then would the mapping look like? : (doesn't work)

<

typeAlias alias="BasePage" type="Web.App_Code.BasePage`1, Web" />

 

 

 

<type type="BasePage" mapTo="BasePage" name="PageBase">

 

 

 <

typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">

 

 <

property name="Prop1" propertyType="Core.IProp1, Core"/>

 

 <

property name="Prop2" propertyType="Core.IProp2, Core"/>

 

 <p

roperty name="Prop3" propertyType="Core.IProp3, Core"/>

 

 </

typeConfig>

 

 </

type>

 

 

 

Nov 25, 2008 at 9:44 PM
Injection configuration is done on concrete types, not inherited. So you'd need to do that configuration on the classes that derive from BasePage<T>, not on BasePage<T> itself.

Nov 26, 2008 at 12:27 AM
Chris is right.  What's your intention of doing that anyway?

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 26, 2008 at 2:07 AM
Because BasePage holds properties for the .aspx pages.
Nov 26, 2008 at 2:36 AM
Sorry, what I meant to ask was if you want to have the same property injection for all classes that derive form BasePage<T>.  Ok, I get it.  Are you still having any trouble?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 26, 2008 at 2:00 PM
Edited Nov 26, 2008 at 2:01 PM
Exactly, all properties would be injected into derived pages.
Yes I'm still unable to inject the properties without [Dependency] attributes. I just get a null reference exception on the property usage. 

<

typeAlias alias="BasePage" type="Store.Web.BasePage`1, Store.Web" />

 

<

type type="BasePage" mapTo="BasePage" name="PageBase">

 

<

typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">

 

<

property name="Repository" propertyType="Core.Interfaces.IRepository, Core"/>

 

</

typeConfig>

 

</

type>

 

Nov 27, 2008 at 2:15 AM
As previously said, injection should be done on concrete types. In your case, you shouldn't be doing it on the BasePage<T> since it's a generic type. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 27, 2008 at 5:51 AM
Not quite, Sarah. He should not be configuring BasePage<T> because it's the base class, not because it's a generic type.

The injection needs to be configured on each type that you're going to resolve, so in that case it'd be on the types that inherit from BasePage<T>.

Nov 27, 2008 at 6:09 AM
Oh, yes Chris, my mistake.  Thanks.