ORM Basics
  • Updated on 09 Dec 2013
  • 3 minutes to read
  • Print
  • Dark
    Light

ORM Basics

  • Print
  • Dark
    Light

ORM Base Classes

There are three ORM base classes that you can inherit from in order to create an ORM object. They are as follows:

  • 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 you will need to write your own ORM type. To do so, you will need to write a class and do the following:

  1. Decorate your class with[ORMEntity("name_of_your_type")] .This will mark your class as an ORM entity and let you name the DB table that is generated to hold the data you save of this class.

  2. Your class should inherit from DecisionsFramework.ServiceLayer.AbstractEntity so that your class will utilize some of the common entity properties (like creation date, deletion date, etc).

  3. 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 your 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 your class should be decorated with [ORMField] . The [ORMField] attribute has many overrides which 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 "full_name" your attribute would look like this: [ORMField("full_name")]

Below is an example showing a full ORM class:

 [ORMEntity("sample_orm_entity_person")]
[DataContract]
[Writable]
public class SampleORMPersonEntity : AbstractEntity
{
    [ORMPrimaryKeyField]
    [PropertyHidden]
    [DataMember]
    public string ID { get; set; }


    [ORMField("full_name")]
    [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 you have created an ORM object you can interact with those ORM entities using DynamicORM object. To do so, create a new instance of ORM. Off that object you will find any 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("full_name", matchType, fullName)
                               });
}</sampleormpersonentity></sampleormpersonentity></sampleormpersonentity></sampleormpersonentity>

Object Relationships

Using the ORM, you can also create objects that have relationships of one-to-one, one-to-many, and many-to-many. You do this by specifying the desired relationship on your 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 our ORM object. When creating the new ORMOneToManyRelationship object, we specify a field name of "_SampleOrmWithRelationship" . This adds a_SampleOrmWithRelationship field to the sample_orm_entity_person database table which will contain the ID of the SampleOrmWithRelationship object to which it is related.

 ORMOneToManyRelationship<sampleormpersonentity> _PersonList = new ORMOneToManyRelationship<sampleormpersonentity>("_SampleOrmWithRelationship", false)</sampleormpersonentity></sampleormpersonentity>

Here is an example of the full ORM object on which we specify the one-to-many relationship of _PersonList :

 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>
Was this article helpful?