Skip to content

SG Getting Started

MaudChiva edited this page Mar 8, 2023 · 2 revisions

Getting Started - Tutorial

SoftFluent CodeModeler is used with a Model Driven approach to development. You design your business model first. From this model, some configured technical producers generate the required source code for the business and data layers of your application. This source code will be updated whenever the model is updated.

Nota : the c# code uses partial classes so that your own code is not impacted when the CodeModeler model is regenerated. We will see later how to inject your own code in CodeModeler model itself to extend the generated code.

You will find below how to create a very simple CodeModeler project.

To learn more advanced subjects, refer to the Architect Guide and the Developer Guide from Home.

Design your first business model

Create a project from the list of Visual Studio templates :

  • select the SoftFluent CodeModeler category in Visual Studio 2017
  • search for CodeModeler or Soft in the search template box in Visual Studio 2019

Select the "Blank " Model template, and name it as you want (ex: OrderProcess), we will use it as the default .Net namespace.

Create an entity

To create your CodeModeler model you need to edit the Default surface project item.

CodeModeler allows you to define entities and enumerations. To do so you will use the ribbon on the top of the graphical modeler surface integrated into the Visual Studio.

For instance, create a new Entity named “Product” in a new OrderProcess.Marketing namespace.

Add a property

After creating a new entity like “Product” above, you need to add some properties to it. In our example we add “Id”, “Name”, “Price”.

The first property added will be the key by default.

If you want more information about the “Keys” you can go to the Keys section.

When you add a property to an entity, you also need to specify the property type, which can be a simple type (integer, string, currency, date, time, etc.) or a more advanced one (images, files, entity, entity collection, enumeration). This type will be automatically mapped to .Net CLR types and database types by CodeModeler producers.

The model is persisted physically as a “part” file (*.sfcm). You can see them in the Parts folder of the Visual Studio project. Do not edit these files.

In the Model folder, you can see all namespaces and elements (classes, enumerations) of your model, that are displayed on the design surface.

Generate your first data and business object model layers

In order to generate the different layers of code from the model, you first need to create two Visual Studio projects to host the related generated source code:

- C# class library (.Net Standard) project - OrderProcess.BOM

- SQL Server database or an empty project - OrderProcess.Persistence

OrderProcess.BOM will contain the .NET object model generated from your CodeModeler Model by the Business Object Model (BOM) (see below) :

  • Your entities will become classes, properties will become properties, methods will become methods and so on. The generated .NET classes do not derive from a base technical class and are all partial classes so you can extend them easily without losing your changes.

  • Absolutely no dynamic code is created to keep the code simple, predictable and dev-friendly to debug**.**

OrderProcess.Persistence will contain all the SQL scripts generated.

Generate the persistence layer

Now you need to add a "Producer" to generate the persistence layer code.

Right click on folder “Producer” and select Add new Producer.

In Persistence Layer Producers, select the target type of your Database : Microsoft SQL Server (for SQL and SQL Azure), mySQL or PostgreSQL.

Then click on suspension points.. and select your SQL Project.

Nota Bene: Default target version of SQL Server is SQL Server 2016.

In our example, the project folder to select is OrderProcess.Persistence

During the build of your solution, CodeModeler generates the SQL scripts in the project OrderProcess.Persistence

The most important scripts at this point are:

  • _tables.sql: it’s used to create the database schema from scratch. For each entity it includes the related table, view with their constraints such as the unicity, or primary and foreign keys.

  • _table_diffs.sql: it’s a migration script. CodeModeler compares the difference between your model and the existing database (already generated) to generate this. This is the script applied after the second generation.

  • _procedures.sql: it’s used to create or update the stored procedures used to access the data from the business layer.

Generate the Business Object Model

Now add the Producer for your C# data access and business layer and target the C# class library project (.Net Standard).

Right Click on folder “Producer” and select Business Layer Producers, like before.

Click on Business Object Model (BOM) and target the C# class library project folder.

Now you have two producers, the SQL Server and the BOM.

You can build your CodeModeler’s project.

As you can see, in our C# class library project (OrderProcess.BOM) you now have 2 generated folders, Marketing and Sales. One folder is created for each namespace to follow Microsoft C# development guidelines.

In our Marketing folder,you have 2 files: Product.cs and ProductCollection.cs : for each entity in the model, 2 .NET classes are generated : “NameYourEntity.cs” and “NameYourEntityCollection.cs”. They include all the code needed for the typical CRUD (Create , Read, Update, Delete) methods.

In our example,

  • Product.cs contains the Product class with its properties and its methods. In particular the Save for new or updated product, the Delete method and the Load method(s) using the key property(ies) are generated. It also includes the automatic call to validation checks needed before saving the entity depending on the properties defined.

  • ProductCollection contains the properties and the methods used for handling set/collection of Product instances like loading all products, loading product(s) by some properties or searching. Loading methods include advanced concepts for you like paginated and non paginated loads.

Connection string configuration for build and at runtime

By default, a default connection string is created and used by database level CodeModeler runtime and during the model build. It is defined using the default store name as the database name and the local address 127.0.0.1 as the database server.

This default connection string can be overridden using the CodeModeler project level property Default Connection String

The connection string to be used at runtime needs to be set by configuration

Configuration

  • In a .Net Framework usage context
    The runtime (CodeModelerConfiguration) looks for a XML element named after the current store name in the .Net default configuration file(s), which is in the following order

    1. CodeModeler.config

    2. current loaded .Net configuration

    3. explicit look for web.config

    4. explicit look for xxx.exe.config

  • In a .Net Core usage context
    A XML file named CodeModeler.config can be added as a content file to the .Net Core project.
    CodeModelerConfiguration looks for a XML element named after the current store name

The attribute connectionString will then be used

CodeModeler.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <OrderProcess connectionString="Application
        Name=OrderProcessProd;Server=.;Initial Catalog=OrderProcess;Integrated
        Security=SSPI" />

</configuration>

(.Net Framework applications) App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
    <configSections>
        <section name="OrderProcess"
            type="System.Configuration.SingleTagSectionHandler"/>
    </configSections>

    <OrderProcess connectionString="Application
        Name=OrderProcess;Server=.;Initial Catalog=OrderProcess;Integrated
        Security=SSPI"/>

</configuration>

Setting connection string by code

When Xml Config file can not be used (serverless or other reason), you can set the CodeModeler.Runtime.CodeModelerPersistence.GlobalConnectionString property that set it for all future calls from any current process threads and use the configuration feature you use for other configuration values ( environment variables, json files etc..)

Default Store Name

The default store name is created after the namespace of the CodeModeler project added to the solution (here OrderProcess).

In the .Net Standard project Library hosting the BOM Producer generated code, it can be found in the _cm_rt.cs file for the Constants class: here OrderProcessStoreName, so "OrderProcess"

\[System.CodeDom.Compiler.GeneratedCodeAttribute("CodeModeler",
"1.0.01234.05678")\]

public sealed partial class Constants

{

    public const string OrderProcessStoreName = "OrderProcess";

....

}
Clone this wiki locally