Now, once a Business Logic and a Business Entity instance is
created via the BusinessLogicObjectFactory and the BusinessEntityObjectFactory classes
respectively, the exposed public properties of the BusinessService class, namely
BusinessLogic and BusinessEntity, are used to set the business logic and the
business entity instances created using the factory classes mentioned above.
This is done in the presentation layer or the user interface layer of the
application.
The following code snippet shows how you can use the
BusinessService class to decouple the business logic from the user interface
layer of your application using dependency injection technique.
Listing 11
IBusinessLogic iBusinessLogic = BLFactory.GetBLObject(BLObjectType.Employee);
IBusinessEntity iBusinessEntity = BEFactory.GetBEObject(BEObjectType.Employee);
//Code to populate the business entity instance with data
BusinessService businessService = new BusinessService();
businessService.BusinessLogic = iBusinessLogic;
businessService.BusinessEntity = iBusinessEntity;
businessService.Execute(OperationType.Create);
Note how the factory classes have been used to create the
business logic and the business entity instances respectively. The static
methods GetBLObject and GetBEObject accept references of the BLObjectType and
BEObjectType enumerators. These methods then check the corresponding values and
return business logic or business entity instances appropriately. Refer to the
code shown in the listing 7 and 8 above. You can create instances of the
DepartmentBO or the DepartmentDO by simply changing the binding as shown below.
Listing 12
IBusinessLogic iBusinessLogic = BLFactory.GetBLObject(BLObjectType.Department);
IBusinessEntity iBusinessEntity = BEFactory.GetBEObject(BEObjectType.Department);
//Code to populate the business entity instance with data
BusinessService businessService = new BusinessService();
businessService.BusinessLogic = iBusinessLogic;
businessService.BusinessEntity = iBusinessEntity;
businessService.Execute(OperationType.Create);
We can make the decoupling even more efficient (more loosely
coupled) using a XML file where we can store the binding information. The
binding information here implies the information that relates a user interface
to its business logic and business entity classes.
Then we can use the factory classes to create the business
logic or business entity instances as illustrated below.
Listing 13
IBusinessLogic iBusinessLogic = BLFactory.GetBLObject
(ConfigurationManager.GetBLObjectName(“U001”));
IBusinessEntity iBusinessEntity = BEFactory.GetBEObject
(ConfigurationManager.GetBEObjectName(“U001”));
//Code to populate the business entity instance with data
BusinessService businessService = new BusinessService();
businessService.BusinessLogic = iBusinessLogic;
businessService.BusinessEntity = iBusinessEntity;
businessService.Execute(OperationType.Create);
In the above code example, notice that we have used a class
called ConfigurationManager to read the binding information from a XML file.
The methods GetBLObjectName and GetBEObjectName return the names of the
business logic and the business entity classes respectively. These methods
accept the screen ID as parameters and use this ID to search and retrieve the
binding information from the XML file that contains it.
We are done! I leave it to the readers to design the XML
schema that relates a user interface to its corresponding business logic or
business entity class name. Then you have to implement a class that reads this
binding information; you can name it as ConfigurationManager.