David
David

Reputation: 33

@Enterprise Library Unity property inject

I'm a fresher with the enterprise library. I want to ask some questions and any help will be appreciate.

1、 How to deploy inject an instance property.

public class MyObject
{
   public MyObject(string Title)
   {
      ///...
   }
   public MyObject(InjectObject injectObject)
   {
      ///...
   }
   public InjectObject InjectObject{get;set;}
   public List<string> MyList {get;set;}
   public string Title {get;set;}
} 

Now I know how to inject the default value to the title property. But how to do 
with 
the InjectObject and the MyList.

<register type="IMyObject" mapTo="MyObject">
   <property name="Title" value="MyFirstObject">
   </property>
   //But how to assign or instance the InjectObject and the MyList
</register>
But how to assign or instance the InjectObject and the MyList

 <register type="IMyObject" MapTo=“MyObject”>
  <constructor>
     <param type="string" name="title" value="MyFirstObject"/>
  </constructor>

2、 How to deliver a class instance to the constructor and I know how to assign a string value to the constructor. But how to transfer a class instance.

How can I assign a class instance to the constructor and How if i have two constructor method to deploy.

Thank you for your help. Best Regards.

Daivd

Upvotes: 1

Views: 345

Answers (1)

Rory Primrose
Rory Primrose

Reputation: 633

Firstly, prefer constructor injection over property injection.

To inject the type to the constructor, you use the <dependency [name=""] /> attribute.

For example:

<register type="IMyObject" MapTo=“MyObject”>
    <constructor>
        <param name="injectObject">
            <dependency />
        </param>
    </constructor>
<register>

<register type="InjectObject" />

UPDATE:

To add an array as the injection value you need to configure something like this:

<param name="parmName">  
    <array>  
        <value value="firstValue" />  
        <dependency />  
        <value value="some other value" />  
    </array>  
</param>  

Check out the Unity configure schema for all the detail on how to do this.

Upvotes: 1

Related Questions