-
Notifications
You must be signed in to change notification settings - Fork 0
SG Enhance your model
Now that we know how to create a simple Model, let’s add some subtleties.
For predefined values, with CodeModeler you can create an enumeration. This is an advanced type.
Example:
We will add an enumeration “OrderStatus” for our orders.
On your ribbon click on “Add Enumeration”.
After that, add some
values, by clicking on “Add Values”.
Now that you have some values, we can add a property using this enumeration on an entity.
Add a property and select Type Advanced. Click on suspension points and select “OrderStatus”.
Validate the type and the property.
We can see a relation between OderStatus and the enum.
Once th emodel built, in the c# BOM classes the code generated will map automatically the database level integer values to .Net enumeration.
Example
In a typical real business model, some entities are related to another. With CodeModeler, we will design this creating a relation (as in "relational database") or relationship between the two entities.
Example:
Here we need to create a Customer property on the Order entity. We want that one Customer has many Orders, and that One order is related to exactly one existing Customer: this is One to Many relation.
To do this, select your entity Customer, and (SHIFT key) the related entity Order: the following relation creation dialog box is displayed :
You need to specify the properties that will represent each side of the relation. Here, the properties will be Customer and Orders.
Choose the type of the relation between the two entities, one Customer has many Orders (In English, one customer can have many orders).
Specify also that Orders must be deleted before Customer in Cascade zone so that no orphaned order exists.
Click on “OK” to add the relation to the model. You can now see the relation on the design surface
Code Generated SQL Level :
Code Generated BOM:
For instance, if an entity Order is related to a Customer entity, a LoadByCustomer method will be generated automatically for the Order entity at the BOM level, and a related stored procedure at the SQL level level.
Another Example:
You can also use an Entity Collection type.
Now we want to link the property Products in Order, to the entity Product.
Add a new relation.
Select the entity Order
The related entity is ProductCollection. Here, we select an advanced type and not a property.
The type of relation is One Products has many Orders.
SQL Code Generated:
Code Generated BOM:
CodeModeler allows to create instances in the persistence layer at production time. It is very useful when developers want to initialize specific tables with reference data.
Example:
Now let's add some data to our model.
we will add some values on your entity Product: Apple, Bread, Soda.
Select the Product entity and open the Instances Grid windows from the ribbon.
Fill the grid with all
the products you need to initialize your database with.
Build your model and your development database is now filled with the initial data.
SQL Code Generated :
The SQL code generated uses the Save stored procedure of the entity so that any database level constrains are enforced correctly.
Event rules refer to rules allowing the developer to plug his own code in the generated code. A developer can add event rules at three levels:
-
On entities,
-
On properties
-
On methods
Whatever the rule type is, or at which level the rule is, rules are always declared the same way.
Example:
In our example, we want to insert a debug log entry before a Customer is added. To do so we will use a OnBeforeCreate rule.
Select your entities and click on “Add Rules” on ribbon.
Click on
OnBeforeCreate and fill the name with the desired method to be
called that will implement the rule.
Build your project, now you can see a compilation error for the BOM library :
'Customer' does not contain a definition for and no accessible extension method accepting a first argument of type 'Customer' could be found.
To solve this error, the first approach is to create your own method LogBeforeCreation. But each time you build your project, all files will be regenerated. So, you need to create an additional file like Customer.partial.cs and add a partial class “Customer” declaration in it. In this partial class you can now add your own method(s).
See “Methods” paragraph to see other approach to inject your own code in the model.
This topic explains how to set the key attribute on properties:
-
to declare a single primary key on an entity, set the Key attribute on one of its properties
-
to declare a composite primary key on an entity, set the Key attribute on several of its properties.
If you don't explicitly
set the Key attribute on any of the entity properties, the first
property whose type is key compatible will have the key attribute set
implicitly.
Nota bene:
The generated Primary keys are no clustered by default using the SQL Server producer.
If it makes sense for instance for a string property key that can be ordered, you can ensure it is clustered using the Custom and Producers tab to display SQL producer level attributes that can be set on the property
CodeModeler offers advanced validation capabilities using Entity or Property level Validation rule.
In this example, we verify the user’s phone.
Select the property “Phone”, click on “Add Rule”. On Validation Rules, select RegularExpression
Enter the regexp expression in the configuration
On the generated code, a validator expression is added
And this is used in following generated code ValidateMember
Validation can be combined. In addition to the format validation above, we can add Required validation to prevent the call of database layer or external webservice call if data is not provided (validate early for performance approach)
ValidateMember is then combining all validation rules
In addition to ensuring that input data is not ill-formatted, it's a common need in business applications to validate data based on a business rule : in this case the Entity Lifecycle Rules are recommended to insert custom validation code before saving for instance (see Below).
By default, CodeModeler generates a set of methods including CRUD methods, and methods which are a physical implementation of the designed relations.
Custom methods are platform independent queries that will be translated:
-
by the Microsoft SQL Server code generator into T-SQL queries,
-
by the Business Object Model generator into C# methods
they are 3 way to create a custom method:
-
CodeModelerQueryLanguage (CMQL)
-
Snippet with method signature.
-
Snippet without method signature (Not recommended)
CMQL is the preferred approach to generate load/search type of code using the entities properties and relations. It allows very short pseudo code to generate all required C# and database level code.
Snippets are chunks of code that are declared in a CodeModeler model and injected in the generated classes source files. It should be used when CMQL is not possible for complex queries but you can call customer database level code such as SQL custom stored procedure. It is useful also when some business layer code is needed to validate complex business rule. Another case is when an external sub-system needed to be called for an entity level rule like an external webservice or a .Net library.
Snippets have the following advantages:
-
snippets can be injected in the layers of the application automatically (e.g. business layer, data layer, etc.) without the need for additional source files added manually to the projects,
-
since snippets are declared in the model, they survive model generations,
-
snippets are practical because they use the programming languages we know: a snippet is coded in the targeted language directly since it will be copied and pasted as is in the generated code.
We need to have a method to get all products that are available and not all products. To do so, add a method and select CMQL type
Then add this text in the designer window opened.
LOAD(bool availability)
WHERE IsAvailable=@availability
Now build your project.
In the ProductCollection.cs source file you can now find the generated method LoadByAvailable along its paginated variant PageDataLoadByAvailable.
These methods the SQL level stored procedure also generated : Product_LoadByAvailable?
The 2 CMQL lines are mapped to nearly 50 lines of code.
Add a method and select Snippet with method signature type
Then write the body method in the designer window opened.
By default, all method return void. We need to modify the return type. In our example the method returns a string.
So, select the method and click on properties. In “Return Type Name” select the type. Here this a string.
Moreover, you can add some parameters, rules so on.
This method is not recommended.
You write the method directly in the windows including the signature part of the method . After the build, the method will be copied as is in the Entity or EntityCollection source file.
public string GetName()
{
return \_firstName + " " + \_lastName;
}
- Introduction
- Architect Guide
- Concepts
- Using Visual Studio
- Overview
- Creating a CodeModeler Project
- Visual Environment
- Project Hierarchy
- Design Surface
- Customizing Design Surfaces
- Ribbon Bar
- Property Grid
- Member Format Expressions
- Model Grid
- Method Editor
- View Editor
- Instance Editor and Grid
- Resources Editor
- Inferred Model Viewer
- Building
- Project Physical Layout
- Source Control Support
- Generating
- Aspect Oriented Design (AOD)
- Developer Guide
- The Business Object Model (BOM)
- CodeModeler Query Language (CMQL)
- Starting Guide - Tutorial
- Upgrade From CFE