InjectionMethod: When does it occur?

Sep 5, 2008 at 7:18 PM
What consideration should I be aware of when implementing InjectionMethod? I understand what is in the documentation:

When to Use Method Call Injection

You should consider using method call injection in the following situations:

You want to instantiate dependent objects automatically when your instantiate the parent object.
You want a simple approach that makes it easy to see in the code what the dependencies are for each class.
The parent object requires a large number of constructors that forward to each other, making debugging and maintenance difficult.
The parent object constructors require a large number of parameters, especially if they are of similar types and the only way to identify them is by position.
You want to hide the dependent objects by not exposing them as properties.
You want to control which objects are injected by editing the code of the dependent object instead of the parent object or application.

Here's my simple sample:

//Bootstrapper
protected override void ConfigureContainer()
{
  base.ConfigureContainer();

  Container.Resolve<Sample>();
}

// Sample Class
public class Sample
{
 public void GetInjectedValue(IRegionManager manager)
 {
   string message = "My value is of type {0}";
   MessageBox.Show(string.Format(message, manager.GetType()));
 }
}


My GetInjectedValue in the Sample class does not fire.: why?

Thanks in advance,

Andres Olivares

Sep 8, 2008 at 4:50 PM

Hi

 

You must apply the [InjectionMethod] attribute to your GetInjectedValue method so it is called when the Unity container performs the injection.

 

using Microsoft.Practices.Unity;

public class Sample

{

[InjectionMethod]

public void GetInjectedValue(IRegionManager manager)

{

string message = "My value is of type {0}";

MessageBox.Show(string.Format(message, manager.GetType()));

}

}

 

Another approach is to receive all the class dependencies in the constructor. In this case it is not necessary to add an attribute to the constructor, because it will be automatically injected.

 

public class Sample

{

public Sample(IRegionManager manager)

{

string message = "My value is of type {0}";

MessageBox.Show(string.Format(message, manager.GetType()));

}

}

 

Please, let me know if it helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti
Sep 10, 2008 at 6:57 PM
mconverti,

Thanks.
Sep 17, 2008 at 8:34 PM
Edited Sep 17, 2008 at 8:35 PM
When you say "In this case it is not necessary to add an attribute to the constructor, because it will be automatically injected.", what does automatically actually mean?  Does it mean that when one attempts to create an instance of class Sample, they do not have to pass an instance of IRegionManager into the constructor?   Compiler will not allow that.



Sep 17, 2008 at 9:37 PM

Hi,

 

To get the dependencies injected via de the constructor of the class, you must create the instance with the container, as follows:

Sample mySample = _container.Resolve<Sample>();

 

Ignacio Baumann Fonay

http://blogs.southworks.net/ibaumann/

Sep 18, 2008 at 11:01 AM
makaveli_0000,

Correct, Ignacio Baumann. To elaborate further, object resolution constructs your object with the proper constructor based on its construction policies/strategies and scans your object to inject property dependencies, as well as call injection methods. At design time your object can have as many constructors as it needs to have. The constructors that rely on dependent objects that will be registered at that time will be injected by the container.

As Ignacio Baumann pointed out, construction of the Sample object, in the example above, would invoke the constructor with dependencies. When the Sample instance is created it would contain an instance of RegionManager as a parameter of its constructor. Looking further into the UnityBootstrapper code, you'll find code something like:

container.RegisterTypeIfMissing<IRegionManager, RegionManager>();

To learn more about all this, grab the documentation:

Composite Application Guidance
http://www.codeplex.com/CompositeWPF/Release/ProjectReleases.aspx?ReleaseId=14982

Unity
http://www.codeplex.com/unity/Release/ProjectReleases.aspx?ReleaseId=13598

Hope this helps,

Andres Olivares