【问题标题】:Is it possible that two different classes have a property -a collection of classes and each object knows which of the first two classes it belongs to?两个不同的类是否有可能具有一个属性——一个类的集合,并且每个对象都知道它属于前两个类中的哪一个?
【发布时间】:2021-01-14 17:36:18
【问题描述】:

我还有 3 节课

class Review
{
   public int Id{get; set;}
   public string Text{get; set;}
   public ICollection<Comment> Comments{get; set;}
//some additional properties
}
class User
{
   public int Id{get; set;}
   public string Name{get; set;}
   public string Login{get; set;}
   public ICollection<Comment> Comments{get; set;}
//some additional properties
}
class Comment
{
   public int Id{get; set}
   public string Comment{get; set;}
   public ??? Target{get; set;}
}

在这种情况下,目标是注释所附加的内容。对于某些事情,我需要确切地知道目标是谁 - 特定用户或特定评论。 并且由于参数的强烈区分,从接口继承是不合适的。而且我需要使用数据库,所以对象类型不适用于那种情况(据我所知)。

【问题讨论】:

  • 我想到的唯一实际工作是使用像“U123”这样的字符串,其中第一个字符 - 我使用什么类型,所以所有类都有 ID,123 - id。但在我看来,有更好的方法来解决这个问题。如果找不到其他答案,我会使用这种方法。

标签: c# ef-code-first


【解决方案1】:

从提供的代码中,您可以使用objectdynamic 作为Target

但您可能更喜欢使用ReviewUser 的基类,例如BaseObjectWithComments,以便能够编写BaseObjectWithComments Target,这样会好一些。

所以我们可以将一些成员移动到这个根类。

我们还可以为Comment添加一个构造函数来传递所有者。

public abstract class BaseObjectWithIdAndComments
{
  public int ID { get; set; }
  public ICollection<Comment> Comments { get; set; }
}

public class Review : BaseObjectWithIdAndComments
{
  public string Text { get; set; }
}

public class User : BaseObjectWithIdAndComments
{
  public string Name { get; set; }
  public string Login { get; set; }
}

public class Comment
{
  public int ID { get; set; }
  public string Text { get; set; }
  public BaseObjectWithIdAndComments Target { get; set; }
  public Comment(BaseObjectWithIdAndComments owner, int id, string comment)
  {
    Target = owner;
    ID = id;
    Text = comment;
  }
}

测试

var review = new Review();
var user = new User();

review.Comments = new List<Comment>();
user.Comments = new List<Comment>();

review.Comments.Add(new Comment(review, 1, "review comment 1"));
review.Comments.Add(new Comment(review, 2, "review comment 2"));

user.Comments.Add(new Comment(user, 1, "user comment 1"));
user.Comments.Add(new Comment(user, 2, "user comment 2"));

Console.WriteLine("Owner of review comment #1: " + review.Comments.ElementAt(0).Target.GetType().Name);
Console.WriteLine("Owner of user comment #2: " + user.Comments.ElementAt(1).Target.GetType().Name);

输出

Owner of review comment #1: Review
Owner of user comment #2: User

关于使用数据库,您需要更改设计。

例如表格的字段可能是这样的:

CommentID
ReviewID
UserID
Text

但这并不干净,因此您可能更喜欢有 2 个表以更常规:UserCommentsReviewComments,因此这些表中的每一个都有一个 OwnerID 指向 UsersReviews .

因此,您可以在设置所有者引用的同时加载先前类的实例中的数据。

也许您可以考虑使用 ADO.NET 类型的数据集,它可以为您完成所有工作,同时能够使用 Visual Studio RAD 设计器

Typed DataSets (MS Docs)

How to: Create and configure datasets in Visual Studio (MS Docs)

Beginning C# 2005 Databases (Wrox)

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2023-03-14
    • 2023-04-04
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多