Home > Developing Using CodeFluent Entities, News, PostgreSQL, SoftFluent Products > Introducing CodeFluent Entities PostgreSQL Producer

Introducing CodeFluent Entities PostgreSQL Producer


CodeFluent Entities 2012 – Build 060809 introduced a new Persistence layer producer: the PostgreSQL Producer!

The producer supports PostgreSQL 8.4 and above (8.3 is soon to be discontinued) and relies on the ADO.NET data provider Npgsql 2.0.11. The producer will try to load dynamically the Npgsql assembly. (You might need to copy the Npgsql.dll and Mono.Security.dll in your CodeFluent Entities installation directory if you don’t want to register Npgsql in the GAC).

Please also note the PostgreSQL Producer also requires the liboss-uuid library to be installed on your server. On Windows, this library is shipped with the standard Windows packages of PostgreSQL 8.4+, and on Unix/Linux, you may need to install the postgresql-contrib package and its dependencies like libossp-uuid16, for the producer to be able to deploy the generated script on your server.

Getting started

The producer is available in the Persistence Layer Producers section of the Add New Producer window:

as well as directly from the Starter Wizard:

This is how you can set the connection string (in the Wizard pages):

A first model

I created a new project with the Start Wizard and design this very simple two-entity model:

Basically all the employees have a first name, a last name, a birth date, and a job. Each job has salary – of type currency, a name and employees. I added two instances to the model: an employee named John Doe, born on June 12th, 1974 with a job named CEO and which salary is 30,000.

I just set the Target Version of the PostgreSQL Producer to version 9.0 which the version I am currently running on my computer, and built the project.In the output window I can see the result of the generation process; the PostgreSQL producer generated 11 files and succeeded:

All the automatically generated .SQL script files are available in the Persistence project of the solution:

If I connect to my database with an administration tool I can see that a new database has been created with the named I specified in the Connection String, and populated with my model and instances:

And I haven’t written a line of code yet!

Consuming the generated persistence layer

Consuming the PostgreSQL generated database is as easy as configuring the right connection string in the client project. In my case I created a Console Application Project named MyCompany.Management.Client.

To prepare it, I had to reference my BOM Project – in my case MyCompany.Management – that was automatically created by the Starter Wizard, Npgsql.dll and CodeFluent Entities runtime, i.e. CodeFluent.Runtime.dll and CodeFluent.Runtime.PostgreSQL.dll. Note that you might need to set the Copy Local property to True for the Npgsql.dll reference if the dll is not registered in the GAC.

I also had to change the target framework of the project from .NET Framework 4.0 Client Profile to the full .NET Framework 4.0.

As I am using the PostgreSQL Producer I have to set the correct Connection String in App.config file:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="MyCompany.Management" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
  </configSections>
  <MyCompany.Management persistenceTypeName="PostgreSQL" connectionString="Server=localhost;Database=MyCompany_Management;User Id=postgres;Pwd=password" />
</configuration>

Note it is important to specify the persistenceTypeName=“PostgreSQL” attribute.

Here is now the code of my console application :

using System;
namespace MyCompany.Management.Client
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create a new employee
      Employee newbie = new Employee();
      newbie.FirstName = "Jane";
      newbie.LastName = "Doe";
      newbie.BirthDate = new DateTime(1990, 01, 15);

      // Persist Jane Doe
      newbie.Save();

      // Load all the employees
      EmployeeCollection employees = EmployeeCollection.LoadAll();
      foreach (Employee employee in employees)
      {
        Console.Write("- {0} {1}, born on {2:MM-dd-yyyy}",
          employee.FirstName,
          employee.LastName,
          employee.BirthDate);

        if (employee.Job != null)
        {
          Console.Write(", is {0} with a salary of {1:C}",
            employee.Job.Name,
            employee.Job.Salary);
        }

        Console.WriteLine();
      }
    }
  }
}

It creates a new employee, saves it, and then loads a list of all the employees. As we can see, there is no specific code about PostgreSQL. Its use is completely transparent at this level.

And here is the result:

And the populated table:

Happy PostgreSQL-ing!

Baudouin Giard

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 41 other followers