CodeFluent Entities Documentation
The Persistence and the Business Layers
See Also Send comments on this topic.
CodeFluent Entities > Tutorials > Using The Modeler Edition > Developing Desktop Applications > The Persistence and the Business Layers

Glossary Item Box

[This is preliminary documentation and subject to change]

Note: Even though this tutorial was realized using Microsoft Visual Studio 2010, it also applies to Microsoft Visual Studio 2008.

Preparing your development environment

First of all we'll prepare our development environment.
The first step is to open Microsoft Visual Studio, and create a new project. We'll name the project Sample and the solution will have the same name.
The project type is a Class Library and this Sample project will contain the business model of our application.


Secondly, we're going to create two other projects:
• A SQL Server 2008 Database Project (or SQL Server 2005 Database Project, depending on your configuration) named Sample.Persistence which will contain the generated scripts used to create the persistence layer,

• A Blank CodeFluent Entities Model (located in the “CodeFluent Entities” template category) named Sample.Model which will contain our CodeFluent Entities model and its related resources.


A default namespace dialog will pop-up, ensure the default namespace of your application is Sample.

Suppress the Class1.cs files that Visual Studio automatically creates for Class Library projects.
In the end, your solution should look as so:

images\Tutorials\Modeler Edition\PersistenceAndBom\VS2010CodeFluentEntitiesModel_thumb.jpg

 

Now that our Microsoft Visual Studio projects correspond to the CodeFluent Entities-generated application blocks; we're going to model our application in the Sample.Model project and configure CodeFluent Entities to generate the source code in the Sample.Persistence and Sample projects.

Modelling the business concepts

We are going to model a ridiculously simple application which can handle only three concepts: a customer that can order products.
This is done intentionally: the tutorial's purpose is to demonstrate how to set up a n-tier architecture with a custom web site as a front office. We won't demonstrate modelling principles in this tutorial.

Opening the Surface

Go to the Sample.Model project -> Surfaces:

images\Tutorials\Modeler Edition\PersistenceAndBom\Part_thumb.jpg


Double-click on Default, to open the surface.
This will open the Default surface. It is empty because we don’t have entities on this Surface yet.

[This is preliminary documentation and subject to change]

Note: In beta version, good practice is to save frequently in order to avoid any bad surprises.

Adding entities

Now, Right-Click in the Default surface -> Add -> Entity.

images\Tutorials\Modeler Edition\PersistenceAndBom\DefaultSurface_thumb.jpg


A window will pop up. Name the entity Customer and add it to the Sample namespace.

images\Tutorials\Modeler Edition\PersistenceAndBom\AddEntity_thumb.jpg

Repeat those steps for the Order and Product entities (add entities to the existing Sample namespace).
Your model should look like this :

images\Tutorials\Modeler Edition\PersistenceAndBom\AddNewEntityDialog_thumb.jpg

 

Adding properties to entities

Our entities were added to the model, we now can add their properties.

Right Click on the Customer entity -> Add -> Property

images\Tutorials\Modeler Edition\PersistenceAndBom\EntitiesCreated_thumb.jpg


In the dialog, name the property Id and set its type as an int.

Note: Instead of typing the type name yourself, you can also select it from a predefined list.
images\Tutorials\Modeler Edition\PersistenceAndBom\AddProperty_thumb.jpg

 

Add the following Standard Properties:

Customer:
- Id (int)
- Name (string)
- Address (string)

Order:
- Id (int)
- Code (string)
- Date (datetime)

Product:
- Id (int)
- Code (string)
- Label (string)
- Price (currency)

You can also add Advanced Type properties. Just add a new property to Customer, name it Orders, and in Type choose Advanced and browse for the desired type.
A new dialog will appear.

images\Tutorials\Modeler Edition\PersistenceAndBom\AddNewPropertyDialog_thumb.jpg

 

Select the Order type (Sample).

Add the following relation properties:

[TODO: Screenshot]


Here is the final model:

images\Tutorials\Modeler Edition\PersistenceAndBom\AddRelation_thumb.jpg


Now that our business logic is modeled, we're going to add producers.

Generating

Before generating, we need to configure our producers.
We want to generate the persistence and the business layers, so we simply have to add two producers: one for the persistence, and one for the BOM (Business Object Model).
To add a producer, Right Click on the default surface and select Add -> Producer

images\Tutorials\Modeler Edition\PersistenceAndBom\RelationResult_thumb.jpg


The “Add new producer” window will appear. We will first add a persistence producer which in our case will be SQL Server. To do so select “SQL Server” under “Persistence Producers”.

images\Tutorials\Modeler Edition\PersistenceAndBom\AddProducer_thumb.jpg

To add it, we need to specify where the producer will put the generated files. Browse the Target Directory (under Production) to open the “Browse For Project Folder” dialog. Select the Sample.Persistence project and click OK.

images\Tutorials\Modeler Edition\PersistenceAndBom\AddSqlServerProducerConfig_thumb.jpg

You may also need to specify the connection string of your SQL Server instance. Otherwise, it will use the default one (Application Name=[DefaultNamespace];server=127.0.0.1;database=[DefaultNamespace];Integrated Security=true).
To set the connection string, browse Connection String (under Production) and fill the popup with the desired information.

Example:

images\Tutorials\Modeler Edition\PersistenceAndBom\SqlProducerTargetDirectory_thumb.jpg
Note: Since the database wasn't generated yet, you don't have to specify a database name.

Confirm all your changes, and you will see a new area which appeared on the surface, next to your entities. This area will contain all your producers.

images\Tutorials\Modeler Edition\PersistenceAndBom\ConnectionString_thumb.jpg

Repeat the previous steps to add a Business Object Model Producer which will be used to generate files in the Sample class library project. Do not forget to set the Output Name to “{0}.dll

images\Tutorials\Modeler Edition\PersistenceAndBom\SQLProducerResult_thumb.jpg

Once added, your default surface should look like this:

images\Tutorials\Modeler Edition\PersistenceAndBom\BomProducerConfig_thumb.jpg

Building the Sample.Model project will generate the files. Build it and wait for the completion.

images\Tutorials\Modeler Edition\PersistenceAndBom\BomProducerResult_thumb.jpg

Upon completion, you will see files and references added to your solution.

images\Tutorials\Modeler Edition\PersistenceAndBom\Build_thumb.jpg

Congratulations, you have generated your first solution using CodeFluent Entities!


At this point, we generated the persistence layer (database, tables, stored procedures, constraints, etc.), and a set of CSharp classes allowing us to manipulate those persistence objects in upper layers (e.g. in the UI). We now have the foundations of all business applications: whether they are an ASP.NET Web Site, a rich client or a smart client, they will be built on top of those two layers.

See Also