0
\$\begingroup\$

I am implementing a generic repository pattern. I have done this quite a few times now but every time I do it there is something bugging me.

If I have a database design like below. (All the tables relate to one another.) And I make a call like the following:

public IEnumerable<Domain> GetRealEstate()
{
     return _repository.GetAll();
}

enter image description here

I can get all the models from just that one call (The wonders of EF). The thing that bugs me is the fact that I have to say Domain in the method call, from the domain entity I will get all the relevant entity (Lazy loading) Companies etc. etc. It just feels wrong to use domain entity to get all the companies etc. etc. The repo pattern that I am using is a straight forward one.

Is there a better way of writing the methods so that it does not look so weird?

Controller RealEstateController

[RoutePrefix("api/realestate")]
    public class RealEstateController : ApiController
    {
        private readonly IRealEstateService _service;

        public RealEstateController(IRealEstateService service)
        {
            _service = service;
        }

        [Route("")]
        public Task<Domain> GetRealEstates()
        {
            var collection =  _service.GetRealEstate();
            return null;
        }

        [Route("{domainName}")]
        public Task<Domain> GetRealEstate(string domainName)
        {

        }
    }

Service

public class RealEstateService : IRealEstateService
    {
        private readonly IRealEstateRepository _repository;
        public RealEstateService(IRealEstateRepository repository)
        {
            _repository = repository;
        }
        public IEnumerable<Domain> GetRealEstate()
        {
            return _repository.GetAll();
        }
    }
\$\endgroup\$
1
  • \$\begingroup\$ I dont understand what the problem with the question is? Why is it put on hold. PS. this is not someone else's code this is code that i wrote and is in my project. \$\endgroup\$
    – R4nc1d
    Jun 15, 2015 at 6:57

1 Answer 1

1
\$\begingroup\$

I know this is not 100% spot on, but you should be able to take something from it and apply it in your own repository.

Here's a piece of code I use for one of my EF repositories, which I find very useful. I use a lot of Razor templating which won't lazy-load entities, so whenever I need to eagerly-load, I just include whatever relational entities I need before passing it to razor.

    public IQueryable<T> FindByInclude<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] include) where T : class
    {
        IQueryable<T> query = _context.Set<T>().Where(predicate);

        foreach (var item in include)
        {
            query = query.Include(item);
        }

        return query;
    }

Example:

var user = Repo.FindByInclude<User>(z=>z.Name = "Joachim Nordvik", "Address","Properties.Extended");

Now both the Address relation and Properties.Extended relations are eagerly loaded.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for your reply, from some reason these idiots closed the question. Clearly they have no clue what is going on. Ill probably end up using something you suggested. Just have to test it in my scenario. +1 for answering. \$\endgroup\$
    – R4nc1d
    Jun 22, 2015 at 13:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.