You are here
Home > Programming > CodeFluent Entities – A Tool For Developers by Developers

CodeFluent Entities – A Tool For Developers by Developers

Fallback Image

CodeFluent EntitiesCodeFluent Entities – I love being a lazy software developer, especially when a great tool such as CodeFluent Entities is involved. It is incredible how much control this tool puts in my hands while allowing me to generate code with ease. In this article I will show you how to create your first project and generate your database scripts and data access code.

CodeFluent Entities – Starter Wizard

To get you going, CodeFluent Entities allows you to use a Starter Wizard. This is the quickest way to jump into the thick of things. As I am sure with most developers, the way I learn and evaluate a new tool, is to use it. I believe that the ease with which a tool allows you to use it, determines the overall adoption by a developer. CodeFluent Entities could not be easier to use. Head on over to the menu item and select the Starter Wizard (I am using Visual Studio 2013 Ultimate).

CodeFluent Entities

You will be presented with a New Project dialog screen. Select the Starter Wizard and click on OK.

CodeFluent Entities

For the purposes of this demo, I will select to create a blank CodeFluent Entities model. You will use this model to describe your business application and target the required technologies and layers from there.

CodeFluent Entities

Next you need to select your application’s architecture. As you can see, there is quite a selection of architectures. From Windows Forms all the way through to an ASP.NET MVC2 app. Select your preference and click next.

CodeFluent Entities

C# and VB.NET developers both get equal amounts of love on the object language screen. I’m a sharpie, so C# is the choice for me.

CodeFluent Entities

I’m going to create a car sales application. So I just named my application accordingly.

CodeFluent Entities

For your database, CodeFluent has a nice spread available for you too. This is called the persistence layer. Here I will just select Microsoft SQL Server.

CodeFluent Entities

The next screen allows me to set up my connection string.

CodeFluent Entities

Because I haven’t created my carSales database yet, CodeFluent Entities tests my connection to the Master database.

CodeFluent Entities

After you have configured your database connection, CodeFluent Entities will give you a summary overview of your model.

CodeFluent Entities

Clicking next will start the project creation process.

CodeFluent Entities

When this has completed, you will see the Actions Report displayed.

CodeFluent Entities

In Visual Studio’s solution explorer you will now see the created projects. These consist of the client, a class project for data access, a model and persistence projects.

CodeFluent Entities

The next part is where the fun begins.

CodeFluent Entities – Define Your Model

What we need to do next is define the business logic of the application via a model. This model will use entities that can be easily modified at a later stage should additional business requirements come to light or change. In the model, expand the Surfaces folder. This is the canvass I will use to design my application. You can create other surfaces to model your application with for different aspects of your application (think namespaces). For now I will just go with the default.

1 double click default

After double clicking on the default surface, it will open up in Visual Studio as an empty surface void of entities. The idea behind this demo application is to create a car sales application. So we need to model two entities namely vehicles and sales persons.

2 default surface open

Click on the ‘Add Entity’ button and define your entity name. Because I already have the existing carSales namespace, I’ll add my entities to that.

3 add new entity

You will see CodeFluent Entities now adds the Vehicle entity to the default surface.

4 vehicle entity added

The next thing I need to do is to add properties to this entity.

5 add property

The first property I’ll add is the Id property which will act as the primary key.

6 add id property

Continue adding the required properties until you are satisfied you have added them all.

7 all properties added

Next, I obviously need to add a sales person to sell my vehicles. By repeating the steps above, I can add the SalesPerson entity to the default surface.

8 add all entities and properties

If we now think about this specific instance and the relationship between sales persons and vehicles, we can say the following. A sales person can sell more than one vehicle (at least we hope so), but a specific vehicle can only be sold by one sales person. So we can define this relationship between these entities.

9 Add relationship

Add a new property to the Vehicle entity and select the advanced type.

10 Choose Salesperson Type

For this type, I will select the carSales.SalesPerson type. CodeFluent Entities will now ask me to add the relation between Vehicle and SalesPerson. For now I will just leave the related property as unspecified.

11 Add new relationship

CodeFluent will now specify the relationship between the two entities visually by joining them with a relationship indicator.

12 relationship added

CodeFluent Entities – Generating Code

Continue adding entities to your default surface until you have modeled your application completely. The next step is to generate some code and database scripts. In the carSales project, create a folder for your database scripts. You can call this folder whatever you like.

13 add scripts folder

Then head on over to the Producers folder. CodeFluent entities have a few producers you can use out of the box.

14 double click on sql server producer

You can add more as needed by right clicking on the Producers folder and selecting ‘Add New Producer’. For now I will just be using the SQL and BOM producers.

15 change target directory

In the properties for the SQL Server Producer, I now define the output (or target) directory to be the ‘Persistence Scripts’ folder created earlier. Clicking on the browse button allows me to select the correct folder.

16 change to persistence scripts folder

Next, I will select the properties for the BOM (Business Object Model) Producer. This producer will create the code for me that is needed to connect to and write data to and read data from my database.

17 double click bom producer

I select the BOM Producer to output to my carSales project.

18 select carSales project

Lastly, I don’t need the default Class1.cs project. So I will just delete this.

19 delete class1

At last we come to the magic part. We are ready to generate our project. To do this, right click on the CodeFluent Entities project and select ‘Build’ from the context menu. CodeFluent Entities will now call each of the specified producers one-by-one to generate your code. The SQL Producer will create the T-SQL scripts and execute them on your configured SQL server. It will then add the created scripts to your previously defined target folder. The BOM Producer will then create the corresponding C# classes and add them to the class library project I identified earlier. CodeFluent also adds the required references making this project valid and able to compile right away.

20 build project

Heading over to SQL Server Management Studio, I can see the created tables, views and stored procedures that were scripted from the previous CodeFluent project build.

21 view carSales Database

Writing The Code

Back in Visual Studio you will see that the required references have been added to the Client project.

22 Client references

The last required step is to open up the App.config file and add a connection string to this file. Be sure to add the configSections tag and carSales (or whatever you have called your project) connection string before the startup tag, else a connection will not be established successfully to the database.

23 client app config add connection string

The code to add an entry into the Vehicle table is just as easy to write. Instantiate a new Vehicle object and the properties defined previously in our model will be available in the code.

24 add code to form load

Calling the Save method on the code will write the data to my Vehicle table. This is evident in SQL Server Management Studio when viewing the Vehicle table.

25 table data inserted for vehicle

As you can see from the simple app above, it is very easy to write working code by creating a model of your application logic first. If at any time something changes, you can simply change the model and generate the project code again. This will update all the Business Object model code as well as the database scripts and tables if required. Give CodeFluent Entities a try by requesting your free, fully featured and unlimited time license for Personal and academic use. The Personal license is limited to only 50 entities though. This should be enough for any evaluation or small personal project.

Dirk Strauss
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has almost mastered.

Similar Articles