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

Glossary Item Box

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 Class Library projects:

 

In the end, your solution should look as so:

Starting Solution

Suppress the Class1.cs files that Visual Studio automatically creates for Class Library projects.

Now that our Visual Studio projects correspond to the CodeFluent-generated application blocks; we're going to model our application in the Sample.Design project and configure CodeFluent 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.

Note: If you've already created your model through the CodeFluent Starter Wizard, you can skip this part up to the Generate.bat creation.

Add the created model parts to the Sample.Design project, update the Generate.bat so it builds the Sample.Producers.xml (in models generated by the wizard, it's the producer part which imports the other parts unlike the one hereunder).

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 modeling principles in this tutorial.

To do so:

The Sample.xml file will be our main CodeFluent model part. Copy and paste the following content in it:

Sample.xml Copy Code
<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
            defaultNamespace="Sample">
  <cf:import path="Sample.Producers.xml"/>
  <Customer>
    <Id typeName="int"/>
    <Name typeName="string"/>
    <Address typeName="string"/>
    <Orders typeName="OrderCollection"/>
  </Customer>
 
  <Order>
    <Id typeName="int"/>
    <Code typeName="string"/>
    <Date typeName="DateTime"/>
    <Customer typeName="Customer"/>
    <Products typeName="ProductCollection"/>
  </Order>
  <Product>
    <Id typeName="int"/>
    <Code typeName="string" collectionKey="true"/>
    <Label typeName="string"/>
    <Price typeName="Money"/>
  </Product>
</cf:project>

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

Generating

Before generating, we need to configure our producers. We'll add the producers in an other part named Sample.Producers.xml (which is already imported by the main part though the cf:import node), so that the business logic and the producer logic don't get mixed-up.

To do so:

The Sample.Producers.xml file is a new model part that will contain our desired production logic. In our case, since we want to produce a Microsoft SQL Server database and a CSharp Business Object Model (BOM); we'll configure two producers, each one target their own platform. Moreover, since we're working with Visual Studio, we'll specify them to not compile the generated sources, and to generate them in our Visual Studio projects.

Copy and paste the following content in the Sample.Producers.xml:

Sample.Producers.xml Copy Code
<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
            defaultNamespace="Sample"
            defaultConnectionString="server=(local);database={1};Integrated Security=true">
  <cf:producer typeName="CodeFluent.Producers.SqlServer.SqlServerProducer, CodeFluent.Producers.SqlServer">
    <configuration targetDirectory="../{0}.Persistence"/>
  </cf:producer>
  <cf:producer typeName="CodeFluent.Producers.CodeDom.CodeDomProducer, CodeFluent.Producers.CodeDom">
    <configuration
      outputName="bin\Debug\{0}.dll"
      compileWithVisualStudio="true"
      targetDirectory="../{0}">
    </configuration>
  </cf:producer>
</cf:project>

Note: {0} corresponds to the value defined in the defaultNamespace attribute of the cf:project node.

{1} corresponds to a database-friendly version of the same value, meaning that all invalid characters that the default namespace might contain are replaced by '_'. For instance, the default namespace Test.Test1 will become Test_Test1.

Note: You might have to update the default connection string to one matching your environment.

Last but not least, we're going to create a batch file that will generate our application. To do so:

Open it, and copy/paste the following script:

Generate.bat Copy Code
@ECHO OFF
call "%ProgramFiles%\SoftFluent\CodeFluent\Current\cfvars.bat"
call "%CF_CURRENT_PATH%\CodeFluent.Build.exe" Sample.xml

We are now ready to generate the Sample application. Open a command prompt in the Sample.Design directory, and launch the Generate.bat file.

The following output should be displayed:


Producers ran and:

  1. Generated all SQL scripts needed to create the modeled database and its content (tables, columns, stored procedures, relations, etc.),
  2. Ran the scripts on the SQL server engine pointed by the connection string to create the Sample database,
  3. Produced all CSharp classes from the modeled concepts.
Note: Generation of the SQL Server objects relies on the Microsoft SQL Server Management Objects (SQL SMO) and the Microsoft SQL Distributed Management Objects (SQL DMO) technologies. Those components are installed when installing complete versions of Microsoft Visual Studio. Consequently, if you're running an Express edition of Visual Studio you might not have those components. To use the CodeFluent automated generation feature for SQL Server in such environments, it's possible to install those components by installing the Microsoft SQL Server 2008 Feature Pack available here.

All source code was generated in the folders pointed by our producers, the next step is now to add those files in Visual Studio and to their respective projects.

Including the generated source files in Visual Studio

To do so:


Provided your solution looks like the figure above, build the Sample project.

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 a ASP.NET Web Site, a rich client or a smart client, they will be built on top of those two layers.

See Also