在DAL层有个基类,代码如下
 T Create();
        T Update(T entity);
        T Insert(T entity);
        void Delete(T entity);
        T Find(params object[] keyValues);
        List<T> FindAll();
        IQueryable<T> Query(Expression<Func<T, bool>> filter);
        IList<T> QueryByPage<TKey>(int PageSize, int PageIndex, Expression<Func<T, bool>> filter, Expression<Func<T, TKey>> orderby, bool iscount, out int recordsCount);现在我在实现BLL层的时候,按下面的方法写的,但是DAL有基类,BLL层怎么实现这个基类,不用每个类都写上一些类似的代码namespace BLL
{
    public class Category_tbl_BLL
    {
        IRepository<Category_tbl> repository;
        public Category_tbl_BLL()
        {
            this.repository = new Category_tbl_DAL();
        }
        public Category_tbl Create()
        {
            return repository.Create();
        }
        public Category_tbl Insert(Category_tbl entity)
        {
            return repository.Insert(entity);
        }
        public Category_tbl Update(Category_tbl entity)
        {
            return repository.Update(entity);
        }
        public void Delete(Category_tbl entity)
        {
            repository.Delete(entity);
        }
    }
}

解决方案 »

  1.   


    public interface IRepository<T>
    {
        T Create();
        T Update(T entity);
        T Insert(T entity);
        void Delete(T entity);
        T Find(params object[] keyValues);
        List<T> FindAll();
        IQueryable<T> Query(Expression<Func<T, bool>> filter);
        IList<T> QueryByPage<TKey>(
            int PageSize, 
            int PageIndex, 
            Expression<Func<T, bool>> filter, 
            Expression<Func<T, TKey>> orderby, 
            bool iscount, 
            out int recordsCount);
    }public class CategoryRepository : IRepository<Category_tbl>
    {
        // TODO: implement the methods defined in IRepositoryBase
    }namespace BLL
    {
        public class Category_tbl_BLL
        {
            IRepository<Category_tbl> repository;
            public Category_tbl_BLL()
            {
                this.repository = new CategoryRepository();
            }
            public Category_tbl Create()
            {
                return repository.Create();
            }
            public Category_tbl Insert(Category_tbl entity)
            {
                return repository.Insert(entity);
            }
            public Category_tbl Update(Category_tbl entity)
            {
                return repository.Update(entity);
            }
            public void Delete(Category_tbl entity)
            {
                repository.Delete(entity);
            }
        }
    }public class Category_tbl
    { }
      

  2.   

    补充:
    如果你想共享代码,可以创建一个RepositoryBase class,这个类应该实现所有共享的代码,然后按照下面的代码改写CategoryRepository,这样就不必重新写共享的代码。
    public interface IRepository<T>
    {
        T Create();
        T Update(T entity);
        T Insert(T entity);
        void Delete(T entity);
        T Find(params object[] keyValues);
        List<T> FindAll();
        IQueryable<T> Query(Expression<Func<T, bool>> filter);
        IList<T> QueryByPage<TKey>(
            int PageSize, 
            int PageIndex, 
            Expression<Func<T, bool>> filter, 
            Expression<Func<T, TKey>> orderby, 
            bool iscount, 
            out int recordsCount);
    }public class RepositoryBase 
    {
        // TODO: implement the shared methods defined in IRepositoryBase
    }public class CategoryRepository : RepositoryBase, IRepository<Category_tbl>
    {
        // TODO: the method which is not implemented by RepositoryBase
    }
      

  3.   

    不好意思,注释有些错误,下面是修改过的public interface IRepository<T>
    {
        T Create();
        T Update(T entity);
        T Insert(T entity);
        void Delete(T entity);
        T Find(params object[] keyValues);
        List<T> FindAll();
        IQueryable<T> Query(Expression<Func<T, bool>> filter);
        IList<T> QueryByPage<TKey>(
            int PageSize, 
            int PageIndex, 
            Expression<Func<T, bool>> filter, 
            Expression<Func<T, TKey>> orderby, 
            bool iscount, 
            out int recordsCount);
    }public class RepositoryBase 
    {
        // TODO: implement the shared methods defined in IRepository
    }public class CategoryRepository : RepositoryBase, IRepository<Category_tbl>
    {
        // TODO: the method which is not implemented by IRepository
    }
      

  4.   

    我在 DAL层 已经有 RepositoryBase 类了
      

  5.   

    那就妥了,在你的RepositoryBase 实现共享的代码,然后定义相应的Repository,这个Repository要继承你的RepositoryBase和实现IRepository中剩余的没有实现的方法。
      

  6.   


    不要设置什么“基类”,甚至连接口也不要硬性规定。DAL要有“柔性”,任何T的实现实例都可以使用DAL。假设你在实体中去设计什么“增删改查”,那么就完全背离了分层概念了。因为实体甚至也要带到表现层,那么实体具有“增删改查”那么你各层都有“增删改查”了。你的“基类”根本就是错误的。
      

  7.   

    这个地方可不是玩什么“继承”概念。这个地方体现的是“反射”的应用,既自动化地将对象的Field(或者Property)值与关系数据库进行读写操作,而不是搞“继承”。
      

  8.   

    我DAl层中的 都继承了RepositoryBase 但是我不想再BLL层中对每个类都写一遍 RepositoryBase 方法的调用
      

  9.   

    我是在DAL中实现的 增删改查,  基本我找到的例子都是这么写的,只是他们是BLL中对DAL的调用都是“手写的”
      

  10.   

    Friend, invoking a method from a Repository should be based on your requirement because it is BLL. I think that you may need to review some concept about layer architecture.I can't use Chinese because the computer used now doesn't support it, sorry about that.
      

  11.   

    To learn some materials would be helpful in your future career success. As for me, I prefer using DDD (Domain-Driven Design) methodology for system architecture. Recommending two books for you:1. Domain-Driven Design: Tackling Complexity in the Heart of Software By Eric Evans 
    2. Applying Domain-Driven Design and Patterns: With Examples in C# and .NET, 1/e By Jimmy Nilsson 
      

  12.   

    真是不好意思 这些英文我看得很吃力, 有没有一个demo我下载来看看?
      

  13.   

    你可以从下面的连接下载关于Domain-Driven Design的例,不过是另外一本书,书名是:.NET Domain-Driven Design with C#。通过这本书你会理解怎样用DDD来架构.NET应用程序。你只要下载第三章或第四章的代码即可(Chapter 3 Code/Chapter 4 Code)。实例后台的Data Access用的是ADO.NET,前台是WPF,道理都是一样的。通过例子,你会发现一个很棒的Synchronous例子。Synchronous的例子可以用在超市的终端,因为断电时终端仍然要正常运行(只能是现金)。想成为一个真正的程序员,是要下苦功夫的。希望对你会有帮助。http://www.wrox.com/WileyCDA/WroxTitle/-NET-Domain-Driven-Design-with-C-Problem-Design-Solution.productCd-0470147563,descCd-DOWNLOAD.html
      

  14.   

    另外再给你推荐一个非常棒的例子,也是关于Domain-Driven Design,还有POCO等概念。这个例子的后台是Entity Framework4。例子是:KandaAlpha,见下面的连接。http://kandaalpha.codeplex.com/