Archive
Custom Sub Producer Example
There are several ways to add custom code to the generated code. You could simply do it by using Partial classes (which would be a manual action), Snippets or Aspects. But what if you wanted to add custom code to a specific layer such as the Business Object Layer right before production? Well you would need to create your own Sub Producer.
Creating your own custom Sub Producers in CodeFluent Entities is pretty straightforward: all you have to do is to:
1. create a Class Library project referencing the following assemblies:
- CodeFluent.Model.dll
- CodeFluent.Model.Common.dll
- CodeFluent.Producers.CodeDom.dll
2. create a class implementing the ICodeDomSubProducer interface.
3. build and Deploy the Class Library to the CodeFluent current directory (%ProgramFiles%\SoftFluent\CodeFluent\Modeler).
Everything is documented here with examples: Developing a Custom Sub-Producer.
Besides, since it easier to understand something we can actually see running, you’ll find a sample project to play with based on real world code on our forum: Ninject dependency injection.
Cheers,
Thibault NESTOR
CodeFluent Entities: Writing a custom aspect
In the previous post, we’ve seen that CodeFluent Entities infers a meta-model from our model and that we could interact with this meta-model to apply application wide changes. In this post we’ll see how ![]()
Step 1: Writing your aspect
As an example we’ll write an aspect which will add a “IsDeleted” property on all entities by default.
First of all, we added a new class library project named “Demo.Aspects” to our solution and added the following references: CodeFluent.Model.dll, CodeFluent.Model.Common.dll and CodeFluent.Runtime.dll. Aspects can either be written in XML or in .NET. In my opinion, it’s easier to start writing aspects in .NET as you’ll have Visual Studio’s IntelliSense which will help you out using the API.
In practice, an aspect is a class implementing the IProjectTemplate interface. This interface contains a single method “Run” which takes as a parameter a “context”.
Here’s a sample implementation:
using System.Collections;
using System.Xml;
using CodeFluent.Model;
namespace Demo.Aspects
{
public class MyAspect: IProjectTemplate
{
public static readonly XmlDocument Descriptor;
public const string MyAspectNamespace = "http://www.mycompany.com/aspects/myaspect/2013/1";
static MyAspect()
{
Descriptor = new XmlDocument();
Descriptor.LoadXml(
@"<cf:project xmlns:cf='http://www.softfluent.com/codefluent/2005/1' defaultNamespace='MyAspect'>
<cf:pattern name='My Aspect' namespaceUri='" + MyAspectNamespace + @"' preferredPrefix='mya' step='Tables'>
<cf:message class='_doc'>This is some description for our custom aspect.</cf:message>
<cf:descriptor name='enable'
typeName='boolean'
category='My Custom Aspect'
targets='Entity'
defaultValue='false'
displayName='Add the MyCustomProperty'
description='Description for our custom descriptor.' />
</cf:pattern>
</cf:project>");
}
public XmlDocument Run(IDictionary context)
{
if (context == null || !context.Contains("Project"))
{
// we are probably called for meta data inspection
return Descriptor;
}
// the dictionary contains at least these two entries
Element = (XmlElement)context["Element"];
Project = (Project)context["Project"];
foreach (Entity entity in Project.Entities)
{
Property property = new Property();
property.Name = "IsDeleted";
property.TypeName = "bool";
entity.Properties.Add(property);
}
// we have no specific Xml to send back, but aspect description
return Descriptor;
}
}
The method returns a XmlDocument which is described above as a string. It could also be written on a separate file.
As you can see this XML defines the aspect’s name, its XML namespace, and how it extends the CodeFluent schema (here it adds a “mya:enable” attribute on entities, whose default value is false).
Step 2: Using your aspect
Compile the “Demo.Aspects” project.
In the solution explorer, select the “Aspects” folder and click “Add Existing Aspect…”. Browse for our Demo.Aspects.dll that we created previously and here’s what you should see:
Great! As you can see, information returned by our descriptor is properly parsed and displayed ![]()
Hit OK, and now by default all your entities should have an extra “IsDeleted” property added when the model is inferred, which means that a “IsDeleted” column will be created for all tables, all stored procedures will be updated to include this column, and it’s the same in the middle tier and the UI tier. You can view this by clicking on the “View Inferred Model” button available in the ribbon:
Useful Links
- Documentation: Aspects Overview
- Documentation: The CodeFluent Entities API
- Documentation: Writing an aspect (.NET)
Hope this helps,
Carl Anderson
New CodeFluent Entities Release Available
A new version of CodeFluent Entities is publicly available (1.0.60110.644) which contains a set of new features which we’ll overview in this post.
Feature #1: The Resource Grid
This new release provides a new culture-oriented grid form which allows batch resources edition (project-wide or node-specific).
Feature #2: The Instance Grid
Same as the the resource grid but for instances:
We can now edit multiple instances at once ![]()
Feature #3: “Go To Xml”
Doing a right-click on a concept on the surface (entities, properties, views, methods, forms, etc.) you now have a “Go To Xml” option in the contextual menu which will take you to the XML node corresponding to the selected shape.
Feature #4: Support for non-latin characters
I’m sure all non-latin users will be glad to know that using the latest build, you can design your application using non latin-characters:
Feature #5: Minimize the Ribbon
Probably the sexiest feature in this release: you can now hide/show the ribbon using the upper-right corner arrow:
Interested in getting more details on what is shipped with each release? Subscribe to the CodeFluent Entities RSS Feed and you’ll automatically get all bug fixes and new features comprised in each build as soon as they are online!
Carl Anderson
CodeFluent Entities: Member Format Expressions (Final Part)
Last post exploring this feature of the Modeler, detailing what you can do in conditions and the MessagesBrowser class.
Conditions
In the previous post, we’ve seen that we could specify a condition attribute in the <if> and <font> tags. A few notes on the condition attribute:
- Property paths in condition attributes should not be surrounded by curly brackets (i.e. use IsKey instead of {IsKey}),
- Shortcuts available on the right side of the statement to ease condition declaration,
- Therefore prefer placing evaluated values on the left side of the statement rather than on the right.
The following operators are supported:
- “==” or “=” for a case insensitive equals,
- “[=CS]” for a case sensitive equals,
- “!=”, “<>” for a case insensitive not equals,
- “[!=CS]” for a case sensitive not equals,
- “>=” for greater than equals,
- “<=” for lower than equals,
- “>” for greater than,
- “<” for lower than,
- “[StartsWith]” for a case insensitve starts with,
- “[StartsWithCS]” for a case sensitive starts with,
- “[EndsWith]” for a case insensitive ends with,
- “[EndsWithCS]” for a case sensitive ends with,
- “[Contains]” for a case insensitive contains,
- “[ContainsCS]” for a case sensitive contains.
Here’s how to highlight all properties ending with “Id”:
<font condition=”Name [EndsWith] ‘Id’” color=’red’>{#Name}<else/>{#Name}</font>
Note: don’t forget to surround the right side statement with quotes so the evaluator knows it’s a string.
Please note that there are also shortcuts such as you don’t have to specify ‘> 0’ for numbers for instance:
{#Name}<font condition=’AllRules.Count’ color=’#007F0E’> (Rules={AllRules.Count})</font>
Will suffix properties with “(Rules=X)” in green:
The MessagesBrowser Class
The MessagesBrowser class is a class you can use as a property path and which allows you to access messages (a.k.a. resources) defined for the current concept (entity or property). For instance, if you defined English resources for each of your address properties in the documentation class:
Using the following expression you can display them:
{#Name} ({MessagesBrowser._doc.en-US})
Here’s the result:
Note: As of today, for performance results the MessagesBrowser class is cached and built on first access, so that defining a new resource or editing one will not be reflected on the surface.
Carl Anderson
CodeFluent Entities: Member Format Expressions (Part 2)
In the previous post, we’ve seen that we could define expressions such as “{Name}” to display the property name, “{#Name}” to display the decamelized property name, and mix values and literals in expressions to do stuff such as “{#Name} (IsKey={IsKey})”.
In this post we’re going to go a little further by introducing “tags”. Three tags are supported in expressions:
- Font: use it to define styles in your expression,
- If: use it to define conditions,
- Else: same as if.
For instance specify the following expression:
<font color=”blue”>{#Name}</font>
Will display all properties in blue:
We could also combine the tags to display key properties in red and the other ones in blue:
<if condition=IsKey><font color=”red”>{#Name}</font><else/><font color=”blue”>{#Name}</font></if>
Note: the value in the condition attribute is automatically evaluated, no need to wrap the property path between curly brackets.
And here’s the result:
So expressions don’t get too long the <font> tag also has a condition attribute so you don’t necessarily have to wrap it in a <if> tag. Therefore, you could get the same result using this expression:
<font condition=IsKey color=”red”>{#Name}<else/><font color=”blue”>{#Name}</font></font>
The <font> tag supports the following attributes:
- family: takes a font family name (e.g. Lucida Console),
- size: a font size in pt,
- stretch: a font stretch (System.Windows.FontStretch),
- style: a font style (System.Windows.FontStyle),
- weight: a font weight (System.Windows.FontWeight),
- color: a color (can take a value from System.Windows.Media.Brushes, or a #RGB value, or a #ARGB),
- decorations: a comma separated list of System.Windows.TextDecorations,
- trimming: a text trimming (System.Windows.TextTrimming),
- alignment: a text alignment (System.Windows.TextAlignment),
- direction: a flow direction (System.Windows.FlowDirection),
- lineheight: a line height,
- maxwidth: a maximum text width,
- maxheight: a maximum text height,
- maxlinecount: a maximum line count,
- condition: a condition to be evaluated.
Here’s another expression displaying the property type name in a light purple (Alpha=DD, Red=6E, Green=14, Blue=FF):
{#Name} : <font color=”#DD6E14FF”>{#DisplayTypeName}</font>
And here’s the result:
Once again use member format expressions with caution as they’ll be evaluated for all property names, so the lighter they are, the better performances will be and vice versa.
Carl Anderson
CodeFluent Entities: Member Format Expressions (Part 1)
Using CodeFluent Entities, you can use “Member Format Expressions” to customize the way names of entities and properties are formatted:
Several expressions are provided by default but you can also define your own expressions by selecting “Choose” (you do this from the “Modeler Tab” of the ribbon or from the contextual menu):
Clicking “Add New” will allow you to define your own:
Creating this one will display “Test!” for all my properties:
It’s not that useful in practice, but still, it illustrates that you can set whatever you want in it and most importantly that it supports literals ![]()
As you can see, the default expressions use property paths surrounded by curly brackets such as {Name} and {DisplayTypeName} to displays values from the model. You can access a list of available property paths by clicking on the “Insert Property Path” button which will display the following dialog:
For instance, here’s an expression displaying the property name and indicating if the property is a key between parenthesis:
Here’s the result on my Address entity:
If you prefix your property path with a ‘#’, the value will be decamelized. For instance the expression “{#Name} (IsKey={IsKey})” will display (the difference is visible on the Line1 and Line2 properties):
Sweet isn’t it?! ![]()
It for sure is a nice feature too change as you can change how the model is presented with a single expression, however use format expressions with caution though, because as they’re evaluated to display each of your properties in your model, big expressions doing a lot of stuff and displaying a lot of text can become a performance killer on big models.
Carl Anderson
Adding your own models to the CodeFluent Entities Starter Wizard
As mentioned in the posts “The Starter Wizard” and “New CodeFluent Entities Milestone”, the Modeler provides a wizard creating the structure of your solution as well as configuring your producers so they point to the freshly created projects.
Among the wizard pages, there’s a page allowing you to select a model template from which you want to start:
Models displayed in this page are the default official ones (blank, and sample ones), but just as you can add your custom producers or custom rules to the Modeler, you can add your custom models to his screen as well.
First you need to locate the custom configuration. You’ll find this information by:
- opening Visual Studio,
- go to “Tools > Options… > CodeFluent Entities”,
- and select the advanced view using the advanced button at the top of the property grid.
As you’ll see, there’s a property named “Custom Configuration File Path” which points to a Custom.config file:

Open this file (or create it if necessary) and add the following XML content to it:
<codeFluent.Modeler>
<starterWizardModelDescriptors>
<starterWizardModelDescriptor name="Test" fileName="Test.cfxproj" description=”A custom model.” />
</starterWizardModelDescriptors>
</codeFluent.Modeler>
This indicates the Starter Wizard to display a model named “Test”, corresponding to the “Test.cfxproj” project, in the “Select your model” wizard page.
Note: the cfxproj file should be placed in CodeFluent Entities’ Visual Studio templates folder which is “C:\Program Files (x86)\SoftFluent\CodeFluent\VSTemplates\Projects”).
Save the file, open your wizard, here’s what you should see now:

And voilà: our custom model project is now proposed in the wizard!
Carl
CodeFluent Entities Importer: Deducing Relationships
Last week we hosted a live webcast – which by the way was recorded and is available here – and following it we had several questions regarding the demo where we created a model from the Northwind database, and especially on how the tool deduced the relationships between entities.
The CodeFluent Entities importer detects foreign keys and transforms them into model relations, so we’re sorry to disappoint so many of you but there is absolutely no magic here!
The importer in fact relies on a database abstraction layer that presents a common interface for all supported databases or modeling systems that can be imported.
Here is a screenshot demonstrating how this intermediary abstraction layer « sees » an « old » Northwind Access Database, prior being imported, in a tool we call « Model and Database Explorer ».
Here you see two foreign keys that will be transformed into relations between entities if this database is imported as a model.
Now, back to magic! Even if there are no foreign keys on tables, the importer is truly capable of casting some voodoo spells ![]()
There is a parameter to the importer which is highlighted here:
When this parameter is set to true (the default value), the importer will try to determine automatically relations using column names. For example, say you have two tables like these:
Customer
+ CustomerId
+ Name
Order
+ OrderId
+ Reference
+ CustomerId
If no foreign key is declared, the importer will « guess » if this is a 1:M or 0:M relation.
Storing Custom Files In Your CodeFluent Entities Project
By default a CodeFluent Entities project contains the following elements:
- a Default surface, and its part “Default.Surface.cfp” containing layout information (shapes, positions, etc.)
- the project part named by your default namespace (e.g. “MyApplication.cfp” in the example above) and which contains the definition of all your entities, properties, rules, forms, views, etc.
Projects can contain multiple surfaces and multiple parts, which can be added by clicking on the Surfaces and Parts folder of your project.
Folders in your CodeFluent Entities project are virtual. If you open the corresponding project folder in Windows Explorer here’s what you’ll see:
- The project file (<ProjectName>.cfxproj)
- The parts (<DefaultNamespace>.cfp and the other ones you created)
- The surface parts (Default.Surface.cfp and possibly the other ones you created)
CodeFluent Entities projects also contain a “Files” folder which can contain any extra-files. Drop files in your project folder, and then using Solution Explorer’s “Show All Files” button and “Include in project” option.
Extra-Feature:
- If you place a CFP file in your project folder, including it to your project will add it to the Parts folder,
- If you place a XML file in your project folder which contains a valid root project node, including it to your project will automatically add it to the Parts folder,
- Other than that, any extra-file goes in the Files folder.
Working Together on Models using the Modeler
As mentioned in this previous post, working as a team on model is possible since you can:
- chunk models into several files (called parts),
- when working with the Modeler, you can also have several surfaces,
- organize your entities in namespaces,
- add your CodeFluent Entities project to your source control
Here’s a video featuring a quick preview of those points: