Home > ASP.NET, Developing Using CodeFluent Entities > Get started with the CodeFluent Entities API

## Get started with the CodeFluent Entities API

The CodeFluent Entities API has been around for a while, and this post will help you get started with it! If I tell you that the API is very easy to use and extremely powerful, you will not believe me. Hopefully, you will believe me by the end of this post.

We assume that we have a CodeFluent Entities solution, with its CodeFluent project and its class library for the BOM. We also have created an empty console application.

In this console application, we need to reference 3 libraries:

• CodeFluent.Model
• CodeFluent.Model.Common
• CodeFluent.Runtime

We are now ready to write some code. First we need to load the CodeFluent model, which is a cfp file. This cfp file is an XML file that corresponds to our CodeFluent Entities model:

Project project = new Project();
project.Load(@"c:\path\to\model\myModel.cfp");

The next step is to navigate through the model, and this is where things get easier!

foreach (Entity e in project.Entities)
{
Console.WriteLine("Entity: " + e.Name);
foreach (Property p in e.Properties)
{
Console.WriteLine(" |- Property: " + p.Name);
// insert code below here
}
}

If this code does not try to edit the model, it shows you that the API is very straight forward. In a few lines, the API lets you navigate through every property of every entity of your ALL model! And yes, you can also read every attribute of each property, read the methods, rules…

Now, let us try to modify the model. We will look at the default value of each property, and modify some of them:

if (!string.IsNullOrEmpty(p.DefaultValue))
{
switch (p.DefaultValue)
{
case "MyDefaultTitle":
Console.Write("DEFAULT VALUE: " + p.DefaultValue + " was changed to: ");
p.DefaultValue = "Title";
Console.Write(p.DefaultValue + Environment.NewLine);
break;
case "Sample2":
Console.Write("DEFAULT VALUE: " + p.DefaultValue + " was changed to: ");
p.DefaultValue = "Sample1";
Console.Write(p.DefaultValue + Environment.NewLine);
break;
case "Sample1":
Console.Write("DEFAULT VALUE: " + p.DefaultValue + " was changed to: ");
p.DefaultValue = "Sample2";
Console.Write(p.DefaultValue + Environment.NewLine);
break;
}
}

We are looking for three default values (“MyDefaultTitle”, “Sample1”, “Sample2”) and we are modifying them. There is one last thing to do, which is to save the model.

project.Package.Save();

The save method needs to be inserted at the end of your method, outside the for loops.

In this example, you have learned how to use the API to modify your CodeFluent Entities model. To go further, you could add/edit/delete methods, rules, producers… The possibilities are almost limitless and yet, the API remains very easy to use. Have fun!

Vincent Patry