• Repository 模式


    namespace ContactManager.Controllers
    {
        /// <summary>
        /// CRUD
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public interface IRepository<T> where T : class {
            IEnumerable<T> FindAll(Func<T, bool> exp);
            void Add(T entity);
            void Delete(T entity);
            void Save();
        }
    
        public class Repository<T> : IRepository<T> where T : class {
            protected System.Data.Linq.DataContext m_context;
            public Repository(DataContext context) { m_context = context; }
    
            #region IRepository<T> Members
    
            public IEnumerable<T> FindAll(Func<T, bool> exp)
            {
                return m_context.GetTable<T>().Where(exp);
            }
    
            public void Add(T entity)
            {
                m_context.GetTable<T>().InsertOnSubmit(entity);
            }
    
            public void Delete(T entity)
            {
                m_context.GetTable<T>().DeleteOnSubmit(entity);
            }
    
            public void Save()
            {
                m_context.SubmitChanges();
            }
    
            #endregion
        }
    
        public class ContactRepository : Repository<Contact> {
            public ContactRepository():this(new ContactManagerDataClassesDataContext()) { }
            public ContactRepository(DataContext context) : base(context) { }
        }
    }
  • 相关阅读:
    vitual box 虚拟机调整磁盘大小 resize partiton of vitual os
    单向链表逆转
    搭建公司的React开发环境
    2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)
    背包问题初探
    HDU 2588 GCD (欧拉函数)
    ZOJ
    ZOJ
    ZOJ
    HDU
  • 原文地址:https://www.cnblogs.com/chinaniit/p/1518451.html
Copyright © 2020-2023  润新知