Tag: repository

如何在存储库中正确处理Linq到SQL DataContext?

在Rob Conery风格的ASP.NET MVC应用程序中,您通常有一个存储库: public class CustomerRepository { DataContext dc = new DataContext(); public IQueryable AllCustomers() { return db.Customers; } public Customer GetCustomer(int customerID) { return db.Customers.FirstOrDefault(c => c.CustomerID = customerID); } } 和一个控制器: public class CustomerController: Controller { CustomerRepository _repository; public ActionResult Index() { var data = _repository.AllCustomers(); return view(“Index”, data); } public ActionResult Details(int […]

使用Moq对LINQ to SQL CRUD操作进行unit testing

我已经浏览了其他问题,但没有什么能与我正在寻找的东西相提并论……主要是因为我并不是100%肯定我正在寻找什么! 基本上我目前正在开发一个新项目,我已经为数据库实体创建了抽象层,并将DAC设置为存储库。 我想用Mock对象对它进行unit testing,但是我已经用CRUD(特别是C)操作打了一个知识墙,我的unit testing类: [TestClass] public class RepositoryTest { private Mock _repository; private IList _personStore; [TestInitialize()] public void Initialize() { _personStore= new List() { new Person() { Name = “Paul” }, new Person() { Name = “John” }, new Person() { Name = “Bob” }, new Person() { Name = “Bill” }, }; _repository […]

如何初始化VersionControlServer类?

我想从TFS 2013下载文件。我找到了VersionControlServer类: msdn 但是如何初始化呢? VersionControlServer vs; vs.DownloadFile(repositoryPath,localPath); // vs not initialized! 谢谢!

为什么通用存储库被视为反模式?

在我看来,许多专业的存储库类共享相似的特性,让这些类实现一个概述这些特性的接口是有意义的,创建一个通用的存储库 说明我的观点,说我们有这个代码 public class IEntity { public int Id; } public interface IRepository where T: IEntity { IEnumerable List { get; } void Add(T entity); void Delete(T entity); void Update(T entity); T FindById(int Id); } [Table(“Author”)] public partial class Author : IEntity { public int Id { get; set; } [Required] public string authorname { […]

存储库和工作单元 – 负责什么?

在过去一周左右的时间里,我一直在阅读很多关于存储库模式的文章和教程。 许多文章将存储库模式与工作模式单元紧密联系在一起。 在这些文章中,我通常会找到与此类似的代码: interface IUnitOfWork { void RegisterNew(TEntity entity); void RegisterDirty(TEntity entity); void RegisterDeleted(TEntity entity); void Commit(); void Rollback(); } interface IRepository { TEntity FindById(TKey id); IEnumerable FindAll(); void Add(TEntity entity); void Update(TEntity entity); void Delete(TEntity entity); } class Repository : IRepository { public Repository(IUnitOfWork context) { this.context = context; } private IUnitOfWork context; public […]

ASP.NET MVC:有多少个存储库?

我正在使用ASP.NET MVC设计一个网站,对于存储库的确切性质可能有点困惑。 在NerdDinner示例之后,我的站点应该有一个存储库,根据需要提供实体。 但是,我也听说你应该有不同的存储库来处理特定的相关实体集…. 在我的网站的情况下,将有许多实体(大约15个表)但大多数都是相关的。 拥有一个包含拉动/更新/删除等所需方法的存储库,或者我应该将它们拆分,这是否可取/可取?

entity framework4.2“该类型不归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中

我收到以下错误: System.InvalidOperationException未处理Message =类型’Judge’未归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中。 不使用EdmEntityTypeAttribute的POCO实体不能与使用EdmEntityTypeAttribute的非POCO实体包含在同一程序集中。 Source = EntityFramework StackTrace:System.Data.Entity.InternalContext.UpdateEntitySetMappingsForType(Type entityType)at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)…. public class GenericRepository where TEntity : class { internal z context; internal DbSet dbSet; public GenericRepository(z context) { this.context = context; this.dbSet = context.Set(); } public GenericRepository() { this.context = new z(); this.dbSet = context.Set(); } public virtual IEnumerable Get( Expression<Func> filter = null, […]

使用entity framework使用存储库和单元工作模式正确处置?

干杯!我对使用工作单元和存储库有些怀疑。 特别是来自Entity Framework的子上下文的角色。 我已经搜索了很多关于这个主题的信息,但是我发现只是不同类型的使用模式,我很困惑,我无法理解主要思考。 1.我应该在哪里实现处置和储蓄? – 它是否在DbContext的inheritance类中正确实现了Disposable? 之后在存储库和工作单元中实现还是只在Uni fo工作中实现? – 把方法保存在工作单元或存储库中? 我的存储库将是Generic我的代码在架构师风格和其他细节方面是否正确?请告诉我的想法是否错误。 interface IRepository : IDisposable { void Create(); void Delete(); void Update(); void Get(); T getSomeByExpression() …Some another costum operations …should I remember about Save here? } class Repository : IRepository { SomeContext context = new SomeContext(); …Using using(context = new SomeContext()){} in functions?? […]

如何将Entity Framework DbContext注入SharpRepository的ConfigurationBasedRepository

我真的想将SharpRepository与Ninject一起使用,但我不明白如何配置Ninject以在存储库之间共享Entity Framework DbContext。 我正在使用Entity Framework版本5和Ninject版本3。 目前我在源代码中使用Ef5Repository ,但我想用ConfigurationBasedRepository替换它。 但我无法弄清楚如何将EF DbContext传递(或注入)到存储库。 示例(当前状态): using SharpRepository.Repository; public interface IProductRepository : IRepository { } using SharpRepository.Ef5Repository; using System.Data.Entity; // TODO Tightly coupled to Ef5Repository. public class ProductRepository : Ef5Repository, IProductRepository { // TODO The DbContext has to be injected manually. public ProductRepository(DbContext context) : base(context) { } // […] } […]

在MVC中使用SelectListItem创建一个通用存储库DropDown

几个月后我回到了这个问题,我已经在下面添加了我目前的最佳答案。 在最初的问题中,我仍然在寻找一种实现通用DropDown的简单方法,但标题与我当时遇到的具体错误更紧密相关。 我修改了标题以更密切地反映答案。 希望这可能对某人有所帮助。 原始问题: DropDownListFor throws的通用编辑器模板无法转换类型错误 我正在尝试使用从以下post中提取的想法为下拉列表创建通用模板: 将Html DropDownListFor移动到编辑器模板中 我创建了一个DropDownHelper类: public class DDLOptions { public T Value { get; set; } public IEnumerable Items { get; set; } } 我从这里修改了控制器: public ActionResult Create() { var model = new FilmEditViewModel(); FilmDropDownViewModel films = new FilmDropDownViewModel { Items = _repo.GetSelect(), }; model.filmName = films; return View(model); […]