Software development, scripting and programming.
User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2014-01-25 06:07 »

I found this a while back, about "active record" from Martin Fowler, a really excellent advice and idea. Although, this doesn't fit all situations but it makes life a lot easier in many applications. I can't believe I didn't come up with this myself, because I always build my own custom wrappers for data rows, I feel like an idiot. Shame on me! :oops:

The picture explains this concept very clearly.

Basically, (putting it very simple), I usually have a...

Code: Select all

class Person {
    public int Id;
    public string Name;
}


...and then I have a Application.GetPersonEarnings(int PersonId) or Application.GetPersonEarnings(Person vPerson);

With his idea...

Code: Select all

class Person {
    public int Id;
    public string Name;
    private void GetEarnings();
}


...it gets a lot simpler and cleaner in my humble opinion because you will always have Person.GetEarnings() ready to use. :mrgreen:

http://martinfowler.com/eaaCatalog/activeRecord.html

An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.

activeRecordSketch.gif
activeRecordSketch.gif (7.52 KiB) Viewed 2182 times

An object carries both data and behavior. Much of this data is persistent and needs to be stored in a database. Active Record uses the most obvious approach, putting data access logic in the domain object. This way all people know how to read and write their data to and from the database.