-
Notifications
You must be signed in to change notification settings - Fork 0
Reusing .NET Types
Using SoftFluent CodeModeler, you can store existing .NET types data to the persistence layer, whatever the persistence layer is SQL Server, MySQL, etc. In fact, there are several ways to do it.
Let’s suppose we want to store, graphical shapes in the database. We define this very simple model, defining coordinates, width and height of the shape bounding rectangle:
However, defining a Shape object like this can be done differently since there are an existing .NET Rectangle types, for example in the System.Drawing namespace, from the standard System.Drawing.dll assembly. Instead of defining an extra custom object, we can use the standard .NET type.
To do so, in Visual Studio, you can add a .NET references to the CodeModeler project, using the References folder node “Add Reference” menu item, “.NET” tab (you may have to wait for a few second that the list appears as it may be computed on-the-fly):
Choose System.Drawing and press OK, now, the project should display it as a reference:
Now, you can add a new property, 1) choose an Advanced type, 2) Browse, 3) “Browse Clr Types…” and 4) choose the “Rectangle” type in the System.Drawing assembly and namespace:
Now the entity will be defined like this:
That will work fine because CodeModeler tries to serialize everything it does not “know”. Known types are (using C# aliases when it exists): int, uint, long, ulong, short, ushort, byte, sbyte, byte[], bool, float, double, decimal, string, char, System.Guid, System.DateTime, System.TimeSpan, explicit CodeModeler blob types (declared using “file”, “video”, “image”, “audio”), all enum (System.Enum) types. Known types are fully documented in the Type System article.
For all other “unknown” types, CodeModeler uses serialization. Several kinds of serialization are supported: Binary serialization, XML serialization, and Lightweight serialization.
Existing enumerations in a .NET assembly can be easily reused. For example, here we use System.DayOfWeek as the type for a Day property:
CodeModeler will use what has been configured on the property. This is defined by the model attribute “Persistence Serialization Mode” (“Advanced Properties” tab). The default value is Binary | ArrayOfBytesType:
It means the .NET instance will be serialized using .NET standard serialization (not XML, nor SOAP), and stored as an array of bytes.
Thus, our rectangle will be serialized as an array of bytes. With this model, the corresponding Bounds column data type in the database will be TEXT/NTEXT (SQL server) or BLOB (MySQL).
With the following C# using the generated Business Object Model (please note that we use the standard .NET Rectangle type, as expected):
var shape = new Shape();
shape.Bounds = new System.Drawing.Rectangle(10, 20, 30, 40);
shape.Save();
We will have this in SQL Server:
Now, we could use XML Serialization instead of the binary one used by default. To do so, we need to set the “Persistence Serialization Mode” attribute to Xml | StringType:
Because we used Xml combined with StringType, the corresponding “Bounds” column data type in the database will be XML. With the same C# code as previously, here's what will have this in SQL Server:
If we click on the Xml column, we can see the Rectangle instance was automatically serialized as plain Xml:
Now, let’s suppose we want to load all our shapes ordered – server side – by the rectangle area, or we want to write some back-end process that needs to access to the inner properties of the stored rectangles. What we want to do is have the X, Y, Width and Height properties available in the persistence layer.
As you know, Lightweight Entities can be mapped to existing .NET types, so a solution would be to declare the Rectangle .NET type as a lightweight entity. Here's how to do it:
First add an entity named “Rectangle” and use “System.Drawing, System.Drawing” as the namespace (this is the namespace and assembly name of .NET ‘s Rectangle struct):
This will add an entity shape with the shape border drawn using a dotted line. It means the entity has been recognized as an existing .NET type and will be considered as a lightweight entity in the CodeModeler model. You will notice the namespace corresponds to the struct’s namespace too. You must then manually add the properties of the struct you want to map to the CodeModeler model:
Note when an existing type is declared as an entity, this entity does not have the notion of key properties. You just need to declare what properties you want CodeModeler to use when persisting an instance of it. These properties must exist on the .NET type, and their name is case sensitive. In the case of Rectangle, X, Y, Height and Width are real properties and are all the “System.Int32” or int type. Rectangle also has Bottom and Left properties, but since they are redundant (computed), we don’t declare them. The persistence type is automatically determined by CodeModeler using reflection techniques so you must not set the “Persistence Serialization Mode” attribute.
We can now use this lightweight entity in another entity, like this (a lightweight-typed property has a special display icon):
And we can create a method that will use it:
In this example, the method is a raw method because CodeModeler does not allow the * notation in an order by.
In the database we now have 4 columns representing the Bounds property. If we use the same previous C# code, this is what we have in the database:
- 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