I am trying to get away from the Entity Framework since I have to support HANA databases other than SQL Server databases in our solution.
I do some research using dapper, so I created a quick test environment with some kind of dummy script.
I have the following POCOs that resemble my database schema (I have more, but I limited myself to their simplicity):
public class Adopter { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string Address2 { get; set; } public string City { get; set; } public State State { get; set; } public int StateId { get; set; } public string Zip { get; set; } public string Email { get; set; } public string Phone { get; set; } public string Fax { get; set; } public IEnumerable<Pet> Pets { get; set; } } public class State { public int Id { get; set; } public string Name { get; set; } public string Abreviation { get; set; } } public class Pet { public int Id { get; set; } public string IdTag { get; set; } public string Name { get; set; } public DateTime AdmitionDate { get; set; } public Status Status { get; set; } public int StatusId { get; set; } public string Notes { get; set; } public DateTime AdoptionDate { get; set; } public bool IsAdopted { get; set; } public int? AdopterId { get; set; } public int Age { get; set; } public decimal Weight { get; set; } public string Color { get; set; } public Breed Breed { get; set; } public int BreedId { get; set; } public Gender Gender { get; set; } public int GenderId { get; set; } public IEnumerable<PetImage> PetImages { get; set; } } public class Status { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public class Gender { public int Id { get; set; } public string Name { get; set; } }
I use the following in the repository to return a list of all adoptive parents:
using (SqlConnection connection = new SqlConnection(_connectionString)) { var adopters = connection.Query<Adopter>("SELECT a.* FROM Adopters a"); foreach (var adopter in adopters) { adopter.State = connection.QueryFirst<State>("Select s.* FROM States s WHERE s.Id = @Id", new { Id = adopter.StateId }); adopter.Pets = connection.Query<Pet>("Select p.* FROM Pets p WHERE p.AdopterId = @Id", new { Id = adopter.Id }); foreach (var pet in adopter.Pets) { pet.Status = connection.QueryFirst<Status>("Select s.* FROM Status s WHERE s.Id = @Id", new { Id = pet.StatusId }); pet.Gender = connection.QueryFirst<Gender>("Select g.* FROM Genders g WHERE g.Id = @Id", new { Id = pet.GenderId }); } } return adopters; }
As you can see, I extract the data for each POCO individually in accordance with the previous one and manually make Joins in the code.
Is this the right way to do this or should I make a large query with multiple joins and match the result somehow through dapper and LINQ?
source share