實體的管理主要包括如下功能:
A) 實現實體的緩存;
B) 實現實體的新增,修改,刪除,查詢等功能;
C)刷新緩存與數據庫同步
D)? 將緩存語句與數據庫同步
上面是一些基本的功能,下面是一個基本示例:
~~~
//實體狀態,對于實體采用緩存方式時有用。
public enum EntityState
?? {
?????? NoChanging,
?????? Added,
?????? Modified,
?????? Deleted
?? }
//這個類是真正實體的容器,目的是為了附加一些信息,便于實體的管理。
?? public class EntityItem<T>
?? {
?????? public T StorageItem{get;set;}
?????? public T CurrentItem{get;set;}
?????? public T OrigialItem{get;set;}
?????? public EntityState State{get;set;}
?????? public EntityItem()
?????? {
?????? }
?? }
??//利用委托,將選擇等工作交給用戶自己決定。
?? public delegate bool Selector<T>(T M);
???//實體管理類,需要提供數據庫連接管理,實體加載,查詢,新增,刪除,修改,與數據庫同步等功能
?? //另外也需要提供事務支持功能,支持事務時需要提供能傳入連接和事務實例的構造功能。
???//或者本身實現事務,自我回滾,并拋出異常,這樣可以實現多個管理類之間的任意組合式事務處理。
?? public class EntityMgmt<T>
?? {
??????//實體集合
?????? private List<EntityItem<T>> _entities;
?????? private string _connString;
?????? public EntityMgmt():this("")
?????? {
??????????
?????? }
?????? public EntityMgmt(string Conn)
?????? {
?????????? _entities = new List<EntityItem<T>>();
?????????? _connString = Conn;
?????????? if (_connString == "")
?????????? {
?????????????? //LoadFromConfigurationFile
?????????? }
?????? }
?????? private void LoadEntities()
?????? {
?????????? //構造查詢SQL,加載實體,可參見與數據庫交互部分.
?????? }
????? ?//提供查詢功能
?????? public IEnumerable<T> GetEntities(Selector<T> Selector)
?????? {
?????????? List<T> theRets = new List<T>();
?????????? if (_entities.Count <= 0)
?????????? {
?????????????? LoadEntities();
?????????? }
?????????? for (int i = 0; i < _entities.Count; i++)
?????????? {
?????????????? if (_entities[i].State!= EntityState.Deleted && Selector(_entities[i].CurrentItem) == true)
?????????????? {
?????????????????? theRets.Add(_entities[i].CurrentItem);
?????????????? }
?????????? }
?????????? return theRets.AsEnumerable();
?????? }
???????//新增一個實體
?????? public void Add(T Item)
?????? {
?????????? EntityItem<T> theItem = new EntityItem<T>();
?????????? theItem.CurrentItem = Item;
?????????? theItem.OrigialItem = Item;
?????????? theItem.State = EntityState.Added;
?????????? _entities.Add(theItem);
?????? }
????? //修改
?????? public void Update(T Item, Selector<T> SelectOldItem)
?????? {
?????????? EntityItem<T> theEntityItem = null;
?????????? for (int i = 0; i < _entities.Count; i++)
?????????? {
?????????????? if (SelectOldItem(_entities[i].CurrentItem) == true)
?????????????? {
?????????????????? theEntityItem = _entities[i];
?????????????????? break;
?????????????? }
?????????? }
?????????? if (theEntityItem != null)
?????????? {
?????????????? theEntityItem.CurrentItem = Item;
?????????????? theEntityItem.State = EntityState.Modified;
?????????? }
?????? }
?????? //接受改變,并將變化同步到數據庫.
?????? public void AcceptChanges()
?????? {
?????????? foreach (var item in _entities)
?????????? {
?????????????? switch (item.State)
?????????????? {
?????????????????? case EntityState.Added:
?????????????????????? DbOperation.InsertModel<T>(item.CurrentItem);
?????????????????????? break;
?????????????????? case EntityState.Deleted:
?????????????????????? break;
?????????????????? case EntityState.Modified:
?????????????????????? //這里可以利用item.StorageItem做并發檢測
?????????????????????? break;
?????????????? }
?????????? }
?????? }
?? }
??public enum CacheType
?? {
?????? AppLevel,
?????? SessionLevel,
?????? None
?? }
?? //用工廠模式實現對實體管理類的創建,這樣的好處是可以在這里實現對實體管理類本身的管理:
?? //全局單例模式,Session級共享等.
?? //這個類也可以實現對某個實體管理工廠的單例模式
?? public static class EntityMgmtFactory
?? {
?????? private static readonly Dictionary<Type, CacheType> _CacheTypes = new Dictionary<Type, CacheType>();
?????? static EntityMgmtFactory()
?????? {
?????????? //可以根據文件配置加載緩存方式.
?????? }
?????? public EntityMgmt<T> CreateEntityMgmt<T>(string conn)
?????? {
?????????? //根據緩存類型來創建或獲取實體管理類的實例。
?????????? //這里不再贅述。緩存級別可以利用Runtime的Session.
?????????? return new EntityMgmt<T>(conn);
?????? }
?? }
~~~
================================
上面的示例只是一些基本的功能,在實際應用中需要考慮的會更為復雜,比如可以設置失效期,自動同步等。另外也需要提供直接執行SQL的功能等。
AEF的方式比較復雜,但基本原理差不多,它的ESQL確實比較強大,這是其它一般框架所不具備的,但所謂成也蕭何敗也蕭何,提供如關系數據庫般得功能,
結果把簡單問題復雜化了,反而在緩存,復雜查詢,自由SQL支持方面顯得不足(對自由SQL的支持實際上是違背AEF框架的目標的)。