[Pet Shop Reloaded] Designing the application model – Part 1
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.