Ragrding InjectionConstructor and CreateNew attributes

Topics: Web Client Software Factory
Jan 4, 2007 at 12:42 PM

I have small question but you may think it's silly one. In the Code-behind file (WebPage) we are using Property get/set and in the Module Controller we are using Constructor to inject the object values in the given examples. Is there any specific reason to do this?
Why I am asking you is, if I use instead of the above, i want to use other way. (ie., in web page code behine file Constructor and in controller property) But is is giving compile time error, why it is happening this way?

Can anyone exmplain me?

Jan 4, 2007 at 1:55 PM
I am looking the code how the ObjectBuilder is calling the Injection CToR/Methods/properties. Then i came across the above question.

Jan 4, 2007 at 3:35 PM
The main difference between constructor and setter injection is that when you use constructor injection you are declaring dependencies in a clear and explicit way that is validated at compile time (in fact, I think that having parameters in constructors is the natural way of expressing object dependencies in OOP). This means that you can be sure that after the object is constructed, you have a valid instance ready to be used. This is the reason why we use constructor injection everywhere it's possible.

In the case of an ASP.NET Page we cannot use constructor injection because we don't control the creation process of the page - the ASP.NET runtime creates the page using the default constructor. That's why you get an error if you add a non-default constructor to your page and that's the reason why we use setter injection in ASP.NET Page objects.

Mariano Szklanny
Jan 4, 2007 at 3:45 PM
I agree with your answer, but what about the ModuleController. Here if I declare Property injection dependency instead of CToR, ServiceMissingException is raising? What could be the reason?
I am thinking that, for services we have to declare ServiceDependency attribute, but through Property we can't declare, so, we are using CToR injection here. Am I correct?
Jan 4, 2007 at 11:13 PM
The ServiceDependency attribute (the same applies to CreateNew) can be used for setter injection as well as for constructor injection.

If you get a ServiceMissingException when using ServiceDependency, is because no service has been registered in the module CompositionContainer with the type of the property. Please make sure the service you registered is of the same type of the property or, if you registered the service with a different type (using the overload IServiceCollection.AddNew<T>(type registerAs)) make sure the property is of that type.

Also take into account that services registered in a module CompositionContainer (those registered in the AddModuleServices method in the module controller) cannot be shared with other modules (so you will get a ServiceMissingException if you try to have it injected in a different module). To share services, you have to register the service in the root CompositionContainer (use the AddGlobalServices method instead).

Mariano Szklanny