Archive
Count and Delete methods now supported by Event Rules
Event rules are triggered upon a lifecycle event of a CodeFluent Entities concept allowing the developer to plug himself in the lifecycle (e.g. before saving an entity, or before the validation step) of an entity to extend its behavior. Event rules can be applied on the following three concepts:
- On entities,
- On properties,
- On methods.
- The third one (On Methods) contains three event rule types:
- OnAddParameters
- OnAfter
- OnBefore
- Until today the Count and Delete methods were not supported by Method event rules. This post is meant to let you know that as of today Delete and Count methods can be used with event rules.
More information on Event rules in the documentation.
Cheers,
Thibault NESTOR
Rules in CodeFluent Entities (Video)
Following the same idea as exposed in the Methods post, CodeFluent Entities allows developers to specify rules in their models. Different types of rules are available to us:
- Event rules: triggered upon a lifecycle event of a CodeFluent Entities concept (entity creation, save, delete, read record, property set, etc.),
- Validation rules to validate data inputs,
- Transaction rules: lets you wrap a method in a transaction,
- Implementation rules: specify that an entity implements a particular interface for instance,
- Authorization rules: define permissions in your object model,
- Custom rules: implement your own custom rules.
Here are two videos illustrating different types of rules.
The first one illustrates how to add a Validation rule on a property:
The second one shows how to add an Event rule and how they can be used to extend the generated code by adding your own custom logic:
Integrating your Custom Rule to the Modeler
We saw in a previous post that we could integrate your own custom producer to the Modeler by adding a producerDescriptor element to the Custom.config; well the same feature is available to rules as well.
As you know, you can define rules in your model such as:
- validation rules (validate stings, urls, emails, etc.),
- lifecycle event rules (OnBeforeSave, OnBeforeDelete, OnAfterValidate, etc.),
- transaction rules (wrap an operation in an ADO.NET transaction or a TransactionScope),
- implementation rules (indicate an entity implements a specific interface),
- authorization rules (indicate a method requires specific permissions to be used),
- plus your own custom rules.
To add them to the modeler so you can use them through the “Add Rule Dialog” rather than in XML, you need to locate the Custom Configuration File. Open Visual Studio and in Tools > Options… > CodeFluent Entities, and select the advanced view using the advanced button at the top of the property grid. As you can see, there’s a property named “Custom Configuration File Path” which points to a Custom.config file.
By default, this file doesn’t exist, get to the pointed directory and create this Custom.config file with the following content:
<codeFluent.Modeler>
<ruleDescriptors>
<ruleDescriptor name="MyCustomRule" displayName="My Custom Rule" targets="Entity, Project" typeName="CodeFluent.Modeler.Design.Rules.UnresolvedRuleDescriptor, CodeFluent.Modeler">
<propertyDescriptors category="Global">
<propertyDescriptor name="MyValue" defaultValue="" options="Mandatory" description="A Mandatory value" category="MyCategory" />
<propertyDescriptor name="MyBoolValue" typeName="System.Boolean" defaultValue="true" />
</propertyDescriptors>
</ruleDescriptor>
</ruleDescriptors>
</codeFluent.Modeler>
Launch Visual Studio, open a model and click “Add Rule” on the project or an entity:
Ta-da! Our custom rule was added to the modeler!
Data Validation in an ASP.NET Web Form
In this previous post we created a few web forms which allowed us to list, create, edit, delete contacts using a CodeFluent Entities generated SQL Server database and object model.
Today, we’re going to use CodeFluent Entities to add a validation rule on our contact’s first name and last name, to ensure no invalid characters can be typed-in (we’re going to use a String validation rule to do that).
By the way, CodeFluent Entities provides the following out-of-the-box validation rules:
- Compare with another value (<, >, =, <=, >=)
- String (null, empty, invalid characters),
- Email (valid email, valid domains),
- Url (valid url, absolute or relative, valid host name types, valid host names, valid ports, valid schemes)
- Luhn (valid Luhn code, allow whitespaces),
- Object (is object, allows null or empty),
- Regular Expression (specify a regular expression to validate an input),
- Custom (define your own!)
Since rules are implemented in the Business Object Model, those validation rules will be available in all upper layers, so all your clients whatever they are (ASP.NET, Winforms, WPF, Silverlight, etc.) will benefit from them.
More information on Rules in CodeFluent Entities available here: Documentation.
Check-out the video to see how to declare it in your model and use it in your ASP.NET:
Note: In the video, we’re not using the graphic interface but editing the XML directly.
See you soon for more!
The R&D Team
