lundi 30 janvier 2017

Proposal for Improved Active Record Pattern

I wanted to propose a design pattern I've come up with. I haven't seen it used before which is surprising since it fixes a common problem, so I was wondering if I just haven't found it, or whether my solution is undesirable or breaks any major design principles. If anyone could provide me with any links to where it's been described, or provide opinion or critique. If you have none then feel free to use =)

I often use the Active Record Pattern due to it's simplicity, fully aware of the undesirable coupling it creates on the database, and litters domain objects with ugly SQL code. So my proposition is the following:

class User{

   id:string;
   firstName:string;
   lastName:string;

   constructor(id:string, firstName:string, lastName:string){
      this.id=id;
      this.setFirstName(firstName);
      this.setLastName(lastName);
   }

   function getFirstName(){
      return this.firstName;
   }

   function getLastName(){
      return this.lastName;
   }

   function setFirstName(firstName:string){
      this.firstName=firstName;
   }

   function setLastName(lastName:string){
      this.lastName=lastName;
   }

}


class UserDB extends User{
   user:User;

   constructror(user:User){
      this.user=user;
   }

   function getFirstName(){
      return this.user.getFirstName();
   }

   function getLastName(){
      return this.user.getLastName();
   }

   function setFirstName(firstName:string){
      this.this.setFirstName(firstName);

      this.update();
   }

   function setLastName(lastName:string){
      this.setLastName(lastName);

      this.update();
   }

   function update(){
      // sql execution script
      // update user set first_name=getFirstName(), last_name=getLastName() where id=this.user
   }
}

An then in code which creates instances, whether it be a factory or repo of User would then do this:

function getUser(id:string):User{
   // select first_name, last_name from user where id=id;

   var user=new User(id, firstName, lastName);
   var userdb=new UserDB(user);

   return user
}

This is perhaps a bad example since typically a User doesn't change their first or last name but it was the simplest example I could come up with!

I am essentially using the GoF Decorator pattern, but have never seen it used this way for data access.

It still has the undesirable effect of performing a db access per property update, just as the active record pattern does, but doesn't it at least remove the coupling from the User class to the database and removes the ugly SQL code. The UserDB implementation could go in the same package as other data access classes. Does the solution unbreak the SRP principle from the Active Record Pattern. I know that Active Record Pattern is considered by some to be an anti-pattern, but it is easy to use on simple applications, so point is just the following, if you ARE going to use it, wouldn't this is a better implementation for it?

Aucun commentaire:

Enregistrer un commentaire