[Pet Shop Reloaded] Generating code


 
We have defined our application model and the technical environment of our application Pet Shop Reloaded. It is now time to generate the code we are going to work with. Both, for CodeFluent Entities and Entity Framework, the generated code will be produced from the “Model” we have designed.
 

Generating code from the CodeFluent Entities Model

 
Generating code with CodeFluent Entities is made using “Producers”. Producers will transform our model and all its concepts in concrete text files (source code files).
 

CodeFluent Entities Add a Producer

CodeFluent Entities Add a Producer


 
CodeFluent Entities provides a large choice of Producers and Sub-producers.
 
CodeFluent Entities Producers

CodeFluent Entities Producers

 

CodeFluent Entities Sub-Producers

CodeFluent Entities Sub-Producers


 
As you can see there are huge possibilities for the same Model, as the CodeFluent Entities Model is agnostic of all technology layer you only need to add a specific producer to have a concrete representation of your model.

All the producers will be executed when we build our CodeFluent Entities project, but if needed you can disable specific producers by right clicking them.

CodeFluent Entities does not use templates to generated code.
 

Using the SQL Server producer

 
First, we are going to add a “SQL Server” producer, this producer can be found under the “Persistence Layer Producers”
 

The SQL Server Producer

The SQL Server Producer

 

We just need to add a connection string and an output directory where the generated SQL Server specific scripts will be generated. We have added a SQL Server database project to store our scripts.

CodeFluent Entities supports a wide variety of persistence implementations natively. Using Entity Framework you will need to search for external providers when you want to use some specific data base engines like MySQL, PostgreSQL or Oracle.

As you can see on the image above CodeFluent Entities can execute the generated scripts to create the database if it does not exist, insert the instances we have defined in our model, update the database and create diffs.

Unless Entity Framework, CodeFluent Entities has a diff engine that avoids to drop the database and create a new one each time we make a change in our model structure. This is very useful because this way we don’t have to backup and restore our data for each database update.

 
The SQL Server producer will translate our Model concepts into platform specific code:

  • Properties become columns,
  • Methods become stored procedures,
  • Instances become lines of data,
  • Entity keys become primary keys,
  • Entity relation properties become foreign keys,
  • Views in your model become actual SQL views,
  • etc.

After building our CodeFluent Entities project, we now see the T-SQL scripts on the location we have specified (the scripts will also be executed on the database).
 

Generated T-SQL scripts

Generated T-SQL scripts


 
CodeFluent Entities use stored procedures for CRUD operations.
 
Stored Procedure example

Stored Procedure example


 

Using the Business Object Model (BOM) producer

 
Let’s now add the producer that will generate all the .NET/C# classes that will contain our model structure and logic.
 

The BOM Producer

The BOM Producer


 
We will use a C# class library project to store our generated BOM classes.

Let’s build our CodeFluent Entities project so our BOM classes are generated.

You may also have noticed that the BOM producer allows you to select VB.NET as the Target Language.

Since CodeFluent Entities is made to help developers through their developments its purpose isn’t to force you developing in a language you haven’t choose.

Therefore, CodeFluent Entities gives you the opportunity to develop using the language you love.

 

The generated BOM

The generated BOM


 
All the features in our model will be present in our generated code. For example, we have added Data Annotation Attributes right on our model so our class properties will be decorated with those attributes.
 
Data Annotation Attributes

Data Annotation Attributes


 
For each entity we have 2 classes: a class having the same name as the Entity (e.g. Product, Order…) to handle unitary operations (Load one, save, delete, update) and a class having the suffix “Collection” as you can guess that is used to handle entity collections (Load all, page Load…).

The BOM classes are partial, so you can extend them as you want.

 
The C# classes generated by CodeFluent Entities do not inherit from any technical class, they only implement interfaces, so the code is entirely readable. The BOM classes implement some of the most used .NET interfaces as:

  • Unitary classes:
    • System.ComponentModel.INotifyPropertyChanged
    • System.ICloneable
    • System.IComparable
    • System.IComparable<T>
    • System.ComponentModel.IDataErrorInfo
    • System.IEquatable<T>
  • Collection classes:
    • System.Collections.IList
    • System.Collections.ICollection
    • System.Collections.Generic.IList<T>
    • System.Collections.Generic.ICollection<T>
    • System.ComponentModel.IRaiseItemChangedEvents
    • System.ICloneable
    • System.ComponentModel.INotifyPropertyChanged
    • System.Collections.Specialized.INotifyCollectionChanged
    • System.ComponentModel.IBindingList
    • System.ComponentModel.ICancelAddNew

