Maintaining a two way relationship between classes

前端 未结 6 682
萌比男神i
萌比男神i 2021-02-02 00:34

It is pretty common, especially in applications with an ORM, to have a two way mapping between classes. Like this:

public class Product
{
    private List

        
6条回答
  •  轮回少年
    2021-02-02 01:40

    This is not the correct way to model this problem.

    A Product ought to have a Price, a Price ought not to have a Product:

    public class Product
    {
        public Price CurrentPrice {get; private set; }
        public IList HistoricPrices { get; private set;}
    }
    
    public class Price { }
    

    In your particular setup, what does it mean to for a Price to have a Product? In the class I created above you would be able to handle all pricing within the Product class itself.

提交回复
热议问题