Is it appropriate to repeat data in models to satisfy using law of demeter in collections?

前端 未结 3 327
囚心锁ツ
囚心锁ツ 2021-01-14 21:06

This is a contrived example, say I want to list the population of a country that a person has a friend in, here are two setups below. Would it be best to repeat data in the

相关标签:
3条回答
  • 2021-01-14 21:43

    In my opinion you should avoid repition of data where you can. However, you can create aggregate objects which allow you to combine data. So you can keep your core entities clean but then have additional supporting entities which agreggate objects.

    If you use the example of a View in SQL, you can retrieve a result which is the combination of many entities. This result could be the agreggate entity and is a perfectly legitimate means of 'repeating' data.

    0 讨论(0)
  • 2021-01-14 21:50

    Ahh, the "occasionally useful suggestion of Demeter". (Martin Fowler.)

    I think DIE/DRY and normalization are more fundamental principles, but it's ultimately going to be a contest between conflicting guidelines that you will need to apply common sense to.

    The "law" applied to object classes in one specific project and does have obvious value as a class hierarchy design model.

    But there is controversy over Demeter's application specifically with respect to Rails views. By definition they are a report, and so it is questionable whether the Suggestion of Demeter is applicable.

    0 讨论(0)
  • 2021-01-14 22:05

    This does not seem to be an issue of law of demeter as much as it is an issue of Database design and Data Integrity. The first option should be ruled out because it creates a database that definitely violates Third Normal-Form (3NF):

    In your first example, if a Country HM cities, what happens if say, you update that city to belong to a different region that no longer belongs to the Country? -> Bam! Data Integrity Gone! Of course, it is unlikely that a city will move to a different country but like you said, this is a contrived example and i'm talking about the general case

    You should google Database Normalization and Third Normal Form for more.

    Also, in this case, you're violating 3NF only because you think you can 'improve performance' by doing so. This is a case of pre-optimization and a bad practice. While in some cases de-normalization is a managed risk, Here be dragons, and if you're just starting an app in rails, this is definitely not the case. Let your DB worry about fetching the data quickly. you can help it by providing good indexes.

    Also, I think that what you're looking for is a way to create a nested has many through relationship. You want to it to be the case that:

    a Country HM Counties THROUGH Regions

    and that,

    a Country HM Cities THROUGH Counties

    this will be standard in 3.1 if you're on 3.0, then you can use the

    https://github.com/ianwhite/nested_has_many_through

    gem, which i am currently using, and am quite happy with.

    0 讨论(0)
提交回复
热议问题