repository-pattern

Generic Repository in Spring JPA

烈酒焚心 提交于 2019-12-02 06:00:52
We are working on a Restful project with lots of DB tables. Though the operations on the tables are almost same and mainly INSERT/UPDATE/DELETE/FETCH. my questions is: will we have to create a repository (extending JpaRepository) for every entity (Domain class) we create or, there is an option of creating a GenericRepository that can handle all the above-mentioned functionalities for all the entities? i.e a single GenericRepository for all. if so, could you share an example? is [there] an option of creating a GenericRepository that can handle all the above-mentioned functionalities for all the

Repository generic method GetById using eager loading

ⅰ亾dé卋堺 提交于 2019-12-02 05:30:39
I am using Entity Framework and would like to create generic GetById method in Repository class with eager loading: Here is my method which uses lazy-loading: public virtual TEntity GetById(object id) { return DbSet.Find(id); } I know method Find does not support eager loading, but how it is possible to modify this method to use eager loading, so that I use this method as follows(for an example): _unitOfWork.MyRepository.GetById(includeProperties: "Users"); One possible way is to use FirstOrDefault with predicate over the DbSet with Include s. It's not hard to build manually a predicate using

C# automatic creation of repositories by entity type

倖福魔咒の 提交于 2019-12-02 01:18:16
I found an example of Generic Repository which is based on Entity Framework and trying to understand how to automatically resolve repositories by the same interface and entity type. The link above leads to the repo where you can see the following approach: public class HomeController : Controller { private readonly ICategoryRepository _repository; public HomeController(ICategoryRepository repository) { _repository = repository; } in this case we have to create a separate CategoryRepository - so, repository is per type . That means we end up having a lot of classes for repositories. I would

ORM / How to deal with the correspondence between Domain object and Persistent object?

我只是一个虾纸丫 提交于 2019-12-01 20:21:12
In an application, there are at least two ways for dealing with domain object persistence and an ORM. Mapping directly the domain object to persistence using some kind of ORM (xml or annotations) Making separation of concerns in case of a lot of impedance mismatch between your domain and your persistent model (table columns). That means, domain object is persistence agnostic and there are some conversions to some corresponding persistent object, this latter maps to an ORM. As pure DDD developers know, domain should not be driven by your database needs, thus in my project, I use this separation

Entity Framework: How to check if value exists before submitting

廉价感情. 提交于 2019-12-01 14:01:28
I'm using the repository pattern. I have a Country repository that I'm using a service to submit. Where should I put the check to see if the country already exists in the database, I throw an exception? Is there a way to do this in one database call? (Check and insert if non-existent)? If this is possible, could it be done in the service layer? (if that is where you recommend I do the checks). Because of multi-user concurrency, you can't SELECT then INSERT and be guaranteed of no problems. So the solution proposed by @Coding Gorilla could fail in high concurrency situations. You should put a

Entity Framework: How to check if value exists before submitting

不打扰是莪最后的温柔 提交于 2019-12-01 13:36:00
问题 I'm using the repository pattern. I have a Country repository that I'm using a service to submit. Where should I put the check to see if the country already exists in the database, I throw an exception? Is there a way to do this in one database call? (Check and insert if non-existent)? If this is possible, could it be done in the service layer? (if that is where you recommend I do the checks). 回答1: Because of multi-user concurrency, you can't SELECT then INSERT and be guaranteed of no

Entity Framework 4.1 Generic Repository set up

你。 提交于 2019-12-01 13:24:50
问题 I'm new to Entity Framework and joined a project that uses a generic repository as shown below. Are there any disadvantages to the way the repository is set up? I've noticed that most tutorials describe creating multiple repositories based on a generic base repository rather than having one generic repository that handles everything. To give some background this code is part of an ASP.NET MVC 3 website and we're using unity as an IOC container. All of the components in the business layer

Does a Generic Repository need a Base Entity class to be applied everywhere?

為{幸葍}努か 提交于 2019-12-01 13:03:31
I am creating an Intranet website with ASP.NET MVC and Onion Architecture . I have been implementing the repository pattern but I have a difficulty. Let's say I have a Document table with IDDocument in it. Then this is my repo(with just one method): class Repository<T> : IRepository<T> where T : class { private readonly PrincipalServerContext context; private DbSet<T> entities; //Constructor and stuff here public T Get(long id) { return entities.SingleOrDefault(s => s.IDDocument == id);//Here is my problem } } The problem is that I cannot use this since T is not recognized as being from

Doctrine 2 Restricting Associations with DQL

被刻印的时光 ゝ 提交于 2019-12-01 12:27:51
问题 There seems to be an over sight in Doctrine 2.1 where it isn't easy to return a subset collection for an association. http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html#restricing-associations The docs recommend to write a repository find method, which makes sense because that was the first thing I though of doing. However without having a reference to the EntityManager within an Entity I can't see how you would retrieve the association's Repository

Searching for a Child across Aggregate Roots

只谈情不闲聊 提交于 2019-12-01 10:53:06
The repository pattern suggest that you can only pull aggregate roots. But how would you retrieve a single child using only it's uniqiue identity(Child.ID) if you do not know it's parent(root)? class Parent { public int ID { get; set; } IEnumerable<Child> Children { get; private set; } } class Child { public int ID { get; private set; } public virtual Parent Parent { get; private set; } // Navigational model } My application is stateless (web), for simplicity, the request only contains the ID of the child. I am thinking three approaches: Call all the parents then ask them politely who owns