This way you have all the features you will need for your Business Object Layer.
 

BOM code example

BOM code example


 

If you need your BOM classes to implement custom interfaces you can define it right on your CodeFluent Entities model, you will only need to provide an implementation in a partial class.

You can use the BOM generated by CodeFluent Entities as it. It contains all the validation logic, access to CRUD methods, state tracking capabilities, binding features, comparison capabilities and all the “technical business” features that a Business Object Layer may contain. And we have all that out of the box without the need to customize templates or add extra code generation artifacts.

 

The CodeFluent Entities BOM is more than just POCO classes.

 
We can see that other classes have been generated, classes to handle BLOBs (binary large objects) and security features (authentication, membership features) as we have used the “Membership Entities” provided by the CodeFluent Entities Model. That is very convenient to have completely functional BLOB handlers, handling BLOBs inefficiently can seriously harm an application performance.
 

BOM extra classes

BOM extra classes


 
You can always add more custom generated code as CodeFluent Entities provides an API to access the inferred Model in each of the generation steps. For example you can add templates or aspects to generate or modify the generated code. You can even add custom producers and sub-producers.
 

Generating code with Entity Framework

 
As we are using a Model first approach with Entity Framework we will need to generate the database as well as a Business Model from our entities model.
 

Generate the database scripts with Entity Framework

 
From your Entity Framework model you can generate the SQL scripts to create the database and all the structure.

Generate SQL scripts with EF

Generate SQL scripts with EF

 

Generated SQL script with EF

Generated SQL script with EF

 

Unlike CodeFluent Entities, Entity Framework does not generate a diff of your database structure, so each time you make a change on your model and want to update your database structure you will need to do a backup of your data, execute the scripts and restore your data.

 

SQL code example with EF

SQL code example with EF


 

Generating POCO classes with Entity Framework

 
Entity Framework generates code using T4 templates (.tt files). There are several templates provided by the Entity Framework team and the code generated by default may depend on your Visual Studio version.

We are using Visual Studio 2012 and Entity Framework 5. For this configuration the recommended (and already provided) templates generate POCO entity classes and a context that derives from DbContext. These templates are nested under the .edmx file.
 

EF T4 templates

EF T4 templates


 
Let’s now generate the C# code from our Entity Framework Model.
 
Run Custom Tools with EF

Run Custom Tools with EF


 
The code generated by the default templates is very simple even minimalist. As you will see below, unlike CodeFluent Entities, Entity Framework generated code provides basic classes and not ready-to-use code for real world applications.
 
The EF DbContext

The EF DbContext

 

EF POCO class example

EF POCO class example


 
We use the DbSet and DbContext to manipulate entities (Add, Attach, Remove) and LINQ to read data (as DbSet implement IQueryable).
 

Conclusion

 
As the generated classes by Entity Framework are very simple, we will have to do some extra work if we want to add custom feature to our classes. For example, there is no easy way to add Data Annotations Attributes to our classes as we did it with our CodeFluent Entities model, we will need to add those attributes to our Entity Framework generated classes through partial classes for example.

We can see that the POCO classes generated by the Entity Framework default templates lack basic features for a Business Object layer and we will spend a significant amount of time extending the default templates, creating custom code generation artifacts, searching for external solutions, manually coding those features…

The CodeFluent Entities BOM classes are ready to use, no extra work is required to have a totally functional Business Layer while specific and custom extensions are easy to add.

 


On the next blog posts we are going to see how to use the generated code.

Remember that you can find all the blog posts related to the Pet Shop Reloaded by SoftFluent under the tag “Pet Shop Reloaded“. Stay tuned. 🙂

Cheers,

The SoftFluent Team

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s