Adding Services

Topics: CAB & Smart Client Software Factory
Feb 28, 2007 at 2:52 PM
I am creating a Business Module with a Service, that needs to beaccessible by Views in other Business Modules.

1. I have a created a new Business Module(BM2) and created the service IMyService,

2. I added the same service to WorkItem in the ModuleController of BM2 like this
WorkItem.Services.AddNew<MyService, IMyService>();

3. I added the following in the app.config
<!-- Other services -->
<add serviceType="POCSCFBMServices.Services.IMyService"

Now, I need to access the above service from my other BusinessModule(BM1) using loose coupling. I do not want to add reference to my BM2 module.

Can you please let me know how to do it.
Feb 28, 2007 at 2:57 PM

The easiest way to access this service by both business module it's to register it in RootWorkItem.Services Collection or in an upper level workitem in the workItem hierarchy wich contains both Business Module.

Also, if this service will be available for more business modules, you should encapsulate that service in a foundational module.

I hope it will help you.
Feb 28, 2007 at 3:09 PM

bsatyen wrote:
I do not want to add reference to my BM2 module.

BM1 is going to need a reference to something or else it will never be able to use the Service. So it needs to know about the interface to IMyService. This is why you will probably want to put all of your interface declarations in a separate assembly.

Once you do that then you can have BM2 instantiate the service. But it needs to add it to the RootWorkItem, or else other modules will not know about it.

So, suppose you were creating this Service in BM2 in the ModuleController. You could do it this way:

WorkItem.RootWorkItem.Services.AddNew<MyService, IMyService>();

Your other Modules should then be able to access it via dependency injection with the ServiceDependency attribute.

Feb 28, 2007 at 3:11 PM
My App structure is like this


I need to access the service in BM2 from a view in BM1.

Can you let me know where to add it.
Feb 28, 2007 at 3:35 PM
I added my Service to the root work item in BM2.
Can you please eloborate on how to access this using dependency injection with ServiceDependency attribute..
Feb 28, 2007 at 5:13 PM
Edited Feb 28, 2007 at 5:14 PM
Hi, as Chris Holmes said you need to add a reference in BM1, because BM1 needs to know about the interface IMyService, so when you create the BM2 Module keep checked the “Create an interface library for this module” option.

Then you will have the BM2.Module project and the BM2.Interface project.

In the folder Services in the BM2.Interface project you put the IMyService interface.
In the folder Services in the BM2.Module project you implement IMyService interface. Then you either add the service to the root work item or add it via configuration as pointed on 3 (but you should do one or the other)

In BM1.Module you add a reference to BM2.Interface project, and in the presenter of your view (not in your view) you have to have something like this:

public MyPresenter ([ServiceDependency] IMyService myService)
      _myService = myService;

The BankBranchWorkbench reference implementation uses this approach. See class PurchaseCDViewPresenter.cs (It uses ICustomerAccountService interface)

Let me know if this helps,

Ezequiel Jadib
Feb 28, 2007 at 6:08 PM
I do not want to add reference to BM2 in BM1.
How do I go about this?
Feb 28, 2007 at 6:35 PM

You don’t need to add a reference to BM1 module but a reference to BM1 interface class library project. Is the “contract” of the module, the services that module exposes.
Now if you don’t want to do that, you can have the interface at the root level interface project which is Infrastructure.Interface. everybody in the solution “see” that project.

Let me know if this helps,

Ezequiel Jadib
Feb 28, 2007 at 8:17 PM
I did the same way.
I am getting the following exception.

throw new ServiceMissingException(serviceType);

I am calling the BM2Service from my BM1View.
I added the following in BM2ModuleController.cs

WorkItem.RootWorkItem.Services.AddNew<MyService, IMyService>();

Am I wrong?

Mar 1, 2007 at 2:26 PM
Hi bsatyen,

It is OK to add the new service in the BM2ModuleController. That exception you describe appears when the workitem Get method on the service collection fails to locate the appropriate service. So, since you are adding the service correctly (you are adding it to the RootWorkItem) could you please send the snippet of code where you are making use of the service.

One possibility is that you are trying to use the service before it is getting loaded in the service collection. Could you please ensure that this is not the case? (a couple of breakpoints will do the trick, one for the Add and one for the Get) or try swapping the order of the modules in the ProfileCatalog file (beware of the dependencies).

Let me know if this helps,

Federico Delgado
Mar 1, 2007 at 7:58 PM
This is my code in the BM1 Presenter

private IMyBusService _service = null;
public TrackOrderPresenter(ServiceDependencyIMyBusService myservice)
_service = myservice;
public void ShowPresenterMsg(string message)
In my View,
I have the following code
private void button1_Click(object sender, EventArgs e)
_presenter.ShowPresenterMsg("Hello Service");


Mar 1, 2007 at 9:29 PM
I got it.
Thanks everyone for the help.