Tag: repository pattern

如何实现通用的RepositoryFactory?

我正在尝试实现一个通用存储库。 这是我到目前为止所得到的…… public interface IRepositoryFactory { IRepository RepositoryOf() where T : class; } public class EntityFrameworkRepositoryFactory : IRepositoryFactory { private readonly IWindsorContainer _container; public EntityFrameworkRepositoryFactory(IWindsorContainer container) { _container = container; } public IRepository RepositoryOf() where T : class { var repository = _container.Resolve<IRepository>(); return repository; } } RepositoryFactory由我的工作单元实现使用 public interface IUnitOfWork : IDisposable { IRepository […]

适用于Silverlight 4 + WCF数据服务的IAsyncRepository或IObservableRepository

更新2 :@Enigmativity有一个很好的答案。 我已将其实现为IObservableRepository 。 我的答案详情如下。 问:所以我已经改变了大部分问题(参见编辑历史)如果有人对我的设计进行了评论/validation/推测,我会喜欢它。 =) 所以通常我的Repos看起来像这样: public interface IRepository where T : class { T GetById(int id); IQueryable GetAll(); void InsertOnSubmit(T entity); void DeleteOnSubmit(T entity); int SubmitChanges(); } 但是当谈到Silverlight和WCF数据服务时,它会使所有异步的查询相关数据变得非常烦人。 我必须首先加载父实体异步,然后查询其子实体异步。 所以我提出了一个IAsyncRepository ,我想知道设计是否正常,是否可以改进,(以及在这里使用Rx是否有意义?) 要解决子实体问题,我计划在调用回调之前加载所有必需的子实体。 我的回购看起来像: public interface IAsyncRepository where T : class { void GetById(int id, Action callback); void GetAllFromQuery(Func<MyEntities, IQueryable> funcquery, Action<IList> callback) […]

没有ORM的存储库模式

我在不使用ORM的.NET C#应用程序中使用存储库模式。 但是,我遇到的问题是如何填充实体的一对多列表属性。 例如,如果客户有一个订单列表,即如果Customer类有一个名为Orders的List属性,而我的存储库有一个名为GetCustomerById的方法,那么? 我应该在GetCustomerById方法中加载Orders列表吗? 如果订单本身有另一个列表属性等等怎么办? 如果我想做懒加载怎么办? 我在哪里将代码加载到客户的Orders属性? 在Orders属性中获取{} accessor? 但那时我必须将存储库注入域实体? 我不认为这是正确的解决方案。 这也引发了诸如变更跟踪,删除等function的问题? 所以我认为最终结果是我可以在没有ORM的情况下进行DDD吗? 但是现在我只对我的域实体中的延迟加载List属性感兴趣? 任何的想法? 纳比勒 我假设对于没有在域驱动设计中使用ORM的人来说,这是一个非常常见的问题? 任何的想法?

使用UnitOfWork和存储库模式与entity framework

我将在我的数据访问层中使用存储库和UnitOfwork来执行此操作,查看一个联系人aggregateroot public interface IAggregateRoot { } 这是我的Generic存储库接口: public interface IRepository { IEnumerable GetAll(); T FindBy(params Object[] keyValues); void Add(T entity); void Update(T entity); void Delete(T entity); } 和我在模型中的POCO Contact类 public class Contact :IAggregateRoot { public Guid Id { get; set; } public string Name { get; set; } public string Email { get; set; } […]

在大型项目中使用通用存储库/工作单元模式

我正在开发一个非常大的应用程序。 该域有大约20-30种类型,实现为ORM类(例如EF Code First或XPO,对于该问题无关紧要)。 我已经阅读了几篇关于存储库模式的通用实现的文章和建议,并将它与工作单元模式相结合,产生了类似这样的代码: public interface IRepository { IQueryable AsQueryable(); IEnumerable GetAll(Expression<Func> filter); T GetByID(int id); T Create(); void Save(T); void Delete(T); } public interface IMyUnitOfWork : IDisposable { void CommitChanges(); void DropChanges(); IRepository Products { get; } IRepository Customers { get; } } 这种模式适合大型应用吗? 每个示例在工作单元中有大约2个,最多3个存储库。 据我了解的模式,在一天结束时,存储库引用的数量(在实现中初始化的延迟)与域实体类的数量相等(或几乎相等),因此可以使用工作单元复杂的业务逻辑实现。 例如,让我们像这样扩展上面的代码: public interface IMyUnitOfWork : IDisposable { […]

entity framework通用存储库错误

我正在尝试为我的Entity Framework存储库创建一个非常通用的generics存储库,该存储库具有基本的CRUD语句并使用接口。 我先打了一个砖墙头,然后被打倒了。 这是我的代码,使用entity framework模型在控制台应用程序中编写,其中包含名为Hurl的表。 只需尝试通过其ID撤回对象。 这是完整的应用程序代码。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects; using System.Linq.Expressions; using System.Reflection; using System.Data.Objects.DataClasses; namespace GenericsPlay { class Program { static void Main(string[] args) { var hs = new HurlRepository(new hurladminEntity()); var hurl = hs.Load(h => h.Id == 1); Console.Write(hurl.ShortUrl); Console.ReadLine(); } } public interface IHurlRepository […]

将多个DbContexts与通用存储库和工作单元一起使用

我的应用程序越来越大,到目前为止,我有一个MyDbContext ,它包含我在我的应用程序中需要的所有表。 我希望(为了概述)将它们分成多个DbContext ,如MainDbContext , EstateModuleDbContext , AnotherModuleDbContext和UserDbContext 。 我不确定这是怎么做的可能因为我现在正在使用dependecy injection(ninject)将我的DbContext放在我的UnitOfWork类上,如: kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork)); 我是否应该通过dependency injection和显式设置我希望在我的服务上使用的DbContext来删除此方法,例如: private readonly EstateService _estateService; public HomeController() { IUnitOfWork uow = new UnitOfWork(); _estateService = new EstateService(uow); } 代替: private readonly EstateService _estateService; public HomeController(IUnitOfWork uow) { _estateService = new EstateService(uow); } 或者这有另一种更好的方法吗? 另外作为一个附带问题,我不喜欢将uow传递给我的服务 – 还有另一种(更好的)方法吗? 码 我有这个IDbContext和MyDbContext: public interface IDbContext { […]

在entity framework中处置对象上下文4

我有一个实体类,它是从我的数据库模型自动生成的。 该类inheritance了ObjectContext,它inheritance了IDisposable。 我创建了一个存储库,其中包含各种方法,这些方法使用实体对象的单个实例与数据库进行交互。 自动生成的类 public partial class DevEntities : ObjectContext { public const string ConnectionString = “name=DevEntities”; public const string ContainerName = “DevEntities”; 存储库类 DevEntities db = new DevEntities(); public Customer GetCustomerByID(int id) { var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id); return customers; } public Customer GetCustomerByPasswordUsername(string email, string password) { var customers = […]

存储库模式和多个相关的核心实体或业务对象 – 一个存储库或更多?

我正在考虑实现存储库模式(因为我提出的是90%的实现它),并且遇到了一个设计问题 – 我有两个或更多核心业务对象(例如,业务和联系人)一个CRM应用程序),BO可以强烈相关或根本不相关。 在这种情况下,我应该实现一个存储库(例如CrmRepository,.addBusiness(),。addContact()等),还是多个存储库(BusinessRepository,ContactRepository都有自己的.add(),. delete()等等)。 在这种情况下,最佳做法是什么? 底层DAL是EF4。 问候 武

如何在ASP.NET Core MVC中设计具有dependency injection的存储库模式?

作为ASP.NET Core 1.0 MVC的新手,我决定将一个Repository Pattern用于MVC Core应用程序; 我正在使用SQL DB作为数据层SampleDbContext ,我希望为我的一些业务实体提供一个Repository类。 到目前为止,我在startup.cs , CustomerController.cs和CustomerRepository.cs文件中完成了以下操作,其中示例实体是“Customer”。 在Startup类的ConfigureServices方法中: public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“SampleDB”))); } 在控制器中: public class CustomerController : Controller { private SampleDBContext _context; private CustomerRepository = new CustomerRepository (new SampleDBContext()); public CustomerController(SampleDBContext context) { _context = context; } } 在存储库中: public class CustomerRepository { private SampleDBContext […]