How-To: Create Data Access Components
This walkthrough will get you started on using the Momentum: Data Access Component Generator to create data access components (DAC) for your layered application. Before beginning this guide, please make sure you have created a layered application
from the solutions template and have completed the
How-To: Create Business Entities walkthrough.
- In Solution Explorer, right-click on your Data
project i.e. ExpenseSample.Data and click Add Data Access Components…
- The Momentum: Data Access Component Generator will be launched. Take some time to familiarize Momentum’s user interface.
- Click Connect to connect to a Microsoft SQL Server.
- In the Connect… dialog, enter your Server name and select the appropriate authentication method. Click
Connect to connect to the SQL Server.
- Select a database from the database list located beside the Connect button.
- Select a table from the Sources dropdown list. All columns in the selected table will be listed in the
- Momentum will attempt to suggest the Data Component name for you based on the selected table’s name. By default, components are given a “DAC” suffix. You can change them to your naming conventions if you wish.
- Select a Method type to create. Available method types are: INSERT, UPDATE, DELETE, SELECT, SELECT (with Paging), SELECT (COUNT), SELECT (single object) and UPDATE (single object). A method name will be suggested for you.
- Enter a Method Name or leave it as the default.
- Select the columns that you wish to include in your SQL statement by checking the checkbox located beside the column name in the
Columns grid. Based on the method type chosen (i.e. SELECT method type),
Filter and Order grid columns may appear. Columns checked as Filters will appear in WHERE conditions and columns checked as Order will appear in the ORDER BY statement.
- Optional - In some cases, the column names may contain abbreviations, prefixes or suffixes that make them less meaningful as property names i.e. hr_WkflowID, PrdName, StdMercName etc. You can rectify this with column
mappings. If you do not have such column names, skip ahead to step 14. To define column mappings, click on the
bar above the column grid. This will launch the Define Column Mappings dialog.
- Column mappings allow you to map underlying table columns to meaningful property names in your entities. You can rename your
Property Names, change its case and do whatever required to make them meaningful. If you have previously specified any column mappings in the Inertia: Business Entity Component Generator, they will be shown here. When you are done with the
mappings, click OK.
- A confirmation dialog will be shown. You can choose to apply the mappings immediately by clicking
Yes or click No to not apply the mappings.
- The Reference tables list shows a list of tables that have a primary key connected to the selected table. This list will be empty if there are no reference tables.
- The Ref. column list will show a list of columns in the selected Reference table. You can include these columns in your SQL statements. i.e. Showing CategoryName instead of CategoryID in a Product table query. Click the green
+ (plus) button to add the reference column name.
Warning! You must ensure that whatever reference columns you select here are the same as the ones you have selected during the creation of your business entities.
- If you want try … catch blocks to be included in your data method, check the Include exception handling checkbox.
- Click Add to Code Generation to queue your DAC class and method definitions for code generation. You can queue as many classes and methods for code generation as you wish. To create another DAC, simply choose another table.
- DACs queued for code generation will be listed in the Data Access Components to generate list and the data methods will be listed in the
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 DACs 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 DAC from queue, click on the X (remove) button. This does not remove your DAC code file from Visual Studio but only removes the definition.
To remove a data method definition from the list, select a data method name in the
Methods to generate list and press the Delete key on your keyboard. This does not remove the data method from your code but only removes the definition.
Warning! Removed DAC and data method definitions are not recoverable.
You can preview how the code will be generated by clicking on the preview button (Book icon).
- Click Generate to generate your data access components.
- Click OK on the Code Generation Completed dialog.
- The Data project will now contain the code file(s) of the generated data access components. Review the generated code and make any necessary changes according to your needs. Currently, the generated code is targeted for
Microsoft Enterprise Library 5.0 Data Access Application Block (DAAB). Other data access frameworks maybe considered in future releases.
- 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 Data 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 generating business components later.
How-To: Create Business Entities |
How-To: Create Business Components