是否可以为我的所有对象创建一个通用的存储库类?

发布于 2024-12-10 01:53:52 字数 2934 浏览 1 评论 0 原文

我有以下 POCO 类及其存储库模式实现。 如果我的模型足够大,那么使这个通用化是有意义的,因此只需要完成一个实现。

这可能吗?你能告诉我怎么做吗?

 public class Position
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
        public int PositionID { get; set; }
        [StringLength(20, MinimumLength=3)]
        public string name { get; set; }
        public int yearsExperienceRequired { get; set; }
        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }


public interface IPositionRepository
    {
        void CreateNewPosition(Position contactToCreate);
        void DeletePosition(int id);
        Position GetPositionByID(int id);
        IEnumerable<Position> GetAllPositions();
        int SaveChanges();
        IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate);

    }

public class PositionRepository : IPositionRepository
    {

        private HRContext _db = new HRContext();

        public PositionRepository(HRContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            _db = context;
        } 



        public Position GetPositionByID(int id)
        {
            return _db.Positions.FirstOrDefault(d => d.PositionID == id);
        }

        public IEnumerable<Position> GetAllPosition()
        {
            return _db.Positions.ToList();
        }

        public void CreateNewPosition(Position positionToCreate)
        {
            _db.Positions.Add(positionToCreate);
            _db.SaveChanges();
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }

        public void DeletePosition(int id)
        {
            var posToDel = GetPositionByID(id);
            _db.Positions.Remove(posToDel);
            _db.SaveChanges();
        }

        /// <summary>
        /// Lets suppose we have a field called name, another years of experience, and another department.
        /// How can I create a generic way in ONE simple method to allow the caller of this method to pass
        /// 1, 2 or 3 parameters.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate)
        {
            return _db.Positions.Where(predicate);

        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

I have the following POCO Class with its Repository Pattern Implementation.
If my model is big enough it would make sense to make this generic so only one implementation needs to be done.

Is this possible? Can you please show me how?

 public class Position
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
        public int PositionID { get; set; }
        [StringLength(20, MinimumLength=3)]
        public string name { get; set; }
        public int yearsExperienceRequired { get; set; }
        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }


public interface IPositionRepository
    {
        void CreateNewPosition(Position contactToCreate);
        void DeletePosition(int id);
        Position GetPositionByID(int id);
        IEnumerable<Position> GetAllPositions();
        int SaveChanges();
        IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate);

    }

public class PositionRepository : IPositionRepository
    {

        private HRContext _db = new HRContext();

        public PositionRepository(HRContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            _db = context;
        } 



        public Position GetPositionByID(int id)
        {
            return _db.Positions.FirstOrDefault(d => d.PositionID == id);
        }

        public IEnumerable<Position> GetAllPosition()
        {
            return _db.Positions.ToList();
        }

        public void CreateNewPosition(Position positionToCreate)
        {
            _db.Positions.Add(positionToCreate);
            _db.SaveChanges();
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }

        public void DeletePosition(int id)
        {
            var posToDel = GetPositionByID(id);
            _db.Positions.Remove(posToDel);
            _db.SaveChanges();
        }

        /// <summary>
        /// Lets suppose we have a field called name, another years of experience, and another department.
        /// How can I create a generic way in ONE simple method to allow the caller of this method to pass
        /// 1, 2 or 3 parameters.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate)
        {
            return _db.Positions.Where(predicate);

        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红墙和绿瓦 2024-12-17 01:53:52

是的,您可以这里是一个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;

namespace Nodes.Data.Repository
{
    public class BaseRepository<TEntity>:IRepository<TEntity> where TEntity : class
    {
        internal SampleDBContext context;
        internal DbSet<TEntity> dbSet;

        public BaseRepository(SampleDBContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);

        }

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void DeleteAll(List<TEntity> entities)
        {
            foreach (var entity in entities)
            {
                this.Delete(entity);
            }
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }

        public IQueryable<TEntity> Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
        {
            return dbSet.Where(predicate);
        }
    }
}

UnitOfWork:

public class UnitOfWork
    {
        private SampleDBContext context = new SampleDBContext();

        private IUserRepository userRepository;

        #region PublicProperties

        public IUserRepository UserRepository
        {
            get
            {
                if (this.userRepository == null)
                {
                    UserRepository repo = new UserRepository(context);
                }
                return userRepository;
            }
        }

        #endregion

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

yes, you can here is one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;

namespace Nodes.Data.Repository
{
    public class BaseRepository<TEntity>:IRepository<TEntity> where TEntity : class
    {
        internal SampleDBContext context;
        internal DbSet<TEntity> dbSet;

        public BaseRepository(SampleDBContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);

        }

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void DeleteAll(List<TEntity> entities)
        {
            foreach (var entity in entities)
            {
                this.Delete(entity);
            }
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }

        public IQueryable<TEntity> Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
        {
            return dbSet.Where(predicate);
        }
    }
}

UnitOfWork:

public class UnitOfWork
    {
        private SampleDBContext context = new SampleDBContext();

        private IUserRepository userRepository;

        #region PublicProperties

        public IUserRepository UserRepository
        {
            get
            {
                if (this.userRepository == null)
                {
                    UserRepository repo = new UserRepository(context);
                }
                return userRepository;
            }
        }

        #endregion

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
为人所爱 2024-12-17 01:53:52

我在我的项目中使用这个通用存储库实现已经有一段时间了:

http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/

最初是为EF4设计的,但在我看来,可以修改实现以使用 EF4.1 代码优先。

I've been using this generic repository implementation with my projects for some time:

http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/

It was originally designed for EF4, but it looks to me like the implementation could be modified to work with EF4.1 code-first.

还在原地等你 2024-12-17 01:53:52

您可以尝试:

  1. 要使用此生成器:http://efrepository.codeplex.com/
  2. 自己做类似的。
  3. 使用 Visual Studio 生成简单的方法,并将自定义查询添加到从存储库继承的类中。

You can try:

  1. To use this generator: http://efrepository.codeplex.com/.
  2. To make similar yourself.
  3. To generate trivial methods with Visual Studio and add your custom queries in a class that inherits from repository.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文