How to register global services that rely on module services

Topics: Web Client Software Factory, User Forum
Oct 11, 2007 at 2:09 PM
I'm trying to add a global service that has service dependences to services that I would like to keep at the module level. I'm trying to do this with the following code

protected virtual void AddGlobalServices(IServiceCollection globalServices)
{
globalServices.AddNew<EventRegistrationService, IEventRegistrationService>();
}

protected virtual void AddModuleServices(IServiceCollection moduleServices)
{
moduleServices.Add<IContactRepository>(new AMG.Events.EventRegistration.Services.Persistence.SQL.ContactRepository("Events"));
moduleServices.Add<IReservationDetailsRepository>(new AMG.Events.EventRegistration.Services.Persistence.SQL.ReservationDetailsRepository("Events"));
}

I realized that this wouldn't work since the IServiceCollection that I'm registering the global service doesn't have references to the services in the module service collection. Is there any way to keep from having to register my repository services as global services for my global service to use them?

Thanks
Jonathan
Oct 15, 2007 at 6:46 PM
I am 95% sure you will need to register the dependencies in the Global Services collection, and that there is not a work around, other than having your globally exposed service use the local service classes directly, and not as service dependencies.

I'm not sure we considered service dependencies between services with different visibilities as a major scenario. I could easily see writing a local service with a dependency on a Global service. But doing it the other way around seems a bit odd. I can understand why you would want to do that, but there may be other options for the type of code-reuse you want to do here.

Michael Puleio - patterns & practices
Webhttp://msdn.microsoft.com/practices/
Bloghttp://blogs.msdn.com/mpuleio/
Oct 15, 2007 at 9:57 PM

MichaelPuleio wrote:
I am 95% sure you will need to register the dependencies in the Global Services collection, and that there is not a work around, other than having your globally exposed service use the local service classes directly, and not as service dependencies.

I'm not sure we considered service dependencies between services with different visibilities as a major scenario. I could easily see writing a local service with a dependency on a Global service. But doing it the other way around seems a bit odd. I can understand why you would want to do that, but there may be other options for the type of code-reuse you want to do here.

Michael Puleio - patterns & practices
Webhttp://msdn.microsoft.com/practices/
Bloghttp://blogs.msdn.com/mpuleio/


Michael,
Thanks for the info. I just downloaded the Composite Web Client Automation & noticed that in the module initializer, the controller is registered using container.RegisterTypeMapping<IControllerInterface, ControllerConcreteType>();

What's the difference between doing that & registering a service (other than the fact that you can't instantiate the instance to use directly)?
Oct 16, 2007 at 5:34 PM

kblooie wrote:
Michael,
Thanks for the info. I just downloaded the Composite Web Client Automation & noticed that in the module initializer, the controller is registered using container.RegisterTypeMapping<IControllerInterface, ControllerConcreteType>();
What's the difference between doing that & registering a service (other than the fact that you can't instantiate the instance to use directly)?


The Type Mapping is a new feature we have not talked much about. It is on my list of blog posts to do when Blaine lets me have some time to not work on shipping code.
The difference is that the type mapping will allow you to use interfaces only (if you choose). You can then map the interface to a concrete class in all modules or in each module. For example (and this is off the top of my head), if you have IMyLogger that is used everywhere, you can have MyErrorOnlyLogger enabled for all modules, except the one module that is causing problems, which uses MyVeryVerboseLogger instead. Since this can be changed via config, you can swap out on the fly.

I hope that helps a little bit.
Michael Puleio - patterns & practices
Webhttp://msdn.microsoft.com/practices/
Bloghttp://blogs.msdn.com/mpuleio/