SoftFluent

Tools for developers, by developers.


Welcome

This page provides content to get you started using CodeFluent Entities. It assumes you have already downloaded and activated the product. If not, you can:

Once you are all set and ready go, let's get started!


Introduction

In this section, we'll guide you through the import of an existing SQL Server database to a CodeFluent Entities model and the generation of a brand new .NET application from this new model.

Note: Since we need to start from an existing database, you'll need to download the "Nerd Dinner" sample application available on CodePlex so we can start from this provided database.


Importing & creating your solution

Open Visual Studio 2008 / 2010 / 2012, open the "CodeFluent Entities" menu and select the "Import an Existing Database or Model..." menu:

 

 

This will launch the Import Starter Wizard:

 

 

Click Next to create your Visual Studio solution:

 

 

Type-in:

  • Location: <A desired location on your disk such as "C:\Temp" or "C:\Projects">
  • Solution name: NerdDinner

Keep the "Import Starter Wizard" project template and click OK, to start importing your existing database.

The first step is to select which importer you're going to use:

 

 

In this case since we're importing the SQL Server 2008 NerdDinner database, we'll pick the "Microsoft SQL Server 2000 or higher database" importer and click Next.

On the next page you'll be able to define the connection string to connect to your existing database:

 

 

Please note that if you just want to import a subset of all tables available in the database you can do this using the "Table Names" property. Though not necessary for the NerdDinner database, this can be handy when working on big databases. For instance, here's what you'd see when importing the AdventureWorks database:

 

 

Now that your import is configured the wizard will propose you to save this configuration if desired so that starting over again you won't have to go through screens over again.

 

 

As import is now configured, following screens aim at preparing the brand new solution.

Start by selecting your architecture (Windows Forms Application for instance):

 

 

Your language (C#):

 

 

Your persistence layer (SQL Server in our case):

 

 

Once you selected your soon-to-be persistence layer (i.e. SQL Server for us), you need to define your target connection string (not to be confused with the one we defined earlier to import!):

 

 

Here, as you can see, I typed in a database name which does not exist yet, "NewNerdDinner", so CodeFluent Entities will create it for us. This way we won't alter the original "NerdDinner" one from which we're importing.

Note: Please note that you could totally generate on the original one if desired, however this topic bounds to illustrate how to create a brand new .NET application from an existing database.



 

Once this is done we're ready to finish this wizard which will:

  • Import our existing NerdDinner database into a NerdDinner CodeFluent Entities model,
  • Create all projects corresponding to our configured outputs,
  • Add and configure producers to our model pointing to those projects.

Click finish and you'll see the magic happening :)

 

Generate

Now that you have a platform independent model representing your application, we'll generate some code from it. CodeFluent Entities will parse the designed model and infer an in-memory representation of this model. This inferred meta-model will then be passed to each declared producer which will translate this meta-model into code.

As you understood, a producer is a code generator and CodeFluent Entities provides about 20 different producers out-of-the-box. Completing the wizard as we did previously, the wizard automatically added and configured the following producers in our project:

  • The SQL Server Producer which generates and deploys T-SQL scripts and that we'll use to create our persistence layer,
  • The .NET Business Object Model Producer which generates a set of .NET classes (C# or VB.NET) allowing us to manipulate database objects (the Business Object Model which we mentioned earlier).

As the producers were already added and configured by the wizard to generate code in our target projects: all we have to do is build our project.

To do so, right-click on your CodeFluent Entities project, and select "Build". CodeFluent Entities will:

  • call each of your producer one by one to generate your code,
  • the SQL Server Producer will generate T-SQL scripts, run them on your configured SQL Server, and add the files to the "NerdDinner.Persistence" project
  • the Business Object Model Producer will generate the corresponding C# classes, add them to your class library project ("NerdDinner") and add the appropriate references so you can compile your business object model right away.

Build the project, verify that everything compiles and you're good to go!


Use the generated code

Now that we created our application's database and API, let's create a UI using the generated code.

  • Add references to the NerdDinner class library project and the CodeFluent.Runtime.dll,
  • Rename Form1.cs into MainForm.cs,
  • Open the code-behind for MainForm.cs and paste the following code:

namespace NerdDinner.Client
{
    public partial class MainForm : Form
    {
        private Random _rand;

        public MainForm()
        {
            InitializeComponent();

            _rand = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
        }

        private void OnRefreshButtonClick(object sender, EventArgs e)
        {
            _dataGridView.DataSource = DinnerCollection.LoadAll();
        }

        private void OnCreateButtonClick(object sender, EventArgs e)
        {
            Dinner dinner = new Dinner();
            dinner.Title = "Sample Dinner #" + _rand.Next(10000);
            dinner.Description = "Yet another nerd dinner.";
            
            dinner.HostedBy = "SoftFluent";
            dinner.EventDate = DateTime.Today;

            dinner.Address = string.Format("{0}  Main Street", _rand.Next(1000));
            dinner.ContactPhone = string.Format("{0}-{1}-{2}", 
                _rand.Next(100, 999), _rand.Next(100, 999), _rand.Next(1000, 9999));
            dinner.Country = "USA";
            dinner.Save();
        }
    }
}

  • In the Windows Forms designer, add two buttons and plug each one of them to their corresponding event handler we have just created,
  • then add a DataGridView named "_dataGridView", configure it to display Dinner objects, and select the columns you want to display from this class:


  • Last but not least, add an application configuration file to your application and paste the following (don't forget to replace the connection string with your own):

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="NerdDinner"  type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
  </configSections>
  <NerdDinner
connectionString="database=NewNerdDinner;server=SFCAN03;Trusted_Connection=true" />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

Compile and run the project and here's what you should get:


Customer's opinion

Richard Clark - C2I

"As a service company, CodeFluent Entities empowers us to stand out at low cost while reducing implementation risks."


Sébastien Mizon - Clovidis

I have used CodeFluent Entities for over 2 years and it has completely changed my way of implementing .NET applications. I can't do without it any more!


Peter Standford - Artefaction

SoftFluent sales staff have been responsive without being pushy and it’s refreshing to find a company that knows the quality, support and productivity improvements available through their product is more important to developers than anything else.

Related Information


MVP


Resources