Change default constructor choosen

Sep 26, 2008 at 11:59 AM
Hi guys, I am really new to Unity. I didn't see this question resolved in the documention.

How I can change how unity choose the right constructor of my objects. I know it choose the one with most parameters, but I want it to choose the empty one. And I want to configure it at container level not in every single class. It is possible?


Thanks a lot.
Sep 26, 2008 at 2:22 PM
 Hi Broker,

I remembered reading something on this and found it in the docs  for you;  I copy/pasted the following from the documentation | Key Scenarios | Annotating Objects for the Constructor Injection.  

How Unity Resolves Target Constructors and Parameters

When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the "longest" with the same number of parameters), Unity will raise an exception.

public class MyObject
{

  public MyObject(SomeClassA objA, SomeClassB objB)
  {
    ...
  }

  [InjectionConstructor]
  public MyObject(DependentClassA depA, DependentClassB depB)
  {
    ...
  }

}

Sep 26, 2008 at 3:34 PM
I think I didnt explain well. I know that. But I dont want to mark every constructor on my software. I want to change this behaivour in Unity for all classes.

On Fri, Sep 26, 2008 at 4:22 PM, BillKrat <notifications@codeplex.com> wrote:

From: BillKrat

Hi Broker,

I remembered reading something on this and found it in the docs for you; I copy/pasted the following from the documentation | Key Scenarios | Annotating Objects for the Constructor Injection.

How Unity Resolves Target Constructors and Parameters

When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the "longest" with the same number of parameters), Unity will raise an exception.

public class MyObject
{

public MyObject(SomeClassA objA, SomeClassB objB)
{
...
}

[InjectionConstructor]
public MyObject(DependentClassA depA, DependentClassB depB)
{
...
}

}

Read the full discussion online.

To add a post to this discussion, reply to this email (unity@discussions.codeplex.com)

To start a new discussion for this project, email unity@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Sep 26, 2008 at 6:09 PM
You will need to write a container extension and replace the default ConstructorSelectoPolicy. It's not hard, but you'll need to look into the internals. Take a look at the DefaultBehaviorExtension to see where it's added. The existing code is in DefaultUnityConstructorSelectionPolicy class.

Sep 26, 2008 at 9:09 PM
I didnt know ObjectBuilder2. I tried to see what you said but I feel it is more than I can do. :(

May you write some example ?

Thanks in advance

On Fri, Sep 26, 2008 at 8:09 PM, ctavares <notifications@codeplex.com> wrote:

From: ctavares

You will need to write a container extension and replace the default ConstructorSelectoPolicy. It's not hard, but you'll need to look into the internals. Take a look at the DefaultBehaviorExtension to see where it's added. The existing code is in DefaultUnityConstructorSelectionPolicy class.

Read the full discussion online.

To add a post to this discussion, reply to this email (unity@discussions.codeplex.com)

To start a new discussion for this project, email unity@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Sep 27, 2008 at 2:35 PM
Edited Sep 27, 2008 at 2:55 PM
Hi Broker, I wrote you a demo application that implements an EmptyConstructorSelectorPolicy.  I can see great value in what your looking for, particularly if someone wants to refactor an existing application to use Unity.  COMMUNITY NOTE:  I would appreciate code review comments, improvements or potential issues.

Demo is available => http://www.Global-Webnet.net/Unity/EmptyConstructorDemo.zip 

Note:  the demo is large; for time constraints (I need to finish a client utility so I can back to my open source project) I used a new Unity 1.1 Source solution and added the following three projects to Unity.sln:

Demo  <= you'll want to run this
UnityContrib.Library
UnityContrib.Library.Tests

To use this in your application simply provide a reference to UnityContrib.Library and add the following line to your container configuration:

   container.AddNewExtension<EmptyConstructorExtension>();

When I wrap up my current client requirements (hopefully on Monday) I'll document the demo and blog on it.

EDITED:  The areas you'll be most interested in are the Program.cs and Service\ServiceClass.cs files.    The only changes made to the actual Unity source code was to change Unit Test classes from internal to public so that I could reuse them in my unit test.

Sep 28, 2008 at 12:18 AM
It's great, I will look further in your demo tomorrow.

Thanks a lot !

On Sat, Sep 27, 2008 at 4:35 PM, BillKrat <notifications@codeplex.com> wrote:

From: BillKrat

Hi Broker, I wrote you a demo application that implements an EmptyConstructorSelectorPolicy. I can see great value in what your looking for, particularly if someone wants to refactor an existing application to use Unity. COMMUNITY NOTE: I would appreciate code review comments, improvements or potential issues.

Demo is available => http://www.Global-Webnet.net/Unity/EmptyConstructorDemo.zip

Note: the demo is large; for time constraints (I need to finish a client utility so I can back to my open source project) I used a new Unity 1.1 Source solution and added the following three projects to Unity.sln:

Demo <= you'll want to run this
UnityContrib.Library
UnityContrib.Library.Tests

To use this in your application simply provide a reference to UnityContrib.Library and add the following line to your container configuration:

container.AddNewExtension();

When I wrap up my current client requirements (hopefully on Monday) I'll document the demo and blog on it HERE



Read the full discussion online.

To add a post to this discussion, reply to this email (unity@discussions.codeplex.com)

To start a new discussion for this project, email unity@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Mar 5, 2010 at 1:07 AM
BillKrat wrote:
Hi Broker, I wrote you a demo application that implements an EmptyConstructorSelectorPolicy.  I can see great value in what your looking for, particularly if someone wants to refactor an existing application to use Unity.  COMMUNITY NOTE:  I would appreciate code review comments, improvements or potential issues.

Demo is available => http://www.Global-Webnet.net/Unity/EmptyConstructorDemo.zip 



Broken link.