5

I'working on a db application with ORmlite, my model is like this:

MDL object..

DatabaseTable(tableName = "UserCars")
public class CarMDL
{
    @DatabaseField(generatedId = true)
    private int _id;

    @DatabaseField(columnName = "name")
    private String _name;

//................. etc
}

// DB Helper class...

public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
    private Dao<CarMDL,Integer> _carDao = null;

 @Override
    public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource)
    {
        try 
        {
            TableUtils.createTable(connectionSource, CarMDL.class);

        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e)
        {
            e.printStackTrace();
        }

    }

  public Dao<CarMDL, Integer> getCarDao() 
    {
        if (null == _carDao) 
        {
            try 
            {
                _carDao = getDao(CarMDL.class);

            }catch (java.sql.SQLException e) 
            {
                e.printStackTrace();
            }
        }
        return _carDao;
    }

}

// DatabaseManager class...

public class DatabaseManager
{
    static private DatabaseManager  instance;

    private DatabaseHelper  helper;


    static public void init(Context ctx)
    {
        if (null == instance)
        {
            instance = new DatabaseManager(ctx);
        }
    }

    static public DatabaseManager getInstance()
    {
        return instance;
    }

    private DatabaseManager(Context ctx)
    {
        helper = new DatabaseHelper(ctx);
    }

    private DatabaseHelper getHelper()
    {
        return helper;
    }

// All the Dao functions of all MDL objects are in this class, for example:

public List<CarMDL> getAllCars()
    {
        List<CarMDL> carLists = null;
        try
        {
            carLists = getHelper().getCarDao().queryForAll();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return carLists;
    }

// This is another MDL object..

public List<MarkMDL> getAllMarks()
    {
        List<MarkMDL> marks = null;
        try
        {
            marks = getHelper().getMarkDao().queryForAll();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return marks;       
    }

}

So my question is, is it good have a DatabaseManager with all the functions from all the model objects, like:

listCarById(int id)
listPlaneById(int id)
removeCar(int id)
removePlane(int id)

Etc.....

4

3 回答 3

3

根据格雷的评论更新。

小心你的“单例”实现。您的init方法应该是synchronized确保您不会DatabaseManager由于并发问题而导致类的多个实例。我只是将initandgetInstance方法与以下内容结合起来(注意添加的同步关键字):

public static synchronized DatabaseManager getInstance(Context c)
{
    if(instance == null)
        instance = new DatabaseManager(c);

    return instance;
}

如需进一步阅读,请查看Kevin Galligan(ORMlite的贡献者之一)关于Single SQLite ConnectionAndroid Sqlite 锁定的这些博客文章。

更新:

要回答您有关如何组织加载方法之类的问题getAllCars,我首先建议制作它们static,因为它们不依赖于除您的方法之外的任何其他东西来获取您的单例DatabaseManager,当然,这也是static. 如果您有少量这些类型的方法,您可以将它们全部设为DatabaseManger. 如果你有很多,你可以为一个类型对应的所有静态方法创建一个帮助类。

如果你有一个方法确实依赖于给定的CarMDLor实例的内部结构MarkMDL(比如你需要一个方法来获取一些关联的引用),请考虑使这些方法成为CarMDLorMarkMDL类的成员。

于 2012-04-09T20:57:32.767 回答
0

我将所有一次性应用程序的工作都放在 Application onCreate 中,并保留应用程序实例本身的引用,因此我可以执行许多任务而不必弄乱同步方法或类似方法。所以假设我们有一个应用程序(记得在清单中添加它):

public class App extends Application
{
    private static App gInstance = null;
    // your static globals here

    @Override
    public void onCreate()
    {
        // according to documentation onCreate is called before any other method
        super.onCreate();
        // assign here all your static stuff
        gInstance = this;
    }

    // doesn't need to be synchronized because of the early onCreate
    public static App getInstance()
    {
        return gInstance;
    }
}

那么你的数据库助手类 Manifest.class 是一个包含所有数据类型类的数组:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
    // private constructor, singleton pattern, we use
    // App context so the class is created on static init
    private static DatabaseHelper gHelper = new DatabaseHelper(App.getInstance());

    private DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);

        // cache your dao here
        for (Class<?> cls: Manifest.classes)
        {
            try
            {
                DaoManager.createDao(getConnectionSource(), cls);
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    // if you need the instance, you don't need synchronized because of static init
    public static DatabaseHelper getHelper()
    {
        return gHelper;
    }

    // lookup from cache
    public static <D extends Dao<T, ?>, T> D getTypeDao(Class<T> cls)
    {
        return DaoManager.lookupDao(gHelper.getConnectionSource(), cls);
    }

    // we leak this class here since android doesn't provide Application onDestroy
    // it's not really a big deal if we need the orm mapping for all application lifetime
    // Q: should I keep the instance closeable? the android finalyzer calls somehow close here? I was unable to reproduce, to be sure you can call the super.close() and print a warning
    @Override
    public void close()
    {
        throw new RuntimeException("DatabaseHelper Singleton is ethernal");
    }
}
于 2013-04-17T01:31:11.040 回答
0

将上下文添加到您的 DatabaseManager 方法

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DatabaseManager.init(this.getContext());
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

带有 ormlite 的示例 android 应用程序 https://github.com/elberthcabrales/cfeMedicion

于 2018-04-20T18:20:53.317 回答