Lead Product Evangelist @Kentico, Founding partner @craftbrewingbiz. love to learn / teach web dev & software engineering, collecting vinyl records, mowing my lawn, craft 🍺
I could put all 4 of the types referenced above in separate files, but they are all directly related (referenced in the ProductDetailsViewModel public type definition), so instead I would create a file ProductDetailsViewModel.cs and define all those related classes there.
If it then turns out that the ProductPromotionViewModel is used in another View, I might extract that out to a higher namespace/folder since it would no longer be completely 'owned' by ProductDetailsViewModel.
On the other hand, if all these types were only used by the ProductDetailsController, I would be inclined to move all their definitions into the ProductDetailsController.cs file.
As an example for file organization in folders/namespaces, if I have an ImageViewModel that I use across all features / pages on a site, I'll store that in:
Thanks for the detailed reply @seangwright
. It "feels" like it breaks so many rules that are in my head (one file, one thing). But it does keep things simpler and still follows (one file, one "concept"). They are all heavily related objects, and typically if one thing changes, then a few of them may change. Which... still follows the rule. It does feel cleaner (not 10 files all over the place, when you make "one change" and have to touch all of them). And, at the end of the day, as soon as something else is using it, you can easily refactor/extract. I'll have to give it a try. Thanks agian.
Lead Product Evangelist @Kentico, Founding partner @craftbrewingbiz. love to learn / teach web dev & software engineering, collecting vinyl records, mowing my lawn, craft 🍺
Good article, interesting ideas. It would be good to see how you name these files and where they live in the folder structure.
Thanks for reading Mark!
I typically name the file after the 'primary' type.
If I do include multiple types in a single file, it's because they are all directly related.
Example:
I could put all 4 of the types referenced above in separate files, but they are all directly related (referenced in the
ProductDetailsViewModel
public type definition), so instead I would create a fileProductDetailsViewModel.cs
and define all those related classes there.If it then turns out that the
ProductPromotionViewModel
is used in another View, I might extract that out to a higher namespace/folder since it would no longer be completely 'owned' byProductDetailsViewModel
.On the other hand, if all these types were only used by the
ProductDetailsController
, I would be inclined to move all their definitions into theProductDetailsController.cs
file.As an example for file organization in folders/namespaces, if I have an
ImageViewModel
that I use across all features / pages on a site, I'll store that in:src\delivery\APPCODE.Delivery.Web\Features\Images\ImageViewModel.cs
and that might be the only class in that file.
So, I'm typically combining multiple types in a single file when there is a strong relationship between them and one of the types 'owns' the others.
Thanks for the detailed reply @seangwright . It "feels" like it breaks so many rules that are in my head (one file, one thing). But it does keep things simpler and still follows (one file, one "concept"). They are all heavily related objects, and typically if one thing changes, then a few of them may change. Which... still follows the rule. It does feel cleaner (not 10 files all over the place, when you make "one change" and have to touch all of them). And, at the end of the day, as soon as something else is using it, you can easily refactor/extract. I'll have to give it a try. Thanks agian.
I think you made a perfect TLDR of my post!