Registering and Using Business Module Services in the Web Client Software Factory

Registering and Using Business Module Services in the Web Client Software Factory

by David Hayden ( Florida .NET Developer ), Filed: Web Client Software Factory

 

I presented the Web Client Software Factory (WCSF) at the South Florida Code Camp this weekend. Attendance was so large that I ended up moving the presentation to a bigger room to accommodate more people. The presentation went really well based on the feedback. Thanks to all of you who attended as I enjoyed the opportunity to present the topic. There was a lot of good dialog.

After the presentation, a few developers wanted to better understand dependency injection and programming to interfaces / abstract classes that I talked a bit about in the presentation. After a bit of discussion I think they feel much more confident with the idea, but I thought I would put together a quick example of how you might use these ideas in the Web Client Software Factory.

 

Business Modules

As I discussed in the presentation, WCSF has this concept of Business Modules. Business Modules are associated with web pages. My belief is that the underlying guidance behind the Business Modules is to try an help the developer put all business logic associated with the web pages in the business module. If you can keep your web pages “business logic free,” your application becomes independent of the presentation layer and easier to test, and you can better reuse and maintain the business logic.

On the other hand, when you add business logic to your web pages, you often end up duplicating that same business logic in several pages that need the same functionality. This causes a maintenance nightmare and is considered a really bad practice :)

 

Module Services

Business logic specific to the web pages can be added to the business module they are associated with in the form of Module Services. You can add the services directly into the business module and register them in the ModuleInitializer Class that is in every Business Module. By default, the Web Client Software Factory has the following method inside the ModuleInitializer Class that allows you to register any module services:

 

protected virtual void AddModuleServices
(IServiceCollection moduleServices) {
// Add Module Services here... }

 

Registering Module Services as Interfaces for Loose-Coupling

To keep your application a little less dependent on the actual classes that implement your module services, one option is to register your module services as Interfaces. Hence if you create a CustomerDataAccess Class in your Customers Business Module, you may want to have it implement an ICustomerDataAccess Interface:

 

public class CustomerDataAccess : ICustomerDataAccess
{
    // ...
}

 

and register it for use by client code wanting the Interface:

 

protected virtual void AddModuleServices
(IServiceCollection moduleServices) { moduleServices.AddNew
<CustomerDataAccess,
ICustomerDataAccess
>(); }

 

And then everywhere in your business module where you have a dependency on CustomerDataAccess, just ask for it by ICustomerDataAccess instead.

 

public class CustomersController
{
    private ICustomerDataAccess _customerDataAccess;

    public CustomersController([ServiceDependency]
ICustomerDataAccess customerDataAccess) { _customerDataAccess
= customerDataAccess; } public void InsertCustomer(Customer customer) { _customerDataAccess.InsertCustomer(customer); } }

 

The benefit here is that I can come along and create a new CustomerDataAccess2 Class:

 

public class CustomerDataAccess2 : ICustomerDataAccess
{
    // ...
}

 

and then register it in my CustomersModuleInitializer Class as a replacement to the original CustomerDataAccess Class as such:

 

protected virtual void AddModuleServices
(IServiceCollection moduleServices) { moduleServices.AddNew
<CustomerDataAccess2,
ICustomerDataAccess
>(); }

 

I wouldn't have to change any code anywhere else because all classes are still consuming the same ICustomerDataAccess Inteface.

 

public class CustomersController
{
    private ICustomerDataAccess _customerDataAccess;

    public CustomersController([ServiceDependency]
ICustomerDataAccess customerDataAccess) { _customerDataAccess
= customerDataAccess; } public void InsertCustomer(Customer customer) { _customerDataAccess.InsertCustomer(customer); } }

 

Here is a graphic image that might help one visualize the example better:

 

Customers Business Module

 

 

Conclusion

Hopefully that helps a bit more. Great discussions on using proven practices. I look forward to presenting the same presentation at the Orlando Code Camp in a few weeks.

by David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory

 

posted on Monday, February 12, 2007 12:07 AM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices