Using Simple ORM Objects
  • Updated on 09 Dec 2013
  • 1 minute to read
  • Print
  • Dark

Using Simple ORM Objects

  • Print
  • Dark

Typesafe vs. non typesafe ORM accessors

There are two classes that represent access to the ORM storage

  • ORM
  • DynamicORM

The difference between these classes is the generic one adds a number of typesafe methods.  Below is an example of both of these.


 DynamicORM orm = new DynamicORM();
IORMEntity e = orm.Fetch(typeof (Account), "11223");
Account a = (Account) e;


``` ORM orm = new ORM(); Account a = orm.Fetch("11223"); ```

For the sake of this article, we will be using ORM for examples.

Fetching an object by ID

 ORM<account> orm = new ORM<account>();
Account a = orm.Fetch("11223");</account></account>

Fetching an object with where conditions

 ORM<account> orm = new ORM<account>();
Account[] a = orm.Fetch(new WhereCondition[] {new FieldWhereCondition("name", QueryMatchType.StartsWith, "J"), });</account></account>

Storing an object

 ORM<account> orm = new ORM<account>();
orm.Store(new Account() {FirstName = "Joe"});<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;"> </span></account></account>

Storing a bunch of objects

A list of can be stored using the 'store' method on ORM.

 ORM<account> orm = new ORM<account>();
orm.Store(new Account[] {new Account() {FirstName = "Joe"}});</account></account>

A much more efficient (using batch insert mechanism in DB) is also available.

 ORM<account> orm = new ORM<account>();
orm.BatchInsert(new Account[] {new Account() {FirstName = "Joe"}});</account></account>

Saving only certain fields on an object

 ORM<account> orm = new ORM<account>();
orm.Store(new Account() {FirstName = "Joe"}, false, false, "first_name", "last_name");</account></account>

The extra parameters are:

  • Store relationships
  • Process before and after save methods
  • Fields to save

Seeing if entity exists ```
ORM orm = new ORM();
if (orm.Exists("12234"))
// do something

****Getting count of entities****	  ``` 
 ORM<account> orm = new ORM<account>();
long count = orm.GetCount();</account></account>
Was this article helpful?