简体   繁体   English

有没有更优雅的方法来指定多个相同性质的一对多关系?

[英]Is there a more elegant way to specify multiple one to many relations of same nature?

I'm having trouble trying to create the following entities relations.我在尝试创建以下实体关系时遇到问题。 My code has the classes Shop and Person , and both have multiple Phones .我的代码有ShopPerson类,并且都有多个Phones My first implementation was something like this:我的第一个实现是这样的:

public class Person {
  public int Id;
  public string Name;
  public virtual ICollection<Phone> Phones;
}

public class Shop {
  public int Id;
  public string Name;
  public virtual ICollection<Phone> Phones;
}

public class Phone {
  public int Id;
  public string AreaCode;
  public string Number;

  public int? ShopId;
  public virtual Shop Shop;

  public int? PersonId;
  public virtual Person Person;
}

and the model builder like和模型构建器一样

modelBuilder.Entity<Person>()
  .HasMany(p => p.Phones)
  .WithOptional(p => p.Person)
  .HasForeignKey(p => p.PersonId);

modelBuilder.Entity<Shop>()
  .HasMany(p => p.Phones)
  .WithOptional(p => p.Shop)
  .HasForeignKey(p => p.ShopId);

My doubt here is: can I model it or change anything in my classes so when I'm handling the phones I don't have to keep checking if they are related to a Shop or a Person ?我的疑问是:我可以建模或更改我的课程中的任何内容,以便在我处理电话时我不必继续检查它们是否与ShopPerson

I would suggest the next scheme:我建议下一个方案:

public class Owner
{
  public int Id;
  public string Name;
  public virtual ICollection<Phone> Phones;
  public abstract SendMessage(Phone phone);
}

public class Person : Owner 
{
  public override SendMessage(Phone phone)
  {
   ...
  }

}
public class Shop : Owner 
{
  public override SendMessage(Phone phone)
  {
   ...
  }
}

public class Phone {
  public int Id;
  public string AreaCode;
  public string Number;

  public int? OwnerId;
  public virtual Owner Owner;
}

So you haven't two entities with duplicating fields and, as npinti mentioned, you delegate messaging function to the corresponding entity.因此,您没有两个具有重复字段的实体,并且正如 npinti 所提到的,您将消息传递功能委托给相应的实体。

To add a little bit more context to my comment, the way I would go about it (with the information currently at hand) would be as follows:为了在我的评论中添加更多上下文,我将采取的方式(使用当前手头的信息)如下:

  1. I would create an interface, IMessageSender , which has 1 method: SendMessage(Phone destination) .我会创建一个接口IMessageSender ,它有 1 个方法: SendMessage(Phone destination)
  2. Have the Shop and Person class implement that interface.ShopPerson类实现该接口。 In the implementation of the method you would then have your logic which, from the comments, should differ from Person and Shop .在该方法的实现中,您将拥有您的逻辑,根据注释,该逻辑应该与PersonShop不同。
  3. In the location where you want to send the message, simply iterate over the object and invoke SendMessage .在您想要发送消息的位置,只需遍历对象并调用SendMessage This will, in turn, delegate the logic of the construction of the message to the appropriate class.反过来,这会将消息构造的逻辑委托给适当的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有更优雅的方法来汇总多个属性? - Is there a more elegant way to sum multiple properties? EF Core Code First,相同object的2个多对一关系 - EF Core Code First, 2 Many to one relations of same object 显示null的更优雅方式? - A more elegant way to display null? 通过EF4.3中的代码优先方法模型在实体之间创建多个(一对多)关系 - Create more than one (one to many) relations between to entity by code first approach model in EF4.3 ef核心中相同类型的一对一和一对多关系 - both one-to-one and one-to-many relations of same type in ef core 除了为多个if语句之外,是否还有一种更为优雅的方法来为被选中的多个复选框编写逻辑? - Is there a more elegant way to write logic for multiple checkboxes being checked other than multiple if statements? 确定两个浮点数在C#中是否具有相同符号的更优雅方法? - More elegant way to determine whether two floats have same signs in C#? 使用多个SwaggerRespons的优雅方式 - Elegant way to use Multiple SwaggerResponses 是否存在使用Entity Framework将多个参数传递给SQL存储过程的更简洁或更简洁的方法? - Is there a more cleaner or elegant way to pass multiple parameters to SQL stored procedure using Entity Framework? 编写决策代码的更优雅的方法是评估具有不同优先级的多个输入? - A more elegant way to write decision making code which evaluates multiple inputs with different priorities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM