repository-pattern

Transactions in the Repository Pattern using ServiceStack.ORMLite

試著忘記壹切 提交于 2020-01-01 09:00:43
问题 I'm implementing Repository Pattern using ServiceStack.ORMLite like this: public class MyRepository : IMyRepository { private IDbConnectionFactory DbConnectionFactory = null; public MyRepository(IDbConnectionFactory dbConnectionFactory) { DbConnectionFactory = dbConnectionFactory; } public void MyMethod() { using (var connection = DbConnectionFactory.OpenDbConnection()) using (var cmd = connection.CreateCommand()) { //Do something here } } } But I don't know how to handle DbTransaction when I

Transactions in the Repository Pattern using ServiceStack.ORMLite

a 夏天 提交于 2020-01-01 09:00:11
问题 I'm implementing Repository Pattern using ServiceStack.ORMLite like this: public class MyRepository : IMyRepository { private IDbConnectionFactory DbConnectionFactory = null; public MyRepository(IDbConnectionFactory dbConnectionFactory) { DbConnectionFactory = dbConnectionFactory; } public void MyMethod() { using (var connection = DbConnectionFactory.OpenDbConnection()) using (var cmd = connection.CreateCommand()) { //Do something here } } } But I don't know how to handle DbTransaction when I

Repository & Unit of Work Pattern for Entity Framework using Dependency Injection

寵の児 提交于 2019-12-31 04:14:10
问题 I tried the T4-Template from http://efrepository.codeplex.com, but I'm struggling with the DI (uses StructureMap) Example code. It's just not enough for a DI beginner like me. It doesn't even compile. I already have done a few sample projects with its Non-DI Template. And it worked out great. I love, that it generates all necessary repositories and that I can easily customize and extend them. But I can't get the DI Examples working. Are there any similar projects (with better docs)? 回答1: I

Repository generic method GetById using eager loading

柔情痞子 提交于 2019-12-31 03:47:06
问题 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"); 回答1: One possible way is to use

C# automatic creation of repositories by entity type

落爺英雄遲暮 提交于 2019-12-31 02:44:08
问题 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 -

Repository pattern and inheritance in .net

…衆ロ難τιáo~ 提交于 2019-12-30 04:50:06
问题 i am pretty new to the repository design pattern and i have reached a dead end while trying to implement it, with regards to inheritance. I am not sure even if i started in the right direction. So basically i will have an abstract base class Product, with id and imagePath for instance, and will have several products which inherit from this. namespace Common { public abstract class Product { public int Id { get; set; } public string ImgPath { get; set; } } public class Scale : Product { public

C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

自作多情 提交于 2019-12-28 12:44:06
问题 I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repository. What is the smartest and safest way to go about this? 回答1: A design with multiple ObjectContext instances is only acceptable if your Repository methods commit the transaction. Otherwise, it is possible that external calls to commit the transaction may not persist everything you intend, because you will hold references to different

How should I manage Generic Repository Pattern when the works of different entities are pretty much different?

这一生的挚爱 提交于 2019-12-28 03:13:07
问题 I am new to Repository pattern. When I am managing the CRUD operations of several entities (like: customers, orders etc) then its fine. I am making an interface, I am making a Generic repository. And that serves my purpose, because CRUD operation is common for them. My question is: When the duties of several entities are totally different, there is no common method between them, in this case what should I do? Should I increase the number of interfaces and repositories for those specific

Repository Design Pattern with Dapper

好久不见. 提交于 2019-12-27 11:14:53
问题 This is maybe more a question for code review rather than stack overflow. I am using Dapper for a MicroORM to retrieve and Save Data to SQL Server 2014. I have got DTO classes in a DTO Proj that represent the Data retrieved from the DB or saved to the DB. I am using the Repository Pattern so at my Service layer if a repository is required I am using constructor DI to inject that dependency and then call the method on the Repository to do the work. so let say I have 2 services called

A second operation started on this context before a previous asynchronous operation completed with UnitofWork and async

谁说我不能喝 提交于 2019-12-25 19:36:29
问题 A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. My unitofwork code public class UnitOfWork : IUnitOfWork { private readonly CAMSDbEntities _context; private bool _disposed; public Dictionary<Type, object> repositories = new Dictionary<Type, object>(); private Guid _objectId