zoukankan      html  css  js  c++  java
  • 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) { }
        }
    }
  • 相关阅读:
    动态数组的实现案例
    随机验证码实现案例
    wince下的CPU和内存占用率计算
    RT-Thread--时间管理
    RT-Thread--线程管理
    RT-Thread--内核基础
    RT-Thread--简介
    Git基本操作
    STM32F103/429串口IAP+Ymodem升级
    KEIL_MDK生成Bin文件
  • 原文地址:https://www.cnblogs.com/chinaniit/p/1518451.html
Copyright © 2011-2022 走看看