CodeFluent Entities Documentation
The Persistence, the Business, and the Silverlight Object Model
Send comments on this topic.
CodeFluent Entities > Tutorials > Using The Core Edition > Developing Silverlight Applications > The Persistence, the Business, and the Silverlight Object Model

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:

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.SilverApp 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 Silverlight Object Model (SLOM), and the WCF configuration files for the services as well as the smart client.

Therefore, we'll configure five producers: the SQL Server Producer, the Business Object Model Producer, the Service 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 name="SQL Server" typeName="CodeFluent.Producers.SqlServer.SqlServerProducer, CodeFluent.Producers.SqlServer">
    <configuration targetDirectory="../{0}.Persistence"/>
  </cf:producer>
  <cf:producer name="Business Object Model" typeName="CodeFluent.Producers.CodeDom.CodeDomProducer, CodeFluent.Producers.CodeDom">
    <configuration
      compileWithVisualStudio="true"
      outputName="bin\Debug\{0}.dll"
      targetDirectory="../{0}">
      <subProducer typeName="CodeFluent.Producers.ServiceModel.ServiceProducer, CodeFluent.Producers.ServiceModel"
                   targetDirectory="..\{0}.SilverApp" />
    </configuration>
  </cf:producer>
  <cf:producer name="Server Configuration" typeName="CodeFluent.Producers.CodeDom.TemplateProducer, CodeFluent.Producers.CodeDom" >
    <configuration
      sourceDirectory="Templates\ServiceModel\Server"
      targetDirectory="..\{0}.SilverWeb">
    </configuration>
  </cf:producer>
  <cf:producer name="Client Configuration" typeName="CodeFluent.Producers.CodeDom.TemplateProducer, CodeFluent.Producers.CodeDom" >
    <configuration
      sourceDirectory="Templates\ServiceModel\Client"
      targetDirectory="..\{0}.SilverApp">
    </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: The templates used as the source directory of the template producers aren't official templates and aren't shipped with the product. However, you can find the source of the templates in the Application Configuration article of the Developing Silverlight Applications section.
Note: If using Silverlight 3 or upper, you're going to need to expose serialization callbacks to the serializer in addition to what was described previously. More information is available here: Serialization Callbacks

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 the server and client configuration files.

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:


Now that our object model and its services build, we need to include the Silverlight Object Model (SLOM) in our Sample.SilverApp application so we can use our BOM remotely. To do so:

 

At this point, the persistence layer was generated, the Business Object Model (BOM) was built, and the Silverlight Object Model (SLOM) - which is in fact an enhanced version consuming through WCF services the business object model (BOM) contained in the Sample assembly - was also built.

Here's a screenshot of what the Sample.SilverApp project should look like:


 

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 service layer. In this case, the smart client object model is contained in a Silverlight assembly, and references the CodeFluent Silverlight Runtime making it functional in a Silverlight environment. Therefore, the smart client object model in the Silverlight environment is commonly referred to as the Silverlight Object Model (SLOM).