【问题标题】:Polymorphic model in ASP MVC?ASP MVC 中的多态模型?
【发布时间】:2014-03-20 09:34:48
【问题描述】:

多态模型的概念。例如我们有这个表注释:

+----+---------------+-------------------+------------------------------------+
| id | commentable_id| commentable_type  |            comments_text           |
+----+---------------+-------------------+------------------------------------+
|  1 |    2          |      youtube      |        youtube rocks, buddy!       |
|  2 |    6          |       vimeo       |        hey, vimeo is better!       |
+--------------------+-------------------+------------------------------------+
  • 其中 youtube 和 vimeo 指的是模型 class Youtube(){…}class Vimeo(){…} youtube can 有很多 cmets,vimeo can 有很多 cmets,comment 属于 youtube 和 vimeo。
  • 如果我们以后想添加另一个具有许多 cmets 的模型,我们只需将其添加到 commentable_type 中,而无需在评论表中添加更多字段,例如 something_id
  • 如果我们想添加dailymotion,我们只需要在commentable_type中添加dailymotion,而不需要在评论表中添加字段dailymotion_id
  • commentable_id = 2 指的是 youtube 表中的 youtube_id = 2commentable_id = 6 指的是 vimeo_id = 6 中的 vimeo 表。

在 Laravel (PHP) 中,我们可以简单地这样做:

class Youtube extends Eloquent{

    public function comments()
    {
        return $this->morphMany('Comment', 'commentable');
    }
}

class Vimeo extends Eloquent{

    public function comments()
    {
        return $this->morphMany('Comment', 'commentable');
    }
}

class Comment extends Eloquent{

    public function commentable()
    {
        return $this->morphTo();
    }
}

我们可以像这样简单地访问它们:

$youtube = Youtube::find(1);
$youtube->comments;

$vimeo = Vimeo::find(1);
$vimo->comments;

我的问题是,如何在 ASP MVC 中推导出这个 ORM 的关系?以及我们如何才能轻松访问它们进行 CRUD 操作?

【问题讨论】:

  • 这是一个 ORM 问题,而不是 ASP.NET 问题。您使用的是哪个 ORM,因为每个 ORM 将有不同的方法来解决这个问题?
  • @BiffBaffBoff 也许我关于 ORM 的问题是错误的。如何在 ASP MVC 中做到这一点?如何在 ASP MVC 中定义这种关系?这样我们就可以轻松访问那些 cmets
  • 你目前有什么代码?我不知道您使用什么 ORM 来访问您的模型,所以我无法告诉您如何访问它..
  • @BiffBaffBoff 我先使用 EF 代码

标签: c# asp.net sql asp.net-mvc polymorphism


【解决方案1】:

我不确定这是否完全符合您的要求,但这样的内容就足够了:

public abstract class Video
{
    public int Id { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

public class Youtube : Video
{
    /* Other properties */
}

public class Vimeo : Video
{
    /* Other properties */
}

public class Comment
{
    public int Id { get; set; }
    public string CommentText { get; set; }

    public int VideoId { get; set; }

    public virtual Video Video { get; set; }
}

public class VideoContext : DbContext
{
    public DbSet<Youtube> Youtubes { get; set; }
    public DbSet<Vimeo> Vimeos { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

这会在查询时创建以下结构:

要访问它,您必须执行以下操作:

var context = new VideoContext();

var vimeos = context.Vimeos.Include(x => x.Comments).ToList();
var youtubes = context.Youtubes.Include(x => x.Comments).ToList();

【讨论】:

【解决方案2】:

你可以使用继承http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application

public class Video {
// Id etc.
public virtual IList<Comment> Comments {get; set;}
}

public class Youtube : Video{
// Id etc.
}

public class Vimeo : Video{
// Id etc.
}

public class Comment {
// comment props
 public virtual IList<Video> Videos {get; set;}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2022-01-13
    • 2014-02-18
    • 2021-04-23
    • 2018-03-13
    相关资源
    最近更新 更多