For these examples, download and install the MyGeneration code generator. The templates included in the download will work with MyGeneration.This example will walk you through generating your business objects against a SQL Server database, but the steps are very similar for other databases, such as Oracle.
A quick note about code generation: Code generation has become a controversial topic lately, and this tutorial is not going to resolve the debate. Do you have to use code generation? No, you don't, but you must have more time on your hands than most developers we have known. If you are skeptical, give it a try. You can always uninstall and code your business objects by hand. Our opinion is you'll never go back to the old way of doing things. You can have a complete, fully functional data access and business object layer in a matter of minutes.
Setting up MyGeneration: (one-time setup)
- Start MyGeneration.
- Click on the Edit menu and select Default Settings.
- On the Connection tab, setup your connection string to the database you want to connect to. If you don't know your connection string, click the OLE DB button to launch a wizard to help you. You must use an OLE DB based driver for the code generation to work properly. Click the Test Connection button to make sure MyGeneration can connect.
- Choose the language mapping for which you want to generate code (C# or VB.NET). Do not choose any mapping marked as "System Type".
- IMPORTANT: Choose a DbTarget of DbType. Do not choose a database-specific type, such as SqlClient. EasyObjects.NET is based on the Enterprise Library, which uses the database-independent DbType enumeration.
- (Optional) On the Template tab, you can change the Default Output Path as needed.
- Click the OK button to save and close the Default Settings dialog.
- You are now ready to generate your code.
Generating the standard stored procedures: (2 minutes) - Click on the File menu and select Open.
- In the Open dialog, navigate to where you unzipped EasyObjects. Locate the \scripts\SQL folder. Open the SQL_DAAB_StoredProcs.vbgen script.
- Click on the Template menu and select Execute, or click the green arrow on the template toolbar.
- Select any tables for which you want to generate stored procedures. (Note: it is not necessary to change any of the other settings)
- Click the OK button.
- When the code generation is complete, MyGeneration will automatically display the Output tab with the results. By default, the output is automatically copied to the clipboard as well.
- Start SQL Management Studio (for SQL 2005) or Query Analyzer (SQL 2000). Connect to the database and paste the output into a new query window. Execute the query.
- You should now have your generated stored procedures in your database (5 per table; Get, GetAll, Insert, Update and Delete).
Generating the abstract business objects: (3 minutes) - Click on the File menu and select Open.
- In the Open dialog, navigate to where you unzipped EasyObjects. Locate the \scripts\CS folder. Open the CSharp_EasyObject_BusinessEntity.vbgen script (there are similarly named scripts in the VB folder if you prefer).
- Click on the Template menu and select Execute, or click the green arrow on the template toolbar.
- Enter a Namespace for your objects (optional for VB).
- Select the same tables for which you generated the stored procedures.
- Click the OK button.
- When the code generation is complete, MyGeneration will automatically display the Output tab with the results. By default, the output is automatically copied to the clipboard as well.
- Open your project in Visual Studio. Add the generated files to your project. The generated files will have an underscore ("_") followed by the same name as your database tables, but without any spaces.
- If you haven't already, add references in your project to the NCI.EasyObjects.dll (located in the \bin folder) and the Common, Data and Configuration dlls (located in the Enterprise Library's \bin folder).
Generating the concrete business objects: (1 minute) - Click on the File menu and select Open.
- In the Open dialog, navigate to where you unzipped EasyObjects. Locate the \scripts\CS folder. Open the CSharp_SQL_EasyObject_ConcreteClass.vbgen script (there are similarly named scripts in the VB folder if you prefer).
- Click on the Template menu and select Execute, or click the green arrow on the template toolbar.
- Enter a Namespace for your objects (optional for VB).
- Select the same tables for which you generated the stored procedures.
- Click the OK button.
- When the code generation is complete, MyGeneration will automatically display the Output tab with the results. By default, the output is automatically copied to the clipboard as well.
- Open your project in Visual Studio. Add the generated files to your project. The generated files will have the same name as your database tables, but without any spaces.
Congratulations! You now have a very functional data access and business logic layer for your application. And it only took a few minutes! Last updated: 12/28/2005 |