解剖Entity Framework(4)–實體的管理

來源:互聯網
上載者:User

實體的管理主要包括如下功能:

  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架構的目標的)。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.