困境在EF代码优先使用继承 [英] dilemma on the use of inheritance in EF code-first

查看:122
本文介绍了困境在EF代码优先使用继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中实现如同的功能(类似于Facebook的)。我将有三种类型的像取值:喜欢上岗,征求意见喜欢(给个),并回复喜欢(的评论)。后来,根据我要为用户生成一个动态通知列表活动。

I am trying to implement Like functionality (similar to Facebook) in my application. I will have three types of Likes: likes for posts, likes for comments (to the posts), and likes for replies (to the comments). Later, based on the like activities I want to generate a dynamic notification list for the user.

对于此任务,我想继承会工作的伟大,所以我有以下类(如同是基类)

For this task, I thought inheritance would work great, so I have the following classes (Like is the base class)

如同:LikeId,LikeDate,WhoLiked,WhoseLiked,IsNotificationRead

Like : LikeId, LikeDate, WhoLiked, WhoseLiked, IsNotificationRead

PostLike :帖子ID

CommentLike :CommentId

CommentLike: CommentId

ReplyLike :ReplyId

我需要生成,看起来像一个通知列表:

I need to generate a notification list that looks like:


  1. 用户1很喜欢你的文章(帖子ID 应附此文件的)

用户2喜欢您的评论( CommentId 应附本)
项)

User2 liked your comment (CommentId should be attached to this) item)

用户3喜欢你的答复( ReplyId 应附此项目)

User3 liked your reply (ReplyId should be attached to this item)

不过,我有时间难以产生这个输出的最后2天。最后,我的结论是,我不能使用 dbcontext.Likes 一个LINQ语句中有这样的输出。

However, I am having hard time to generate this output for the last 2 days. Finally, I concluded that I cannot use dbcontext.Likes within a single LINQ statement to have this output.

我我打算分别生成列表中为每个继承实体,并在年底合并所有列表:

I am planning to generate the list separately for each inherited entity and merge all lists at the end:

  var postlikes = db.Like.OfType<PostLike>().Select(a => 
         new {Text = "User1 liked your post", ItemId=a.PostId, Type="Post"});

  var commentlikes = db.Like.OfType<CommentLike>().Select(a => 
         new {Text = "User1 liked your comment", ItemId=a.CommentId, Type="Comment"});

  var replylikes = db.Like.OfType<ReplyLike>().Select(a => 
         new {Text = "User1 liked your reply", ItemId=a.ReplyId, Type="Reply"});



你以为我实现继承的方式很有意义在这种情况下?你建议的另一种方法?你觉得我在这里需要继承?

Do you think the way I implemented Inheritance makes sense in this scenario? Do you recommend an another approach? Do you think I need inheritance here?

感谢

推荐答案

除非你必须从你的例子省略其他成员,你的做法似乎矫枉过正。再加上做3查询只是为了得到喜欢的通知似乎沉重。

Unless you have additional members elided from your example, your approach seems overkill. Plus doing 3 queries just to get likes for a notification seems heavy.

一个简单的实现将有目标ID 如同类C $ C>和的TargetType 属性,然后只需调整根据的TargetType 的文本。你不会得到导航属性的方式,但你可能并不需要它们。

A simple implementation would be to have a single Like class with TargetId and TargetType properties, then just adjust the text according the the value of TargetType. You wouldn't get navigation properties that way, but you might not need them.

如果你想喜欢你比如你的继承类,至少重构所以你打的数据库只有一次:

If you wanted your inherited classes like your example, at least refactor so you hit the database only once:

var likes = db.Like.ToList();

var postLikes = likes.OfType<CommentLike>()...

这篇关于困境在EF代码优先使用继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