How-To: Business Components
This walkthrough will get you started on using the Motion: Business Component Template Generator to create business components (BC) for your layered application. Before beginning this guide, please make sure you have have completed the
How-To: Create Data Access Components walkthrough. Also, please make sure that you have successfully built/compiled your
Data and Business.Entities projects before continuing with this guide.
- In Solution Explorer, right-click on your Business
project i.e. ExpenseSample.Business and click Add Business Components…
- This launches the Motion: Business Component Template Generator. Take some time to familiarize Motion’s user interface.
- Enter a Component name for your business component. If you have previously defined any business components, they will be available in the dropdown list. By default, business components are given the “Component” suffix. Take note that Motion
does not intelligently detect your business components if you have mix suffixes.
- Click Load to load a Data access component assembly (.dll). If you have built/compiled your
Data and Business Entities projects earlier, you will be able to view the assemblies here. Otherwise, Close Motion and return to Visual Studio 2010 to build your projects.
The guidance will build your assemblies to a single bin folder located under your solution. All assemblies other than those that belong to web projects will be targeted to this folder.
Double-click the Data.dll i.e. ExpenseSample.Data.dll. Your data access component assembly should be loaded into Motion. You will receive an error if your data access components contains references to other assemblies that cannot be
loaded by Motion. Please make sure all dependent assemblies are located in the bin folder.
- Enter a Business method name i.e. Submit and then select which DAC that you want to load from the
Data access components dropdown list.
Tip: You are advised to use business-like terms when naming your business methods (i.e. Submit, Cancel, Approve, Reject, List etc.) instead of technical names like Insert, Update, Delete and Select. Example: Use names like
Register for adding a user instead of InsertUser.
- Select a data method from the Data methods list and click the
right-arrow button to include it as part of your business method. You can also
double-click on the data method to directly add it into the
Called methods list. To remove a data method from the Called methods list, select it and then press the
Delete key on your keyboard or click the left-arrow button.
Tip: You can select data methods from different DACs to compose your business method. You can also have more than one data methods in your business methods. In this guide, a
Submit business method is composed from the Create method of the
ExpenseDAC and the Create method of the ExpenseLogDAC. This creates a simple logic where a record is inserted each into both the Expenses and ExpenseLogs database tables when the
Submit business method is called.
- If you would like to return the result of a data method in your business method, you can check the checkbox in the
Return column. You can only have one method returning results.
- If you want try … catch blocks to be included in your business method, check the Include exception handling checkbox.
- Click Add to Code Generation to queue your BC class and method definitions for code generation. You can queue as many classes and methods for code generation as you wish. To create a new business method, click on
New. To create another BC, Enter a new Component Name.
- BCs queued for code generation will be listed in the Business Components to generate list and the business methods will be listed in the
Business methods to generate list under the Code Generation section. You can review the definitions and make any last moment changes to them in the property grid below the list.
Warning! Tampering with the properties in the property grid may cause unexpected results or errors in code generation. Make sure you understand the behavior of the code generator before messing with the values in the property grid.
To mark existing BCs for re-generation, you can click on the Reset
button (double rotating arrow icon). This sets the IsUpdated property of the definition to
true. You can also do this manually in the property grid.
To remove a BC from queue, click on the X (remove) button. This does not remove your BC code file from Visual Studio but only removes the definition.
To remove a business method definition from the list, select a business method name in the
Business methods to generate list and press the Delete key on your keyboard. This does not remove the business method from your code but only removes the definition.
Warning! Removed BC and business method definitions are not recoverable.
You can preview how the code will be generated by clicking on the preview button (Book icon).
- Optional – If you want to execute the data methods within a transaction, you can select the business method from the
Business method to generate list, click on the ellipsis (…) button in the
CallMethods property in the property grid, then in the MethodModel Collection Editor dialog, select the first method that you want the transaction to start and set the
TransactionStart property to True.
Then, select the last method you want the transaction to end and set the
TransactionEnd property to True.
This will create a TransactionScope block around the calls to the data methods in the business method.
- Click Generate to generate your business components.
- Click OK on the Code Generation Completed dialog.
- The Business project will now contain the code file(s) of the generated business components. Review the generated code and make any necessary changes according to your needs.
- If you are regenerating code or if a code file exist in the project, you will be prompted to either Append, Backup, Overwrite or Cancel the process.
Append will append whatever new properties to the bottom of the class. Take note it is not an intelligent append and duplicate properties may be appended to your class.
Backup creates a copy of your code file with a .bak extension and requires you to manually include its contents into your project.
Overwrite will replace your existing entity class with a newly generated one. Any previously defined properties that were not included in the latest code generation will be lost.
Cancel will discard the generated code from memory and preserves the existing code file.
- Finally, right-click on the Business project and click
Build to build the project to ensure no errors. You must make sure your projects compile without errors in order to proceed to the next walkthrough.
How-To: Create Data Access Components