repository-pattern

Repository pattern and mapping between domain models and Entity Framework

落爺英雄遲暮 提交于 2019-11-27 17:03:31
My repositories deal with and provide persistence for a rich domain model. I do not want to expose the anemic, Entity Framework data entity to my business layers, so I need some way of mapping between them. In most cases, constructing a domain model instance from a data entity requires the use of parameterised constructors and methods (since it is rich). It is not as simple as a property/field match. AutoMapper could be used for the opposite situation (mapping to data entities) but not when creating domain models. Below is the core of my repository pattern. The EntityFrameworkRepository class

Join or Mutible DBContext Repository Generic c#

纵饮孤独 提交于 2019-11-27 16:32:23
I have repository generic where I do method as Get,Update,Insert. I get a data from table in data base I use this method. public IEnumerable<typeEntity> Get<typeEntity>(Expression<Func<typeEntity, bool>> newObjectEntity,int page, int rowsByPage) where typeEntity : class { List<typeEntity> Result = null; Result = Context.Set<typeEntity>().Where(newObjectEntity).OrderBy(m => true).Skip<typeEntity>(5 * (page - 1)).Take<typeEntity>(rowsByPage).ToList<typeEntity>(); return Result; } I when get data only a one table this is my code: var collecProducts = repository.Get<Products>(c => true); My

C# How to convert an Expression<Func<SomeType>> to an Expression<Func<OtherType>>

情到浓时终转凉″ 提交于 2019-11-27 14:01:27
问题 I have used C# expressions before based on lamdas, but I have no experience composing them by hand. Given an Expression<Func<SomeType, bool>> originalPredicate , I want to create an Expression<Func<OtherType, bool>> translatedPredicate . In this case SomeType and OtherType have the same fields, but they are not related (no inheritance and not based on a common interface). Background: I have a repository implementation based on LINQ to SQL. I project the LINQ to SQL entities to my Model

How to de-attach an entity from a Context in Entity Framework?

大城市里の小女人 提交于 2019-11-27 12:31:49
I use EF 4.1 with Repository and DbContext.. POCO with T4 template. For every Repository I use a separate DbContext. I need to update an object with has a related property, at the moment I receive this error An entity object cannot be referenced by multiple instances of IEntityChangeTracker. I suppose my problem is beacuse eventObj and candidate are created from different Repositories. So i'm trying to solve the problem with this code, with no success. My question? How do I get rid of this error? Is is possible remove the candidate from its context? public void UpdateAddingCandidate(Event

Where to convert business model to view model?

≯℡__Kan透↙ 提交于 2019-11-27 12:10:53
In my ASP.NET MVC application, I am using unit of work and repository patterns for data access. Using the unit of work class and the repository defined inside it I am fetching the related set of entities in my controller. With my beginner knowledge, I can think of two ways to fetch the business model and convert it to view model. Repository returns the business model to controller, this model than mapped to view model, or Repository itself converts business model to view model and then it is returned to controller. Currently I am using first approach, but my controller code started to look

Onion Architecture, Unit of Work and a generic Repository pattern

被刻印的时光 ゝ 提交于 2019-11-27 10:22:55
问题 This is the first time I am implementing a more domain-driven design approach. I have decided to try the Onion Architecture as it focuses on the domain rather than on infrastructure/platforms/etc. In order to abstract away from Entity Framework, I have created a generic repository with a Unit of Work implementation. The IRepository<T> and IUnitOfWork interfaces: public interface IRepository<T> { void Add(T item); void Remove(T item); IQueryable<T> Query(); } public interface IUnitOfWork :

Repository Pattern in Entity framework 4 when should we dispose?

感情迁移 提交于 2019-11-27 10:13:49
New to EF and I have noticed that using a repository pattern can really simplify things and will allow me to do some mocking too.So far so good. My Question A typical usage of the objectContext is to destroy as soon as possible see below using (var context = new SchoolEntities()) { context.AddToDepartments(department); context.SaveChanges(); } Using the Repository pattern I have noticed that nobody actually uses the "Using Pattern" eg using (var repository= new Repository<Student>(new MyContext)) { repository.Add(myStudentEntity) repository.SaveChanges(); } Should the idea be that we should

LINQ to SQL and the repository pattern

泄露秘密 提交于 2019-11-27 10:10:31
I feel like I'm running around in circles. I can't seem to make up my mind as to what the right repository pattern is using LINQ to SQL . If you're familiar with Rob Conery's MVC Storefront you will see that his implementation wraps the LINQ-generated models with another class and treats the LINQ-generated one simply as a data transfer object (DTO). It looks something like this: //Custom wrapper class. namespace Data { public class Customer { public int Id {get;set;} public string Name {get;set;} public IList<Address> Addresses {get;set;} } } //Linq-Generated Class - severly abbreviated

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

守給你的承諾、 提交于 2019-11-27 09:46:53
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 purposes? Or is there any better solution in terms of best practices? How should I manage Generic Repository

Repository pattern with Entity framework

南楼画角 提交于 2019-11-27 09:42:07
问题 Repository pattern is used to abstract from particular database and object-relation-mapping technology(like EF) used. So I can easily replace (for example) my Entity framework mappings with Linq to SQL in the future if I decide to do so. But when I use EF I have my entity classes from the model - that is they are generated from that visual diagram. If I use this generated entity classes in my repository and then decide to replace EF by something else then I will delete that visual entity