repository-pattern

Should I implement DTOs in repository pattern with EF?

此生再无相见时 提交于 2019-12-05 20:18:46
问题 In my project I'm using EF Code First approach. I have a repository layer, service layer and presentation layer (ASP.NET MVC). I'm using a dedicated viewmodel for each view. What I'm confused about is that should my services return entities to the controller for mapping them to the viewmodels, or should I implements DTOs and return them from services? So the question is when the flow is like "EF -> Repository -> Service -> UI", what will be the data transformation. "Entity -> DTO -> Viewmodel

EF4: how to use a generic repository pattern ?

99封情书 提交于 2019-12-05 18:39:10
I'm trying to streamline my existing repos by using a generic repo I can subclass from. The problem is that I can't figure out how to write a few of my base class methods. I currently have: public interface IRepository<T> : IDisposable where T : class { IQueryable<T> GetAll(); T GetSingle(int id); T GetSingle(string slug); void Save(T entity); } public class HGRepository<T> : IRepository<T> where T : class { protected HGEntities _siteDB; protected IObjectSet<T> _objectSet; public HGRepository(HGEntities context) { _siteDB = context; _objectSet = _siteDB.CreateObjectSet<T>(); } public

SOLID principles, Repository pattern and EntityFramework cache in Asp Net Mvc

好久不见. 提交于 2019-12-05 18:34:25
I have a visual studio solution using solid pattern. I have a IRepository (Crud implementation), IDbFactory (used by a repository), IUnitOfWork. I also have services, who uses repositories to build custom querys and complex database operation. I am using also IoC pattern with Ninject. In a web mvc controller I use only services to access to database. A repository receive a IDbFactory who build a EntityFramework Context. I have some problems: In a service when i have to access to two tables for join them i should use two repositories, calling the GetAll() method of both of them. In this case

IUnitOfWork how to use - best practice

倾然丶 夕夏残阳落幕 提交于 2019-12-05 17:51:34
I'm using EF4.3.1 in a .Net4.0 web forms (not MVC!) application. I tend to use the repository pattern with an IUnitOfWork interface. But I'm wondering if I'm following best practices, especially since most examples I've followed are based on MVC apps. I will say it's only a small web app, so that may affect the solution choices. The solution currently has 3 projects, Model, Logic and Site. Model contains the codefirst entities and the IUnitOfWork interface. Logic contains the repositories and service layer. Site obviously contains the website, codebehind, etc. I don't use any third-party

Enable @PrePersist and @PreUpdate in Spring

别等时光非礼了梦想. 提交于 2019-12-05 17:43:59
How does one turn on the required Spring / JPA / Hibernate behaviours to invoke @PrePersist and @PreUpdate hooks? Our stack includes those three and a Repository for each entity, but we feel it is better to model this as a resposibility of the entity itself with the help of these hooks, but they are not called. Each Repository has an EntityManager injected. I am using those EntityManagers, not Sessions. The EntityManager is created by a org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean which uses the org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter and org

Managing Transactions either in service layer or repository layer?

此生再无相见时 提交于 2019-12-05 17:15:18
I have a certain scenario where inserts and updates are done on multiple tables based on some constraints ..so its natural to use Transaction scope for these scenarios.Now,I have a respository layer and a service layer. service layer mediates the repository and the UI and is persistent ignorant. Now i m confused where to use the transactions either in service or in repository layers.I am not using any ORMs. I have also seen people advocating about Unit of work pattern for such scenarios. are there any examples about unit of work pattern that suits my current scenarios,all the examples i have

Why aren't data repositories static?

人走茶凉 提交于 2019-12-05 14:21:45
问题 I was looking at the repository method for an ASP.NET MVC app and noticed a static class wasn't used. Since the repo is CRUD , why not make it static ? 回答1: 1) It's difficult to do unit testing with static classes (if you are testing a class that depends on your repository, you want that test to work against a fake 'mocked' repository object instead of your real one) 2) You often want to have 1 repository instance per-request to make it easier to ensure that uncommited changes from one user

Is it ok for a DDD repository work with summary objects in addtion to “real” objects

不羁的心 提交于 2019-12-05 13:14:14
Say I'm creating a repository to store digital E-Books as shown in the interface below. This repository will store the actual text of the book, as well as the metadata that identifies the book (title, author, publisher, ISBN etc..). public interface IBookRepository { void AddBook(Book newBook); void DeleteBook(int bookId); void UpdateBook(Book updatedBook); Book GetBook(int bookID) } public class Book { public int BookId {get; set;} public string Title {get; set;} public string Author {get; set;} public IList<Page> Contents {get; set} } public class Page { public int PageNumber {get; set;}

How can I wrap Play/JPA's Model class with a generic Repository?

孤人 提交于 2019-12-05 12:44:02
I don't like working with models objects directly, because this breaks encapsulation. Instead, I prefer the Repository Pattern . When I try to implement a simple repository public abstract class BaseRepository<T extends Model> { public T findOne(String query, Object... params) { GenericModel.JPAQuery result = T.find(query, params); return result.first(); } } public class UserRepository extends BaseRepository<User>{} UserRepository repo = new UserRepository(); repo.findOne("byUsername", "test"); I get exceptions because of the way java's generic or JPA annotations work: java.lang

MVC: Repositories and Services

早过忘川 提交于 2019-12-05 10:17:30
I am confused as to the limitations of what gets defined in the Repositories and what to leave to the Services. Should the repository only create simple entities matching tables from the database or can it create complex custom object with combinations of those entities? in other words: Should Services be making various Linq to SQL queries on the Repository? Or should all the queries be predefined in the Repository and the business logic simply decide which method to call? You've actually raised a question here that's currently generating a lot of discussion in the developer community - see