LINQ TO SQL的ORM模型构架,实现DATA层的方法!

#region 简单数据层代码,足可以说明面向接口的编程思想,一种思想的提高才是真正的提升
    /// <summary>
    /// 简单数据工厂
    /// </summary>
    internal class Factory
    {
        volatile static Dictionary<Thread, DataContext[]> divDataContext = new Dictionary<Thread, DataContext[]>();
        static System.Timers.Timer t = new System.Timers.Timer(1000);
        static Factory()
        {
            t.AutoReset = true;
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            t.Start();
        }
        static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            List<Thread> list = new List<Thread>();
            list.AddRange(divDataContext.Keys.Where(i => i.ThreadState == ThreadState.Stopped));
            for (int index = 0; index < list.Count; index++)
            {
                for (int refer = 0; refer < divDataContext[list[index]].Length; refer++)
                {
                    if (divDataContext[list[index]][refer] != null)
                    {
                        divDataContext[list[index]][refer].Dispose();
                        divDataContext[list[index]][refer] = null;
                    }
                }
                divDataContext.Remove(list[index]);
                list[index] = null;
            }
            list = null;
        }
        internal static DataContext CreateDb(string dbName)
        {
            return CreateDb(dbName, Thread.CurrentThread);
        }

        internal static DataContext CreateDb(string dbName, Thread thread)
        {
            if (!divDataContext.Keys.Contains(thread)) //如果不包含当前线程,就创建一个
            {
                divDataContext.Add(thread, new DataContext[1]);
            }

            if (dbName.Equals("lawyer"))
            {
                if (divDataContext[thread][0] == null)
                {
                    divDataContext[thread][0] = new DataClasses1DataContext();
                }
                return divDataContext[thread][0];
            }

            return null;
        }
    }
    /// <summary>
    /// 简单数据基类
    /// </summary>
    public abstract class DbBase
    {
        protected DataClasses1DataContext db = (DataClasses1DataContext)Factory.CreateDb("lawyer");
    }

    /// <summary>
    /// 简单数据实体接口
    /// </summary>
    public interface IDataEntity
    {

    }
    /// <summary>
    /// 简单数据操作通用方法
    /// </summary>
    public interface IRepository
    {
        void insert(IDataEntity entity);

    }
    #endregion

    #region 具体规范与实现

    /// <summary>
    /// 简单数据操作规范
    /// </summary>
    public interface IUsersRepository : IRepository
    {
        //自己的私有规范
        IQueryable<Users> GetDetails();
    }
    /// <summary>
    /// 简单数据操作对象
    /// </summary>
    public class UsersRepository : DbBase, IUsersRepository
    {

        #region IUsers 成员

        public IQueryable<Users> GetDetails()
        {
            return base.db.Users;
        }

        #endregion

        #region IRepository 成员

        public void insert(IDataEntity entity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public interface IRole_UsersRepository : IRepository
    {
        IQueryable<Role_Users> GetDetails();
    }
    public class Role_UsersRepository : DbBase, IRole_UsersRepository
    {
        #region IRole_UsersRepository 成员

        public IQueryable<Role_Users> GetDetails()
        {
            return base.db.Role_Users;
        }

        #endregion

        #region IRepository 成员

        public void insert(IDataEntity entity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    #endregion

posted @ 2011-05-18 14:33  张占岭  阅读(931)  评论(0编辑  收藏  举报