0

I'm working on a social media website, we are developing a notification system Facebook style enter image description here

The problem is that we have a Notification base class and there are many different derived notification's classes as MentionNotification, LikeNotification.

This derived classes many times has their own navigation properties, so we has to change NotificationRepository to Include all this new navigation properties,

I can't find a way to avoid modify the Base repository avoiding N+1 query.

Any idea will be welcome

RJardines
  • 850
  • 9
  • 18

1 Answers1

0

If you're using Table Per Type... Then...

public class MyContext : DbContext
{
  public virtual DbSet<NotificationBase> Notifications { get; set; }
}

var mns = await MyContext.Notifications
  .OfType<MentionNotification>()
  .Include(mn => mn.SomeProperty)
  .ToListAsync();

(Off the top of my head, I'm sure it's something like this...)

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • The idea es design something that not need to change the Notification Repository base every time i need to add a new Notification type to the system. And it loads every notifications and its navigation properties. a call to notificationRepository.All() return all the notifications in the system with its navigation properties loaded. If i add a new project with a new type of notification i could still calling notificationRepository.All() and the new type is also returned. – RJardines Jun 24 '16 at 20:57
  • I don't think it must be done in just one query because maybe the query generated is so time consuming that we can still getting timeouts, but could be a little more intelligent to load the notifications for type and do unions, although the worst case here will be N+1. Just playing with differents idea to resolve this problem – RJardines Jun 24 '16 at 21:01
  • If it's timing out it sounds like you're having a [Cartesian Product](http://stackoverflow.com/questions/22161234/optimize-entity-framework-query/22625208#22625208) problem. – Erik Philips Jun 24 '16 at 21:23
  • *The idea es design something that not need to change the Notification Repository base every time i need to add a new Notification type to the system* - I would not recommend that with Entity Framework, you lose a ton of very cool features, but [each to their own](http://www.usingenglish.com/reference/idioms/each+to+their+own.html). – Erik Philips Jun 24 '16 at 21:25
  • thanks by your ideas, i think the best way will be denormalize my data and have to deal with that avoiding hierarchy – RJardines Jun 30 '16 at 14:16