標籤:
前面幾篇,斷斷續續地囫圇吞棗地讀了ActiveAndroid的部分源碼,大致瞭解了ActiveAndroid的註解反射原理。其中很多細節還不算很清楚,加之內容非常多,為了更好地閱讀接下來的內容,在此對前面閱讀的部分作一個總結。
在之前的幾篇中,重點閱讀了ActiveAndroid中的三個類:Conguration , ModelInfo , TableInfo。下面將對這三個類的作用做一個簡單地總結:
一、Conguration
先看一下Conguration的成員變數:
public final static String SQL_PARSER_LEGACY = "legacy"; public final static String SQL_PARSER_DELIMITED = "delimited"; ////////////////////////////////////////////////////////////////////////////////////// // PRIVATE MEMBERS ////////////////////////////////////////////////////////////////////////////////////// private Context mContext; private String mDatabaseName; private int mDatabaseVersion; private String mSqlParser; private List<Class<? extends Model>> mModelClasses; private List<Class<? extends TypeSerializer>> mTypeSerializers; private int mCacheSize;
其中有,寫成常量的SQL解析器
public final static String SQL_PARSER_LEGACY = "legacy"; public final static String SQL_PARSER_DELIMITED = "delimited";
資料庫的上下文、
資料庫的名稱、
資料庫的版本、
資料庫的解析器、
資料庫中model子類的列表、
資料庫中序列化的列表、
和緩衝大小。
總體來看,Conguration就是存了整個資料庫的基本資料和資料庫中有需要儲存的類的列表。每次啟動地初始化過程都會初始化這個類,並從AndroidManifest和檔案中讀取它的基本資料。
二、ModelInfo
同樣先看一下ModelInfo的成員變數:
private Map<Class<? extends Model>, TableInfo> mTableInfos = new HashMap<Class<? extends Model>, TableInfo>(); private Map<Class<?>, TypeSerializer> mTypeSerializers = new HashMap<Class<?>, TypeSerializer>() { { put(Calendar.class, new CalendarSerializer()); put(java.sql.Date.class, new SqlDateSerializer()); put(java.util.Date.class, new UtilDateSerializer()); put(java.io.File.class, new FileSerializer()); } };
ModelInfo的成員變數只有兩個mTableInfos和mTypeSerializers。
mTableInfos儲存了每個需要儲存的類與TableInfo的映射關係。
mTypeSerializers儲存了每個序列化的介面。
每當使用者要對要儲存的類進行讀/寫操作時,就需要從mTableInfos這個map中找到屬於自己的TableInfo,然後根據TableInfo中資訊進行相關操作。
三、TableInfo
TableInfo的成員變數如下:
private Class<? extends Model> mType; private String mTableName; private String mIdName = Table.DEFAULT_ID_NAME; private Map<Field, String> mColumnNames = new LinkedHashMap<Field, String>();
分別是:
TableInfo對應的類、
這個類對應的表名、
這個表的Id欄位名、
這個類和表中,成員和欄位名的對應關係。
當我們調用要儲存的類的相關方法時,這個類就會找到它的TableInfo,然後根據mColumnNames的映射去操作資料庫中的對應欄位。
Done~
讀ActiveAndroid源碼(五)