Using an Existing .NET Class as a Source Object
If you already have — or plan to declare — a public .NET class, you can configure the Rule Editor to use that class as its source object.
Any public .NET class can be used as a source object, provided it declares at least one public property or one public method that returns a value type (an in-rule method). Make sure to read the Code Effects Basics topic for details.
The CodeEffects.Rule.Editor.Control class declares several properties you can use to specify which type the editor should use as its source object. The most common is SourceType. The code sample below creates an instance of the Control with the Patient class as its source type:
using CodeEffects.Rule.Editor;
var editor = new Control("divRuleEditorId")
{
SourceType = typeof(Patient)
};
You can also use the full type name of the source object and the name of its declaring assembly:
using CodeEffects.Rule.Editor;
var editor = new Control("divRuleEditorId")
{
SourceTypeName = "CodeEffects.Rule.Demo.Models.Patient",
SourceAssembly = "CodeEffects.Rule.Demo"
};
The SourceAssembly property of the Control can be either the name of the referenced assembly that declares the source object or the fully qualified name of the assembly that you plan to load programmatically. It can be retrieved as Assembly.GetAssembly(SourceObjectType).FullName. The SourceTypeName property is the full name of the source object’s type. It can be retrieved as sourceInstance.GetType().FullName.
The Control class exposes additional source-related properties that can be used to further configure the source object. You can also customize how properties and methods are presented and evaluated by applying the custom attributes available in the CodeEffects.Rule.Common.Attributes namespace.
In the Rule Repository
The Rule Repository (RR) does not use SourceType or the other properties of the Control, because your application does not create the editors. Instead, you give RR the class when you sync your sources, and RR reflects it with the same rules that the Control uses, converts it to a Source XML document, and stores that document:
using CodeEffects.Rule.Repository;
var settings = new SourceSettings();
settings.SourceTypes.Add(typeof(Patient));
SyncResult result = Context.SyncSources(dbProvider, settings);
foreach(var source in result.Sources)
Console.WriteLine($"{source.AliasName}: {source.Outcome}");
Each source reports one of these outcomes: SourceCreated, VersionCreated, LabelsUpdated, Unchanged, or Failed. Running the same sync again with an unchanged class writes nothing, so it is safe to pass all of your sources every time. If a class lives in an assembly that your application does not reference, or uses type aliases, set SourceSettings.TypeResolver so RR can load the types.
RR identifies the source object by its alias. By default, it is the full name of the class. To keep the identity stable if you ever rename or move the class, set an alias that does not change:
using CodeEffects.Rule.Common.Attributes;
[Source(TypeOrAliasName = "Clinic.Patient", DisplayName = "Patient")]
public class Patient { /* … */ }
DisplayName is only a label: changing it updates the stored source in place and never creates a version. Adding, removing, or changing the type of a member does create a new source version, which is not live until someone makes it live, unless you set SourceSettings.NewVersionsGoLive. The Source Versions page of the Rule Repository guide shows how users see them.
Our demo projects provide detailed examples of how to use .NET classes as source objects in Code Effects.