Type Mapping vs Service Registration

Topics: Web Client Software Factory
Mar 22, 2008 at 5:52 PM
Hi, All! Consider, there is a class Foo which implements interface IFoo. I can register type mapping <IFoo, Foo> and use this interface in my properties or method parameters with CreateNew attribute, without mentioning the class Foo. But, if I register a service like services.AddNew<Foo, IFoo>(), isn't it a same effect as with type mapping? The only difference is that I should use ServiceDependency attribute instead of CreateNew. Am I right or wrong? What is concepually correct way of registering the class (through type mapping or service registering), whose interface is separated and the implementation is stateless?
Mar 24, 2008 at 12:55 AM
There is actually a huge difference in these two scenarios that will cause problems if you do not understand it.

A CWAB Service is a application wide singleton object. The same object is used by all classes that have a ServiceDependency on it.
The type mapping coupled with CreateNew will create a new instance of the object for each class it is injected into.

So, if you want the same instance of Foo used everywhere, make it a service and use a ServiceDependency to inject it.
If you want each Presenter, or other class to have their own instance of Foo, use the CreateNew attrribute to inject it.

You might want to review the docs a bit, as concepts like this are covered fairly well. It might take a few hours to skim the docs, but it is well worth the investment. (And considering how much time our team invested to get them right, I sure hope someone reads them. :-) )

Michael Puleio - patterns & practices
Mar 24, 2008 at 8:54 AM
Michael, first of all, thank you for the reply. It's my fault that my post gives you a motive to think that I don't understand even the basic things. Perhaps, it's because of my lack of experience in English :-). Really, I start work with any new product from reading it's documentation and only after that I read it's forums (WCSF is not an exception :-) ). I must mention that the WCSF's documentation and samples are of good quality and really helpful.
Returning to my question. Of course, I understand the difference between this scenarios in terms of what is physically happening when using CreateNew or ServiceDependency attributes (in fact, I am a little bit familiar with sources of WCSF :-) ). Let me give more concrete example. There is a OrderManagementRI application in WCSF's samples. It has a class OrdersService which implements IOrdersService interface. As the OrdersService class is stateless, it was registered in Services collection and is using in OrdersController with ServiceDependency attribute. As I see, I am able to remove IOrdersService from collection, register instead of it the type mapping <OrdersService, IOrdersService> and replace all ServiceDependency with CreateNew attributes (I am still understand, that this way leads to creating instance of OrdersService each time instead of using single instance registered in Services :-) ) In resume, is the type of instancing (single or multi) the only difference between two approaches? Is this the only reason (choice between using class as singleton or as multi instance) when deciding to register class as service or as type mapping?
The cause of my question is such. Our team develops an application. It's business layer mainly consists of so-called "Managers" (stateless classes that contains business logic). Earlier we used this managers in simple way, creating new instances in any place we need the manager:

CableSegmentManager segmentManager = new CableSegmentManager();

We decided to incorporate the WCSF in our application. Also, we have a strong need to separate manager's interfaces, so each manager class now implements corresponding IXxxManager interface. So, there is a dilemma: should we register all our managers as global services (they allow to do it, because all of them are stateless and could be used as singletons), or register a type mapping and continue to create new instances?