Not long time ago, I faced the following requirement:
Company uses custom Data Access Layer, which is developed within company and which does not have any designer tool as Entity Framework or Linq to SQL frameworks have. (Actually the company uses CSLA – but it is modified and cannot be upgraded to latest version even if latest version of CSLA does have any designer tools – but as I know it does not)
The goal is to simplify the creation of new business objects along with data access code.
It is preferred o collect data about business object to be created in some kind of visual form and then generate the business object.
It is also desirable to add business object to Visual Studio solution automatically.
Before they used MyGeneration software, which is separate application running outside of Visual Studio. They had to generate code in it and then manually place the files into VS solution.
After investigating and evaluating few approaches – I stopped on the following:
1) I will use Custom Item Template for Visual Studio to import files into VS solution by using standard VS Add New Item dialog
2) I will use Custom Template Wizard to collect data before adding item to VS solution.
The process of business object creation after implementation looked like this:
Developer runs Add New Item dialog in WPF project:
Developer selects our Custom Item Template:
Developer provides name of business object to create:
After developer clicks Add – he is presented with our Custom Template Wizard:
Here developer can design the entity – set name, type (along with storage type – DBType, default value, mandatory status, whether it is foreign key – so it must be used in query method as parameter, whether the property is key etc.)
Here is a data model accumulated per business object line (property) :
This form also does validation of data entered by developer:
so When property IsAutoKey, it cannot have default value:
Key property cannot be Nullable:
Length is only enabled where applicable to data type:
Validation is applied to property name as well:
Some other validations:
When developer finishes entering business object data he can:
1) generate C# objects and add to solution right away
2) OR first generate SQL procedures for business object
Let’s review 2nd point.
To farther simplify SQL code execution, the Custom wizard also has ability to connect to DB and retrieve metadata for tables/views from it.
To connect to DB, you must provide parameters:
The parameters are stored per machine – so once set up – they will be in effect for any solution / instance of VS.
On this stage we can also import metadata from DB table/view and populate business object properties data.
Just click Import:
On the popped up window developer can choose table/view to import columns from.
When importing from DB table/view, business object name also changes on top:
Developer can choose to generate many types of BO – as RO, Writable, Root, Child etc. – every single one will be generated under the hood by using separate T4 template.
On any stage of working in Custom Wizard Form – the developer can generate scripts for business object:
In this window the following scripts are generated:
Table, Select SP, Update SP, Insert SP, Delete SP
The tool can be extended to have more scripts with any contents – as each script is generated from separate T4 template.
Developer can save any script on this stage:
(unfortunately developer will have to check in the file to source safe manually)
If developer clicks Execute, he will be able to run script right away:
And now the most important part
Developer clicks “OK”, and business objects will be generated and inserted to VS solution at correct place automatically:
Now the architecture.
The code generates from T4 templates -
Each template is provided with custom data accumulated by wizard. The templates use data to generate code.
Hope this idea helps you deciding to use code generation in your projects : )
Now about technical implementation.
You (or any developer that wants to start using this) must build the project attached in the end of this article once – and that is it – it registers Custom Wizard, and zips template and copies it to templates directory for VS (see pre and post build events)
you can get complete source code (http://devarchive.net/downloads/CodeGenerator.zip)
Hope this helps