Quantcast
Viewing all articles
Browse latest Browse all 6

[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).
 

Image may be NSFW.
Clik here to view.
CodeFluent Entities Add a Producer

CodeFluent Entities Add a Producer


 
CodeFluent Entities provides a large choice of Producers and Sub-producers.
 
Image may be NSFW.
Clik here to view.
CodeFluent Entities Producers

CodeFluent Entities Producers


 
Image may be NSFW.
Clik here to view.
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 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”
 


 
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.
 
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.
 

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).
 


 
CodeFluent Entities use stored procedures for CRUD operations.
 

 
 

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.
 


 
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.
 

 
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.
 
Image may be NSFW.
Clik here to view.
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.
 


 
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, binding features, comparison capabilities an application requires.
 

The CodeFluent Entities BOM is more than 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.
 


 
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.
 

Image may be NSFW.
Clik here to view.
Generate SQL scripts with EF

Generate SQL scripts with EF


 
Image may be NSFW.
Clik here to view.
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.

 


 
 

Generate 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.
 


 
Let’s now generate the C# code from our Entity Framework Model.
 

 
The code generated by the default templates is very simple.
 

 

 
We use the DbSet and DbContext to manipulate entities (Add, Attach, Remove) and LINQ to read data (as DbSet implement IQueryable).
 
As the generated classes 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 not easy way to add Data Annotations Attributes to our classes as we did it to our CodeFluent Entities classes.
 
 
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. Image may be NSFW.
Clik here to view.
:)

 
 
Cheers,
 
The SoftFluent Team
 
 
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 6

Trending Articles