CodeFluent 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).
You will be presented with a New Project dialog screen. Select the Starter Wizard and click on OK.
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.
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.
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.
I’m going to create a car sales application. So I just named my application accordingly.
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.
The next screen allows me to set up my connection string.
Because I haven’t created my carSales database yet, CodeFluent Entities tests my connection to the Master database.
After you have configured your database connection, CodeFluent Entities will give you a summary overview of your model.
Clicking next will start the project creation process.
When this has completed, you will see the Actions Report displayed.
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.
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.
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.
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.
You will see CodeFluent Entities now adds the Vehicle entity to the default surface.
The next thing I need to do is to add properties to this entity.
The first property I’ll add is the Id property which will act as the primary key.
Continue adding the required properties until you are satisfied you have added them all.
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.
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.
Add a new property to the Vehicle entity and select the advanced 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.
CodeFluent will now specify the relationship between the two entities visually by joining them with a relationship indicator.
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.
Then head on over to the Producers folder. CodeFluent entities have a few producers you can use out of the box.
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.
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.
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.
I select the BOM Producer to output to my carSales project.
Lastly, I don’t need the default Class1.cs project. So I will just delete this.
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.
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.
Writing The Code
Back in Visual Studio you will see that the required references have been added to the Client project.
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.
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.
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.
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.