CodeFluent Entities Documentation
The Persistence, the Business, and the Service Layers
See Also Send comments on this topic.
CodeFluent Entities > Tutorials > Using The Core Edition > Developing Desktop Applications > Developing Smart Clients > The Persistence, the Business, and the Service 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.

Then, we're going to create several other projects:

Note: The Sample.Proxy project also depends on smart client targeted platform. For instance, if the client is Silverlight client, then the Sample.Proxy should be a Silverlight Class Library.

It's also important to note that you don't have to separate the proxy from the client: it's totally possible to embed the proxy in the client in order to suppress the distinction if desired.

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, Sample, and Sample.Proxy projects.

Modeling 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, and 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). Then update the targetDirectory attributes so it points to your Visual Studio project directories and add the template producers in order to instruct CodeFluent to automatically generate the WCF configuration files.

This is done intentionally: the tutorial's purpose is to demonstrate how to set up a n-tier architecture with a database managed by an object model and exposed through WCF services, consumed by a Windows Forms smart client. 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 thanks to the cf:import node), so that it doesn't mix the main part containing our business logic.

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, a CSharp Business Object Model (BOM), a Smart Client Object Model (SCOM) so we can consume the generated services, and produce the WCF configuration files for the services as well as the smart client.

Therefore, we'll configure five producers: SQL Server Producer, Business Object Model ProducerService Object Model Producer, and the Template Producer twice, to produce each configuration file in the desired Visual Studio project. 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={0};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
      compileWithVisualStudio="true"
      outputName="bin\Debug\{0}.dll"
      targetDirectory="../{0}"
      produceWebBitsServer="false"
      produceWebHandler="false">
      <subProducer typeName="CodeFluent.Producers.ServiceModel.ServiceProducer, CodeFluent.Producers.ServiceModel" targetDirectory="..\{0}.Proxy" />
    </configuration>
  </cf:producer>
  <cf:producer name="ServerConfiguration" typeName="CodeFluent.Producers.CodeDom.TemplateProducer, CodeFluent.Producers.CodeDom">
    <configuration
      sourceDirectory="%CF_TEMPLATES_PATH%\ServiceModel\Server"
      targetDirectory="..\{0}">
    </configuration>
  </cf:producer>
  <cf:producer name="ClientConfiguration" typeName="CodeFluent.Producers.CodeDom.TemplateProducer, CodeFluent.Producers.CodeDom">
    <configuration
      sourceDirectory="%CF_TEMPLATES_PATH%\ServiceModel\Client"
      targetDirectory="..\{0}.SmartClient">
    </configuration>
  </cf:producer>
</cf:project>

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

Note: {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.

Note: produceWebBitsServer and produceWebHandler, are generated by default in order to handle binary large objects (BLOB) in web environments. We disabled the generation of those two classes because we won't use them in this tutorial.

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
Note: cfvars.bat is a batch file shipped with CodeFluent which defines a set of environment variables such as the CF_CURRENT_PATH which point to the installation folder containing the current CodeFluent build or the CF_TEMPLATES_PATH, used here-above, and which points to SoftFluent official templates shipped with CodeFluent.

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 display:


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,
  4. Produced WCF services, contracts and operations exposing the business object model,
  5. Produced an enhanced proxy providing a remote object model taking care of all communication matters,
  6. Produced WCF configuration files: one for the server and one for the client.

All the source code was generated in the defined folders, so to view them in Visual Studio we need to add the newly generated files to their respective projects.

Including the generated source files in Visual Studio

To do so:


Even though the project actually already builds without the CodeFluent.Runtime.ServiceHost.exe, we're going to add a reference to the CodeFluent.Runtime.ServiceHost.exe that SoftFluent provides to host Windows services, in order to ease service developments. In addition, we're also going to set its configuration file (CodeFluent.Runtime.ServiceHost.exe.config) that we included in the project earlier as a content file, and set its Copy to Output Directory property to Copy Always.

The result of these actions, is that when we build our Sample project, the service hoster and its configuration are automatically copied to the output directory. We're doing this in order to be able to easily host our Service Object Model in the service hoster so we can easily debug them.

The last step to be able to do so, is to configure, in the properties of the Sample project, its start action to use the CodeFluent.Runtime.ServiceHost.exe in the Sample\bin\Debug directory as the start program (you need to build Sample once before to have the service hoster in the output directory).


Now that the services are ready to be used we need to configure the proxy:

At this point, the persistence layer was generated, the business object model was built, and the services are automatically hosted in the CodeFluent service hoster so we can easily debug our application. Furthermore, our proxy was generated and is now ready to be built.

Here's a screenshot of what the proxy project should look like:


Note: Depending on the targeted platform a reference to a different CodeFluent.Runtime might be required. For instance, were the smart client a Silverlight 2.0 one, the CodeFluent.Runtime.Silverlight.dll should be referenced instead of the CodeFluent.Runtime.dll. In the case of a Silverlight 3.0 client, the CodeFluent.Runtime.Silverlight3.dll should be referenced.

 

In conclusion, the data tier was created and is a SQL Server database in which the application will persist its data. On top of that, we generated a middle tier which contains a set of objects enabling developers to:

  1. manipulate the data objects stored in the persistence layer,

  2. define the business logic of our application.

Therefore, this middle tier is known as the Business Object Model (BOM). In the end, whatever the user interface or the type of architecture is, the persistence layer (containing the data) and the business layer (containing the business logic) are always the fundamental pillars of an application.

On top of those fundamentals, we generated a set of services (aka the service layer) which expose our BOM and allow smart clients to consume it remotely. This set of services corresponds to the services layer.

See Also