6

Every time I create a new data model with EF, I have to enter the TT file and substitute each occurrence of ICollection and the first occurrence of HashSet for ObservableCollection as well as the namespace Generic for ObjectModel.

The process is described on the internet (the bullet list right above "Lazy Loading" section) and seems to be as a hacky work-around for a shortcoming of a new technology, which with time should have been remedied and not needed.

Is it possible to automatically create observable collection or is the approach described the most efficient and generally applied?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • I've had to edit TT files on multiple occasions and even though it may *seem* hacky, I've found it to be a very efficient and surprisingly solid approach to custom generation. So IMHO, edit the TT file (you only need to do it once) – Niels Filter Aug 05 '15 at 13:02
  • @NielsFilter: Once in _every_ project. That's the problem, especially in an enterprise, where dozens of developers creating (in side project's or in utilities) data models with EF. DRY. – g.pickardou Aug 05 '15 at 13:20
  • Don't repeat yourself. Sorry for the missing link in the _comment_, there is a link in my answer. And also sorry for the ambiguity, that comment was adressed to Niels (now edited), not to your original question. – g.pickardou Aug 05 '15 at 13:24
  • @g.pickardou (Hint: I'm pulling your leg, here.) – Konrad Viltersten Aug 05 '15 at 13:24
  • There is some mix in the title on your model class's interface (a navigation property type: ICollection), and its internal implementation (ObservableCollection). Note: The property still can declared ICollection because both HashSet both ObservableCollection implement ICollection. You are asking about how to change the implementation. Btw as I see lazy loading (as a concept) has nothing to do with this implementation change. – g.pickardou Aug 05 '15 at 13:27

1 Answers1

3

I think customizing a .TT template for your need is an acceptable (or more: best) practice. The only question, Do this customizing process repeatedly/manually or create your own .TT and install it as Visual Studio Item Template. My vote goes to this second option, which is a little application of DRY.

However you need to prepare yourself to a drawback: When original .TT item templates will change (new version, etc) then you should apply those changes on your custom templates too. The easiest way to do this: diff, then choose the most efficient way: Replicate version changes on your custom template or reapply your customization changes on the new version.

g.pickardou
  • 32,346
  • 36
  • 123
  • 268