repository-pattern

Best Repository Pattern for ASP.NET MVC

随声附和 提交于 2019-12-17 17:18:21
问题 I recently learned ASP.NET MVC (I love it). I'm working with a company that uses dependency injection to load a Repository instance in each request, and I'm familiar with using that repository. But now I'm writing a couple of MVC applications of my own. I don't fully understand the hows and whys of the repository my company uses, and I'm trying to decide the best approach to implement data access. I am using C# and Entity Framework (with all the latest versions). I see three general

Repository pattern: One repository class for each entity?

夙愿已清 提交于 2019-12-17 15:37:34
问题 Say you have the following entities defined in a LINQ class: Product Customer Category Should I have one repository class for all: StoreRepository ... or should I have: ProductRepository CustomerRepository CategoryRepository What are the pro & cons of each? In my case, I have several "applications" within my solution... the Store application is just one of them. 回答1: Here's my point of view. I'm a strict follower of the Repository pattern. There should be 3 methods that take a single entity.

Decouple unit of work from services or repo

坚强是说给别人听的谎言 提交于 2019-12-17 15:35:11
问题 I am trying to decouple my unit of work from my services or repository so that I wont have to touch the UoW code whenever I wish to add a new service. How do I do this? _categoryService = _unitOfWork.Get<ICategoryService>(); so instead of _unitOfWork.CategoryService.Add(category) I can just say; _categoryService.Add(category); 回答1: I am trying to decouple my unit of work from my services or repository so that I won’t have to touch the UoW code whenever I wish to add a new service Well, that’s

Generate POCO classes in different project to the project with Entity Framework model

给你一囗甜甜゛ 提交于 2019-12-17 04:26:51
问题 I'm trying to use the Repository Pattern with EF4 using VS2010. To this end I am using POCO code generation by right clicking on the entity model designer and clicking Add code generation item. I then select the POCO template and get my classes. What I would like to be able to do is have my solution structured into separate projects for Entity (POCO) classes and another project for the entity model and repository code. This means that my MVC project could use the POCO classes for strongly

Generate POCO classes in different project to the project with Entity Framework model

冷暖自知 提交于 2019-12-17 04:26:10
问题 I'm trying to use the Repository Pattern with EF4 using VS2010. To this end I am using POCO code generation by right clicking on the entity model designer and clicking Add code generation item. I then select the POCO template and get my classes. What I would like to be able to do is have my solution structured into separate projects for Entity (POCO) classes and another project for the entity model and repository code. This means that my MVC project could use the POCO classes for strongly

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

北城以北 提交于 2019-12-17 03:22:34
问题 Using EF5 with a generic Repository Pattern and ninject for dependency injenction and running into an issue when trying to update an entity to the database utilizing stored procs with my edmx. my update in DbContextRepository.cs is: public override void Update(T entity) { if (entity == null) throw new ArgumentException("Cannot add a null entity."); var entry = _context.Entry<T>(entity); if (entry.State == EntityState.Detached) { _context.Set<T>().Attach(entity); entry.State = EntityState

Is it ok for entities to access repositories?

不问归期 提交于 2019-12-17 03:16:57
问题 I've just started working with DDD, so maybe this is a silly question... Is it ok for an entity to access a repository (via some IRepository interface) to get a value at runtime? For example, I want to enforce a "default" selection for a property: class Person { private Company _employer; public Company Employer { get { return _employer; } set { if(value != null) { _employer = value; } else { _employer = employerRepository.GetDefaultEmployer(); } } } ... } My question is whehter doing

TDD-friendly Singleton-like class

若如初见. 提交于 2019-12-14 04:25:37
问题 I have repository class that is used by at least 2 other classes. This repository class needs to be initialized - which is high in cost (querying database). Now, I create separate instances of repository wherever I need it. The thing is, that everytime I create repository it has to be initialized. How to design such repository to be TDD-friendly? The first thing in my mind was Singleton but it's not the solution. 回答1: Do you use any type of IOC container? Unity is my container of choice, and

Creating Entity Framework objects with Unity for Unit of Work/Repository pattern

ⅰ亾dé卋堺 提交于 2019-12-14 03:43:23
问题 I'm trying to implement the Unit of Work/Repository pattern, as described here: http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx This requires each Repository to accept an IUnitOfWork implementation, eg an EF datacontext extended with a partial class to add an IUnitOfWork interface. I'm actually using .net 3.5, not 4.0. My basic Data Access constructor looks like this: public DataAccessLayer(IUnitOfWork unitOfWork,

Can repository pattern be used for loading of “partial entities”

牧云@^-^@ 提交于 2019-12-14 00:33:55
问题 I'm trying to get better understanding of repository pattern in Domain Driven Design. All examples of repository pattern implementation are dealing with entities only. But what if i need to retrieve only some part of entity? For example, i have Client entity with large amount of properties. Can i define something like this in ClientRepository: public IEnumerable<string> GetClientsNames() { ... } of even: class ClientBaseInfo //includes some subset of properties of Client entity { public