Tag: repository pattern

DI和存储库模式

目前,我的代码与此类似(缩短只是为了说明一点): DAL 存储库接口 public interface IRepository { IList GetAll(); TEntity Get(TKey id); TEntity Add(TEntity item); TEntity Update(TEntity item); bool Remove(TKey id); } 基本EF存储库 public class BaseEFRepository : IRepository where TEntity: class, IEntity where TKey: struct { protected readonly DbContext _dbContext; public BaseRepository() { _dbContext = new MyDB(); _dbContext.Configuration.ProxyCreationEnabled = false; _dbContext.Configuration.LazyLoadingEnabled = false; } public […]

如何在单独的程序集中获取用户标识

我正在开发一个有两个项目的应用程序: 核心 – 使用存储库模式和域驱动设计来容纳数据访问层 UI – 使用ASP.Net MVC。 目前,我能够通过User属性在UI控制器内获取当前登录用户的信息(id,name等),如下所示: using Microsoft.AspNet.Identity; public class ExamController : Controller { IExaminationRepository _repository; public ExamController() { _repository = RepositoryFactory.Get(); } [HttpPost] [Authorize(Roles = “Examiner”)] public ActionResult Create(ExamViewModel viewModel) { try { ExaminationDomain domain = Mapper.Map(viewModel); //TODO: Move this to the repository domain.AuthorId = User.Identity.GetUserId(); _repository.Add(domain); return RedirectToAction(“Index”); } catch […]

asp.net MVC – 如何通过不同的存储库类共享同一个SqlConnection实例

我正在创建一个使用MVC5和普通ADO.NET的新项目(仅作为学习练习),我需要创建一个存储库来注册一个模型,该模型包含多个相关对象,这些对象也需要同时创建,并且这些对象在转弯可能需要插入其他物体。 我能想到的最简单的解决方案是有一个庞大的方法(在存储库中)接收父对象的实例(其中包含需要插入的所有相关对象),并且还有一个接收所有相关的存储过程数据作为表值参数,然后使用单个事务插入所有内容。 虽然这看起来似乎是最简单的方法,但我并不是它的忠实粉丝,所以我想知道的是,我是否可以使用任何方式/通用实践来共享为父对象创建的SqlConnection的相同实例与其他相关的对象? 我想也许在相关对象的构造函数中传递SqlConnection对象,这样每个存储库只需要处理插入单个对象的逻辑,但我不确定。 编辑 ——————————- 这是父对象(Model)的存储库,我认为它应该实例化SqlConnection并启动事务 public class ModelRepository : IModelRepository { public int Add(Model entity) { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); using (var command = conn.CreateCommand()) { command.Transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted); command.CommandText = “up_Model_Insert”; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(command.CreateParameter(“@pName “, entity.Name)); command.Parameters.Add(command.CreateParameter(“@pDescription”, entity.Description)); //Other parameters… //Call the repositories of the other objects […]

对DbContext的通用访问

ObjectContext允许对生成的实体进行generics访问。 DbContext似乎没有这样的支持。 使用通用存储库访问EF5具有挑战性。 假设我想要一个通用的机制来读取任何给定的实体,称之为TEntity: public class DataRepositoryEF5 where T: DbContext { private ObjectContext _context; public DataRepositoryEF5(DbContext context) { _context = ((IObjectContextAdapter)context).ObjectContext; } public IEnumerable ReadAll() where TEntity : class,new() { return GetObjectSet().AsEnumerable(); } protected ObjectSet GetObjectSet() where TEntity : class,new() { ObjectSet result; result = _context.CreateObjectSet(); return result; } } 用法 var context = new […]

如何使用UnitOfWork和DatabaseFactory&Generic Repository添加多个DbContext

我想在My ASP.NET MVC 5 App中添加两个DbContext,为ASPIdentity添加一个DbContext,为我的APP DB添加另一个DbContext。 我正在使用存储库模式。 我的问题是,如何在BaseRepository中指定每个DbContext的实体 ? 这就是我做的。 1- DatabaseFactory和IDatabaseFactory public class DatabaseFactory where T : DbContext,new() { private T dbContext; public T Init() { return dbContext ?? (dbContext = new T()); } } public interface IDatabaseFactory where T : DbContext { T Init(); } 2- IUnitOfWork和UnitOfWork public class UnitOfWork : IUnitOfWork where […]

存储库模式通用应用程序

几天前,当我开始学习使用Unity的Repository Pattern时,我觉得这种模式的主要好处是数据层与业务层的分离。 换句话说,如果需要改变方式,应用程序如何存储数据,这很容易,因为只有一个主模型负责通信。 这意味着,如果应用程序当前将数据保存到序列化的XML文件中,则将此逻辑更改为连接到数据库并不是非常困难。 我发现很少有很好的演示也使用了Unit Of Work层,这看起来非常方便。 让我向您展示一下我的代码。 public class UnitOfWork : IUnitOfWork { private readonly RepositoryContext _context; public IEmployeeRepository Employees { get; set; } public UnitOfWork(RepositoryContext context) { _context = context; Employees = new EmployeeRepository(_context); } public int Complete() { return _context.SaveChanges(); } public void Dispose() { _context.Dispose(); } } 主存储库上下文: public class RepositoryContext […]

如何为使用存储库的所有实例使用一个数据库上下文?

我做了一些建筑MVC错误,因为我不知道 IEntityChangeTracker的多个实例不能引用实体对象。 错误。 我的设置如下:我有一个访问数据库的repository ,它创建了一个dbcontext实例,我有控制器实例化他们需要的managers , managers都实例化他们自己的repository 。 这是问题,当控制器使用多个manager收集数据然后尝试创建使用此数据的对象时,将对象添加到dbcontext时会出现上述错误。 我读到了关于UnitOfWork模式的内容,但重新构建我的代码似乎需要做很多工作。 是否有快速修复能够更新数据库并避免错误? 谢谢。

使用存储库模式切换LazyLoading

默认情况下,在我的DbContext中禁用了LazyLoading。 我使用存储库模式,在某些情况下,我只需要获取简单的对象,而在其他情况下,我需要获取具有导航属性值的对象。 如何为LazyLoading实现类似开关的东西? 任何帮助将不胜感激 我有一个有效的解决方案,但我不确定它是否正确:在存储库的界面中我添加了新属性 public interface IRepository where T : BaseEntity { T GetById(object id); void Insert(T entity); ….. bool LazyLoadingSwitches { set; } } 然后实现它: public bool LazyLoadingSwitches { set { this.context.Configuration.LazyLoadingEnabled = value; } } 当我需要获取相关数据的模型时,我在控制器中使用: repository.LazyLoadingSwitches = true; name = order.Customer.FullName; repository.LazyLoadingSwitches = false; 请建议我最好的解决方案是什么?

如何使用通用存储库模式的连接 – entity framework

我有一个像下面的Generic Repository来处理我的CRUD ,对于一个易于使用的单个实体,当我尝试加入我的POCOs 。 假设我有这些POCO,它们使用流畅的api(多对多和一对多关系)进行映射: public class Student { public Student() { this.Courses = new HashSet(); } public int StudentId { get; set; } public string StudentName { get; set; } //FKs public virtual Standard Standard { get; set; } public int StdandardRefId { get; set; } public virtual ICollection Courses { get; set; } […]

在unit testing中处理多个模拟和断言

我目前有一个存储库,它使用Entity Framework进行我的CRUD操作。 这是注入我需要使用此repo的服务。 使用AutoMapper,我将实体Model投影到Poco模型上,并且服务返回poco。 如果我的对象有多个属性,那么设置然后断言我的属性的正确方法是什么? 如果我的服务有多个repo依赖项,那么设置我所有模拟的正确方法是什么? * – 一个类[setup],其中为这些测试装置配置了所有模拟和对象?***** 我想避免进行10次测试,每次测试在属性上有50个断言,并且每次测试都有几十个模拟设置。 这使得可维护性和可读性变得困难。 我已经阅读了unit testing艺术,并没有发现任何建议如何处理这种情况。 我使用的工具是Rhino Mocks和NUnit。 我也在SO上找到了这个,但它没有回答我的问题: 正确的unit testing服务/存储库交互 这是一个表达我所描述内容的示例: public void Save_ReturnSavedDocument() { //Simulate DB object var repoResult = new EntityModel.Document() { DocumentId = 2, Message = “TestMessage1”, Name = “Name1”, Email = “Email1”, Comment = “Comment1” }; //Create mocks of Repo Methods – Might […]