In this post we will treat the model designing aspect of the application. In the first part we will see how to design our business model with CodeFluent Entities then we will show how to do it with Entity Framework 5. Finally, we will compare both designers’ capabilities and show which benefits you can have by using CodeFluent Entities.
Using CodeFluent Entities model designer (aka modeler)
CodeFluent Entities is a software completely integrated into Visual Studio (2008/2010/2012 and 2013 soon). In fact, Visual Studio is strictly speaking not mandatory (you can edit files with any text editor, or use the Portable Modeler tool we talked about here https://blog.codefluententities.com/2012/09/11/the-codefluent-entities-portable-modeler/), but it’s by far the easiest way to start with CodeFluent Entities.
Designing an application with CodeFluent Entities allows you to directly create from the model:
– Custom methods
– And much more…
As you can see in the screenshot below, the CodeFluent Entities’ designer is composed by a designing surface and a ribbon.
The ribbon contains buttons which allows you to perform actions like adding entities, methods and so on. Those actions are also available by right clicking the surface.
Our first action will be to add an entity, to do so you can hit the ‘Add entity’ button from the ribbon or by a right click on the surface then ‘Add > Entity’. According to our project we will name it ‘Category’.
Once this entity added, we will add a first property named ‘Id’ of type ‘Int’ which will be our primary key. By default, with CodeFluent Entities, the first property added is considered as the primary key but of course, you can change it through the settings of the property.
You may have noticed there is a lot of property types, more than available with Entity Framework 5. The reason is CodeFluent Entities type system is a composite one since it must adapt to the underlying target type.
When designing your model you are not targeting any specific technology. The CodeFluent Entities model is totally technology agnostic.
CodeFluent Entities also provides extra types through its runtime such as email, image, audio, film. An exhaustive list of types can be find over the documentation.
In order to demonstrate other features, we will add three other properties: Description, Name, and Products, all of type string. We will get back to this ‘Products’ property a bit later.
Then we will add another entity named ‘Product’ which will contain five properties: Id, Description, Image, Name, and Category, respectively of type integer, string, image, string and string.
We will now see how to create relations between two entities properties. This can be done directly when the property is added by checking ‘Advanced’ for the type of the property and then selecting the property we want to bound it to. We can also do it through the properties of a property by changing its ‘Type Name’. And finally we can ‘Shift + Left click’, on the property and bound it to the property of our choice.
In this case, we will bound the ‘Category’ property to the ‘Products’ property in order to create a one-to-many relation as it is showed in the following screenshot.
You may have notice that there is also the possibility to create cascade save and delete depending on your needs.
In the introduction, we talked about custom methods. Now that we have two entities we can create a custom method in our ‘Product’ entity which will be named ‘LoadByCategoryName’. In order to add a custom method, we can either click the ‘Add method’ ribbon button or right clicking on the entity and hit ‘Add > Method’.
With CodeFluent Entities you will use CFQL (CodeFluent Query Language) to create custom methods. CFQL syntax is really close to SQL syntax, which allows you to use your SQL knowledge to create methods.
CFQL allows you to keep your model logic independent of any technology. CFQL code will be afterwards converted automatically to stored procedures in the specific target language (SQL Server, Oracle, MySQL…).
As you can see in the following screenshot, this method allow us to retrieve products based on their category name.
In the CodeFluent Entities designer we are also able to add attributes (CLR attributes) or rules to our properties. For instance, we will add an attribute to the ‘Description’ property of our ‘Product’ entity to specify this property is required thanks to the ‘System.ComponentModel.DataAnnotations’ namespace.
In order to add this attribute we will focus on the ‘Description’ property then we can either click the ‘Add attribute’ button in the ribbon or right clicking on the property hit ‘Add attribute’. In the attribute name we can directly reference the attribute, in this case ‘System.ComponentModel.DataAnnotations.RequiredAttribute’, or we can browse through namespaces to find the one which fit our needs by hitting the button next to the ‘Attribute name’ textbox as it is shown in the screenshot below.
To conclude, on CodeFluent Entities designer we will talk about the inferred model. You can access it through the ribbon by hitting the ‘View Inferred Model’ button.
The inferred model allows you to foresee what will be generated by CodeFluent Entities: classes, tables, relations, attributes, and so on. Therefore, you can see if you designed your application the way you wanted to.
The CodeFluent Entities Modeler is much richer than what you have seen in this post. You can take an overview on this video (http://www.youtube.com/watch?v=gsfrHJqCpgc), and for the Modern UI (http://www.youtube.com/watch?v=UUXoiz4mWB8).
The SoftFluent Team.
We know now what are the functional and technical context of our application: Pet Shop Reloaded. We can now start to prepare our working environment.
Getting ready with CodeFluent Entities
CodeFluent Entities is fully integrated into Visual Studio and really easy to install. We need to go to the installation page of CodeFluent Entities, download an .msi file and run it.
We just need to follow the wizard installer.
The installer will add a Visual Studio extension so you can access all the CodeFluent Entities features directly from your favorite IDE. So now that we have successfully installed CodeFluent Entities, let’s open Visual Studio and see that there is now a CodeFluent Entities menu.
You need to request a License to use CodeFluent Entities, there is a totally free, unlimited in time and full-featured license for personal use.
We will also find the CodeFluent Entities project templates when adding a new project from Visual Studio.
For the Pet Shop Reloaded we will add a new CodeFluent Entities blank project.
To discover CodeFluent Entities you can use a sample model provided with the project templates.
Getting ready with Entity Framework 5
Entity Framework on its version 5 is already integrated into Visual Studio 2012.
If you are using Visual Studio 2010 you will need to manually install the latest version of Entity Framework (version 5) from NuGet, you can refer to this page or this one to know more about installing the Entity Framework from NuGet.
If you use Visual Studio 2010 you will also need to install the DbContext generator, see the “Additional Steps in Visual Studio 2010” point.
To add a new Entity Framework model (.edmx), we need to add a new item in an existing project. For the Pet Shop Reloaded application we will host the Entity Framework model right on the ASP.NET MVC project.
Entity Framework does not contain an independent project template, it is added to an existing C# project.
So we add a “New Item” and on the “Data” tab we choose “ADO.NET Entity Data Model”:
As we are going to use the Model First approach as our Model Creation Workflow, we select the “Empty Model” option.
There are essentially 3 different Model Creation Workflows: Model First, Data First and Code First. If you want to know more about this you can take a look at this blog post.
You can extend the Entity Framework behavior by modifying or adding .tt templates.
The CodeFluent Entities project
CodeFluent Entities has its own project on Visual Studio terms. As you have a “Class Library” project, a “WPF” project or an “ASP.NET MVC” project, you will have a “CodeFluent Entities” project in your Visual Studio Solution.
The image below shows the main structure of a CodeFluent Entities project.
- References: to add assembly references.
- Files: to store templates for example.
- Surfaces: contains the Modeler design surfaces (you can split your model on different design surfaces).
- Parts: where are stored the XML files (.cfp file extension) that contain the definition of our model (entities, properties, producers…).
- UserTypes: custom types that can be used as property types on the model.
- Stores: persistence configurations.
- Aspects: contains the available aspects.
- Producers: “code generators” for different layers of our application(s) (Persistence, Business Object Model, Web Services, Web site, Azure, SharePoint…).
- Model: contains a tree view of our model grouped by “namespaces”.
The Entity Framework structure
The Entity Framework model relies on a XML file with an .edmx extension. As we said earlier, an Entity Framework Model must be integrated in a Visual Studio project. For the Pet Shop Reloaded application we are going to call the Entity Framework model “Model.edmx”.
Under the Model.edmx file we can see different items (essentially code generators, .tt files, aka T4 templates).
On earlier versions of Entity Framework (before version 5) the code generation templates are not located under the .edmx file but at the project level.
- Model.edmx: the Entity Framework model.
- Model.Context.tt: will generate the Entity Framework Context.
- Model.Designer.cs: default code generation (disabled by default so the T4 templates are used to generate the code).
- Model.edmx.diagram: a XML definition of our model diagram.
- Model.tt: T4 template that will generate POCO classes from our model.
Updates and extensions
Updating the CodeFluent Entities version
It is important to always have the latest version of CodeFluent Entities (in general it is the case for software products), this way you will have the newest features and fixes.
CodeFluent Entities is shipped with a tool called the “SoftFluent Licensing”, you will find it under the CodeFluent Entities installation folder: Program Files (x86)\SoftFluent\CodeFluent\ModelerSoftFluent.Licensing.Client.exe
This tool allows you to know if there is a newer version of CodeFluent Entities.
For each build version you will find the list of new features and bug fixes.
CodeFluent Entities does not need any external component, plugin or extension to work, it provides a lot of features and concepts. And if you need more or a specific feature you can easily extend CodeFluent (templates, producers, aspects…).
Updating and extending Entity Framework
Entity Framework is available as a NuGet package (or integrated to Visual Studio) so it can be updated from Visual Studio (or the NuGet console). You can also find extensions or templates (code generators) online.
If you consider that the default code generation of Entity Framework is not enough for your project, then you can modify or add T4 templates to adapt your model to your needs.
On the next blog post we are going deeper on each product, starting on how we will design the model of the Pet Shop Reloaded application.
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. 🙂
The SoftFluent Team
1-year CodeFluent Entities Ultimate
1-year CodeFluent Entities Enterprise
1-year CodeFluent Entities Professional
The Pet Shop Reloaded is a sample e-commerce web application. Users will be able to browse a nice set of animals by category, add them to their cart or wish list, and finish their purchase by log in or register to the application.
Users first arrive to the home page where are displayed some random animals to discover.
On the top of the page users can perform actions like:
- See their cart.
- Log in/Log off (if connected).
- Wish list (if connected).
- Account information (if connected).
- Search animals by keywords.
Users can browse animals by category.
When selecting an animal, are displayed all the different kinds of this animal with some details like the quantity remaining and the price.
Users can then add the desired animal to their cart or to their wish list (if logged in).
The cart page contains the list of animals to purchase by quantity.
When the user checks out, he has to confirm his billing and shipping addresses, provide his payment and confirm his order.
If the user is logged in, he can add animals to his wish list and manage it.
Users can manage at any time their account information.
The SoftFluent Pet Shop Reloaded will be developed as a web solution using:
- .NET 4.5.
- C# 5.
- ASP .NET MVC 4.
- SQL Server 2008 R2 (the 2012 version can be used as well).
- Visual Studio 2012.
The CodeFluent version
The CodeFluent Entities PetShop version will use the CodeFluent build 61214.707.
We will build the CodeFluent Entities model using the CodeFluent Entities Designer. This means that all the aspects related to the model will be implemented within the CodeFluent Model Designer, like:
- Data validation and custom business rules.
- Custom data access methods (stored procedures).
- Custom and extra attributes.
- Instances (data used to fill the database, useful to start with an application containing data).
There will be 2 producers to generate the Business Object Model layer (BOM) and the Persistence layer:
- The BOM producer will generate our .NET/C# classes, these classes contain all the logic that we have defined in our model (Validation, Rules, CRUD methods, custom data access methods…).
- The SQL Server producer will produce (and execute) SQL scripts to generate or update our database. CodeFluent Entities provides a “SQL diff engine” smart enough to update our database schema without losing our data.
On the MVC application we will directly use the BOM to manipulate and bind our models.
The Entity Framework version
The Entity Framework PetShop application will be built with the Entity Framework version 5 provided out-of-the-box with Visual Studio 2012.
We will use the Model First approach to design our model and we are going to use the built in templates (.tt templates) to generate our .NET/C# classes.
For validation and custom rules we may need to do some extra work, this will be explained in a blog post dedicated to Validation and Rules.
The persistence layer will be generated using the SQL code generated by the Entity Framework.
We will use essentially LINQ queries to retrieve data and the generated model will be used directly on the MVC application.
On the next blog post we are going to show how to start with CodeFluent Entities and Entity Framework (installation) before starting using the model designers.
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. 🙂
The SoftFluent Team
Some of you may know the story about the Pet Shop application, a .NET/C# revisited version of the original Java Pet Store application made by Sun Microsystems. These two sample applications were used as a mean to show an implementation of the best practices and features of both technologies. Today, these applications begin to get dusty as they were released on November 2001 and May 2001 respectively.
So the SoftFluent team decided to build a reloaded version of the Pet Shop using CodeFluent Entities. To maintain the spirit of the original concept we also decided to develop a parallel version using Entity Framework.
Indeed, a recurrent question about CodeFluent Entities is how it relates to Entity Framework. SoftFluent has published a white paper giving a large set of details on how these two products differ so one can understand how CodeFluent Entities is much more than an ORM.
We are going to write a series of blog posts showing how some concepts and features can (or cannot) be implemented using CodeFluent Entities and Entity Framework. We may not cover all the concepts or features of the two products (as there are many), but we will treat the main features needed to successfully implement a web application.
We will lead this study case as objective as possible and we will love to discuss any question, remark or feedback people may give us, so don’t hesitate to comment every post.
You can find all the blog posts related to the Pet Shop Reloaded by SoftFluent under the tag “Pet Shop Reloaded“. Stay tuned. 🙂
- The Application.
- Getting started.
- The Model Designers:
- Generating code.
- Using the Generated code:
The SoftFluent team.