Press "Enter" to skip to content

Month: October 2012

Changing my ORM project to use a fluent interface

One thing I didn’t like about the ORM I’m writing is that you needed to do some non-obvious setup before executing some calls.

Here’s how to setup a call to retrieve all customers whose names contained a string value, and were created before DateTime.Now (that DateTime.Now condition was just to show how to have a second condition).

ConditionList inclusionConditions = new ConditionList();
inclusionConditions.AddCondition(Customer.COLUMN_NAME, Condition.Comparators.Contains, customerSearcher.SearchCriteria);
inclusionConditions.AddCondition(Customer.COLUMN_DATE_CREATED, Condition.Comparators.LessThanOrEqualTo, DateTime.Now);
customerSearcher.MatchingCustomers = PersistenceManager.RetrieveMatching<Customer>(inclusionConditions);

You needed to create a ConditionList object, populate it with the conditions, and then pass it to the ORM.  Not exactly obvious.

So, I’ve started working on making it a more fluent interface.

Now you can use the following method to perform the same function.

customerSearcher.MatchingCustomers = PersistenceManager.PerformAction()
    .Where(Customer.COLUMN_NAME, Condition.Comparators.Contains, customerSearcher.SearchCriteria)
    .Where(Customer.COLUMN_DATE_CREATED, Condition.Comparators.LessThanOrEqualTo, DateTime.Now)
    .RetrieveMatching<Customer>();

I also have a Set() method, that works similar to the Where() method, for when you do inserts and updates into the database.

Leave a Comment