RafficMaleo / android-active-record

Automatically exported from code.google.com/p/android-active-record
0 stars 0 forks source link

Enhancement proposal #13

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I am new user of this library. So excuse me, if I am wrong and please correct 
me. I found that one of main library usecases: persistance(saving) is quite 
ugly in use. To persist User object I need to make 3 calls:

    User u = new User("szaman", "haslo");
    User newU = db.newEntity(User.class);
    EntitiesHelper.copyFieldsWithoutID(newU, u);
    newU.save();

Why not introduce some simplier solution. Some "one-line-save" solution. For 
example:

package com.dendryt.bicyk.android.model;

import org.kroz.activerecord.ActiveRecordBase;
import org.kroz.activerecord.ActiveRecordException;
import org.kroz.activerecord.EntitiesHelper;

public class ActiveRecordBaseExt extends ActiveRecordBase{
    public <T extends ActiveRecordBase> T copyEntity(T obj, ActiveRecordBase db) throws ActiveRecordException{
        T newEntity = (T) db.newEntity(obj.getClass());
        EntitiesHelper.copyFieldsWithoutID(newEntity, obj);
        return newEntity;
    }

    public void saveAsNewObject(ActiveRecordBase db) throws ActiveRecordException{
        copyEntity(this, db).save();
    }
}

Now etity class should extend ActiveRecordBaseExt and saving it is prepared by 
one call:

User u = new User("szaman", "haslo");
u.saveAsNewObject(dbb);

I am new user of this library. So excuse me, if I am wrong and please correct 
me. I found that one of main library usecases: persistance(saving) is quite 
ugly in use. To persist User object I need to make 3 calls:

    User u = new User("szaman", "haslo");

    User newU = db.newEntity(User.class);
    EntitiesHelper.copyFieldsWithoutID(newU, u);
    newU.save();

Why not introduce some simplier solution. Some "one-line-save" solution. For 
example:

package com.dendryt.bicyk.android.model;

import org.kroz.activerecord.ActiveRecordBase;
import org.kroz.activerecord.ActiveRecordException;
import org.kroz.activerecord.EntitiesHelper;

public class ActiveRecordBaseExt extends ActiveRecordBase{
    public <T extends ActiveRecordBase> T copyEntity(T obj, ActiveRecordBase db) throws ActiveRecordException{
        T newEntity = (T) db.newEntity(obj.getClass());
        EntitiesHelper.copyFieldsWithoutID(newEntity, obj);
        return newEntity;
    }

    public void saveAsNewObject(ActiveRecordBase db) throws ActiveRecordException{
        copyEntity(this, db).save();
    }
}

Now etity class should extend ActiveRecordBaseExt and saving it is prepared by 
one call:

    User u = new User("szaman", "haslo");
    u.saveAsNewObject(dbb);

I am looking forward for some feedback from you:) 

Original issue reported on code.google.com by michal.r...@gmail.com on 11 Feb 2011 at 3:45

GoogleCodeExporter commented 8 years ago
Actually, there is a bit of a hack you can do that makes it so you don't need 
to copy the fields over to the second object.

Simply setting the database on your user object:

User u = new User("john", "smith");
u.setDatabase(dbb);
u.save();

It's still 3 lines, but it saves on unnecessary object creation.

I personally don't like the copying an object to another, especially if the 
object you are copying over to is an ActiveRecordBase subclass.

I have a fork on github under the same name, with some newly added features.  
Criticism would be greatly appreciated :)

Original comment by c.saunde...@gmail.com on 18 Feb 2011 at 2:49