- 07 Nov 2020
- 3 Minutes to read
- Print
- DarkLight
ORM Basics
- Updated on 07 Nov 2020
- 3 Minutes to read
- Print
- DarkLight
ORM Base Classes
There are three ORM base classes that you can inherit from to create an ORM object. They are as follows:
Base Class | Purpose |
---|---|
BaseORMEntity | |
BaseORMEntityWithLogicalDelete | Adds logical delete functionality to our ORM class |
AbstractEntity | Inherits from BaseORMEntityWithLogicalDelete and adds default fields (like creation date, deletion date, etc.), not folder related, is default searchable. |
AbstractFolderEntity | Same as AbstractEntity, but lives in a portal folder. |
Creating an ORM Object
To begin writing your own ORM type. To do so, write a class and do the following:
- Decorate the class with
[ORMEntity("nameofyourtype")]
. This will mark the class as an ORM entity and let you name the DB table that is generated to hold the data you save of this class. - The class should inherit from
DecisionsFramework.ServiceLayer.AbstractEntity
, the class will utilize some of the common entity properties (like creation date, deletion date, etc.). - If you want to use your ORM class in Decisions steps, also decorate it with the following two attributes:
[DataContract]
and[Writable]
.
The first property of the class should be decorated with [ORMPrimaryKeyField]
(and if you are planning to use this type in steps, it is also a good idea to decorate with [PropertyHidden]
).
All other properties of the class should be decorated with [ORMField]
. The [ORMField]
attribute has many overrides that let you set the DB type, the field name, and other specifications. The most common will be to override the field name. For example, to set a field name of "fullname", the attribute would look like this: [ORMField("fullname")]
Below is an example showing a full ORM class:
[ORMEntity("sampleormentityperson")]
[DataContract]
[Writable]
public class SampleORMPersonEntity : AbstractEntity
{
[ORMPrimaryKeyField]
[PropertyHidden]
[DataMember]
public string ID { get; set; }
[ORMField("fullname")]
[DataMember]
[WritableValue]
public string FullName { get; set; }
[ORMField("age")]
[DataMember]
[WritableValue]
public int Age { get; set; }
[PropertyHidden]
[DataMember]
public override string EntityName
{
get
{
return FullName;
}
set
{
FullName = value;
}
}
[PropertyHidden]
[DataMember]
public override string EntityDescription
{
get
{
return FullName;
}
set
{
FullName = value;
}
}
public override BaseActionType[] GetActions(AbstractUserContext userContext, EntityActionType[] types)
{
return new BaseActionType[0];
}
}
Once an ORM is created, you can interact with those ORM entities using the DynamicORM object. To do so, create a new instance of ORM. Off that object, find methods like Fetch, Store, Exists, and Delete. Example code for these methods is shown below:
public static void DeleteSamplePersonById(string id)
{
new ORM<SampleORMPersonEntity>().Delete(id);
}
public static void StoreSamplePerson(SampleORMPersonEntity newSamplePerson)
{
new ORM<SampleORMPersonEntity>().Store(newSamplePerson);
}
public static bool SamplePersonExists(string id, bool includeDeleted)
{
return new ORM<SampleORMPersonEntity>().Exists(id, includeDeleted);
}
public static SampleORMPersonEntity[] GetPersonByName(string fullName, QueryMatchType matchType)
{
return new ORM<SampleORMPersonEntity>().Fetch(new WhereCondition[]
{
new FieldWhereCondition("fullname", matchType, fullName)
});
}
Object Relationships
Using the ORM, objects can have relationships of one-to-one, one-to-many, and many-to-many. Do this by specifying the desired relationship in the field.
Below is an example property on an ORM object that specifies a field of another ORM type with a one-to-many relationship type. The property PersonList is an array on the ORM object. When creating the new ORMOneToManyRelationship object, specify a field name of "_SampleOrmWithRelationship". This adds a SampleOrmWithRelationship field to the sampleormentityperson database table, which will contain the ID of the SampleOrmWithRelationship object to which it is related.
Here is an example of the full ORM object on which the one-to-many relationship of PersonList is specified:
public class SampleOrmWithRelationship : AbstractEntity
{
#region Fields
[ORMPrimaryKeyField]
string id;
[DataMember]
[WritableValue]
[PropertyHidden(false)]
public string ID
{
get { return id; }
set { id = value; }
}
[ORMField]
string SimpleStringMember;
ORMOneToManyRelationship<sampleormpersonentity> PersonList = new ORMOneToManyRelationship<sampleormpersonentity>("SampleOrmWithRelationship", false);
#endregion
#region Properties
[DataMember]
[WritableValue]
[FieldName("SimpleStringMember")]
public string SimpleStringMember
{
get { return this.SimpleStringMember; }
set { this.SimpleStringMember = value; }
}
[DataMember]
[WritableValue]
[FieldName("PersonList")]
public SampleOrmPersonEntity[] PersonList
{
get { return this.PersonList.Items; }
set { this.PersonList.Items = value; }
}
#endregion
}</sampleormpersonentity></sampleormpersonentity>
Adding Indexes
Adding Index to Field
Indexes can be declared on fields that have been marked to be stored by the ORM. An index name and a flag to say if the index is unique can be added to this. Below is an example of specifying an index on a field.
[ORMField("entity_folder_id", typeof(KeyFieldConverter))]
[ORMFieldIndex("entity_folder_id_INDEX", false)]
private string entityFolderID;
Adding Index to Table
If an index includes more than one field, it can be specified at the table level. Below is an example of specifying an index on a table.
[ORMEntity("cache_entity")]
[ORMTableIndex("cache_entity_index", new string[]{ "configName", "instanceName", "itemId"}, true)]
class CacheEntity : BaseORMEntity