fail regist the instance with PerResolveLifetimeManager in config file

Jun 2, 2011 at 10:31 AM

Hi All,

I want to regist the instance with PerResolveLifetimeManager and I'm successfully with the run-time code config.

But I'm fail to regist the instance with lifetime PerResolve. It seems The instance will recreate again.

Here is my code snippet:

public class CustomView

{

      public IPresent Present{get;set;}

}

 

public class CustomPresent

{

    public IView View{get;set;}

    public CustomPresent(IView view)

    {

        this.View=view;

     }

}

Config:

<register type=“IPresent” mapTo=“CustomPresent”>

     <constructor>

          <param name="view" type="IView"/>

     </constructor>

</register>

 

<register type="IView" mapTo=“CustomView”>

      <lifetime type="PerResolve"/>

      <property name="Present" dependenceType="CustomePresent"/>

</register>

 

//--

var view=container.Resolve<IView>();// The exception will be throw out. For the IView is a interface and can't construct.

So It seems when the present(In the CustomView) init, the CustomView is init again.

Is there any mistake for me?  Anyhelp will be appreciate.

 

Best Regard.

Jun 2, 2011 at 12:57 PM

Hi,

Is this already the complete code you're referring to? Could you post your complete code here for us to further investigate them.

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 3, 2011 at 4:05 AM

Hi,

Thank you for your response. Here is My Sample Code 

Sample Code.  and I can't resolve the DefaultView for the exception:

The current type, DVDemo.EL.WebApp.View.Interface.IView, is an interface and cannot be constructed. Are you missing a type mapping?

The follows is my config.

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

     <alias alias="VTest" type="DVDemo.EL.WebApp.View.VTest,DVDemo.EL.WebApp"/>

    <alias alias="IPresent" type="DVDemo.EL.WebApp.Present.Interface.IPresent, DVDemo.EL.WebApp"/>

    <alias alias="DefaultPresent" type="DVDemo.EL.WebApp.Present.DefaultPresent, DVDemo.EL.WebApp"/>

    <alias alias="IView" type="DVDemo.EL.WebApp.View.Interface.IView, DVDemo.EL.WebApp"/>

    <alias alias="DefaultView" type="DVDemo.EL.WebApp.View.DefaultView, DVDemo.EL.WebApp"/>
    <container name="MVP">

      <register type="VTest" mapTo="VTest" name="VTest">

        <property name="Title" value="Title is DVTest"></property>

      </register>
      <register type="IPresent" mapTo="DefaultPresent" name="DefaultPresent">

        <constructor>          

         <param name="view" type="IView"/>

        </constructor>      

     </register>

     <register type="IView" mapTo="DefaultView" name="DefaultView">

        <lifetime type="perresolve"/>

        <property name="Present" dependencyType="DefaultPresent" />

      </register>
    </container>

  </unity>

 

Thank you for you help. Best Regards.


Jun 3, 2011 at 4:05 AM
Edited Jun 3, 2011 at 4:23 AM

: )

Jun 3, 2011 at 5:16 AM

Hi,

You need to specify how will you resolve the IView parameter on DefaultPresent and the IPresent property on DefaultView. Since they are already registered on the container, you just need to specify their registration names on the dependencyName so you can be able to resolve them. Refer to the code below:

<register type="IPresent" mapTo="DefaultPresent" name="DefaultPresent">
        <constructor>
          <!--<param name="view" type="IView"/>-->
          <param name="view" dependencyName="DefaultView"/>
        </constructor>
      </register>
      
      <register type="IView" mapTo="DefaultView" name="DefaultView">
        <lifetime type="perresolve"/>
        <!--<property name="Present" dependencyType="DefaultPresent" />-->
        <property name="Present" dependencyName="DefaultPresent" />
      </register>

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com:

Jun 3, 2011 at 6:26 AM

Hi,

Thank you for your advice. I think the programmatic configuration is more rude than the XML Config. : D