repository-pattern

How can I query cross tables with Repository Pattern?

落花浮王杯 提交于 2019-12-04 02:43:12
In my asp.net mvc 3 application, I'm using the repository pattern. I have 3 entities, Company, Country, City. Each of them has their own repository. Company entity has FoundedCountry and FoundedCity foreign keys. Now in a view, I want to show the company details. In this view I want to view Company details as well as, FoundedCountry name and FoundedCity name. In my opinion I have to handle this with a kind of JOIN query. But I'm stuck at how to achieve this in repository pattern. How can I handle this JOIN in repository pattern? Thank you. The repository should have a task-based interface.

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

眉间皱痕 提交于 2019-12-04 02:15:47
问题 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

Setting up a repository pattern in MVC

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:56:07
I'm trying to figure out how the Repository pattern works and how it can be implemented in a custom MVC pattern. As far as i understand it, the Repository is a layer which simply returns data from an entity class or saves the entity class to a persistent layer. Now i currently see it like this: A request comes in into my controller to create a user. Just a username and password. My controller will do something like this: function CreateAction ( ) { $userRepo = new userRepository ( ); $user = new userEntity ( ); $user->setUsername('user'); $user->setPassword('123456'); $userRepo->create($user);

Why aren't data repositories static?

无人久伴 提交于 2019-12-04 00:26:37
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) 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 don't mess things up for another user. Repository pattern increase testability, static classed decreases it.

Multiple database contexts when using repository pattern

ε祈祈猫儿з 提交于 2019-12-03 22:13:01
I am a bit lost right now... I've never seen this much divergent information regarding solution to the problem. But let us start from the beginning. I am using ASP.NET MVC with Repositories injected to Controllers, thanks to the Ninject. I have 2 simple Entities: Admin with a list of created blog entries and Entries with one virtual Admin field. Admin: public class Admin { [Key, ScaffoldColumn(false)] public int Id { get; set; } [Required(ErrorMessage = "Zły login.")] [StringLength(20), MinLength(3)] [RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "Special characters are not allowed.")]

Setting the identity of a Domain Entity

十年热恋 提交于 2019-12-03 20:43:01
All entities in the domain need to have identity. By inheriting from DomainEntity , I am able to provide identity to classes. City domain entity (stripped down for easy reading): public class City : DomainEntity, IAggregateRoot { public string Name { get; private set; } public Coordinate Coordinate { get; private set; } public City(string name, decimal latitude, decimal longitude) { Name = name; SetLocation(latitude, longitude); } public City(string name, decimal latitude, decimal longitude, int id) : base(id) { Name = name; Coordinate = coordinate; SetLocation(latitude, longitude); } public

If I expose IQueryable from my service layer, wouldn't the database calls be less if I need to grab information from multiple services?

99封情书 提交于 2019-12-03 20:28:42
If I expose IQueryable from my service layer, wouldn't the database calls be less if I need to grab information from multiple services? For example, I'd like to display 2 separate lists on a page, Posts and Users . I have 2 separate services that provides a list of these. If both provides IQueryable, will they be joint in 1 database call? Each repository creates a context for itself. It's best to think of an IQueryable<T> as a single query waiting to be run. So if you return 2 IQueryable<T> instances and run them in the controller, it wouldn't be any different than running them separably in

EF repository pattern many to many insert

一曲冷凌霜 提交于 2019-12-03 20:24:36
We have 2 tables: Table Authority: public class Authority { public int ID {get;set;} public string Name{get;set;} ... } Table Agents public class Agent { public int ID{get;set;} public int FirstName{get;set;} } And we have a relationship many-to-many between these two tables: public class AuthorityConfiguration : EntityTypeConfiguration<Authority> { public AuthorityConfiguration() : base() { HasKey(p => p.ID); HasMany(p => p.Agents).WithMany(a => a.Authorities).Map(mc => { mc.MapLeftKey("AuthorityID"); mc.MapRightKey("AgentID"); mc.ToTable("AuthorityAgent"); }); ToTable("Authority"); } }

Issues with my MVC repository pattern and StructureMap

流过昼夜 提交于 2019-12-03 19:19:28
问题 I have a repository pattern i created on top of the ado.net entity framework. When i tried to implement StructureMap to decouple my objects, i kept getting StackOverflowException (infinite loop?). Here is what the pattern looks like: IEntityRepository where TEntity : class Defines basic CRUD members MyEntityRepository : IEntityRepository Implements CRUD members IEntityService where TEntity : class Defines CRUD members which return common types for each member. MyEntityService : IEntityService

Repository Pattern - Caching

不打扰是莪最后的温柔 提交于 2019-12-03 18:46:22
问题 I'm not sure where I should implement the caching in my repository pattern. Should I implement it in the service-logic or in the repository? GUI -> BusinessLogic (Services) -> DataAccess (Repositories) 回答1: I would handle it in the repository/data access layer. The reasoning is because it isn't up to the business layer on where to get the data from, that is the job of the repository. The repository will then decide where to get the data from, the cache (if it's not too old) or from the live