So, I have a whole system built as microservices.On a service level, I'm utilizing Hexagonal Architecture with lightweight DDD (no events, aggregators etc.)
I have a service that fetches Users via their Tokens, and then I have to fetch their Permissions accordingly.
I'm trying to figure out how to fetch data via EF, without leaking the implementation details to the domain layer.
Approach A: enter image description here
This allows me to lazyload Permissions onto Users after running the method, but since whole logic is contained in the domain - I'm leaking the implementation (usage of IQueryable).
Approach B: enter image description here
This also leaks implementation, because I'm restricting domain to EF - If I used an in-memory database instead of EF - I wouldn't need this solution, and thus it's a bad solution.
Approach C: enter image description here
This is the only solution that doesn't leak anything and allows me to easily plug the repository out of EF, and plug a text file, an in-memory database or anything really.
The only issue is - it eager-loads data, which will become a huge query if I use this approach for stuff like: Posts, Comments etc.
I'm clueless at this point, is there a 4th option?
1 Answer 1
The simplest way to use lazy-loading is by installing the Microsoft.EntityFrameworkCore.Proxies package and enabling it with a call to UseLazyLoadingProxies.
For example:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString);
Or when using AddDbContext:
.AddDbContext<BloggingContext>(
b => b.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString));
https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy
Explore related questions
See similar questions with these tags.
ToList()
call and it shouldn't eager load anymore.ToList()
on the result of the GetUsersByTokens call?