modelsbuilder / ModelsBuilder.Original

The Community Models Builder for Umbraco
MIT License
114 stars 49 forks source link

ModelBaseClass (for individual model) #133

Closed quiznaut closed 7 years ago

quiznaut commented 7 years ago

It would be helpful to be able to specify base class for each individual model, in addition to being able to specify ModelsBaseClass for all models.

My situation is this: following some advice I saw in a blog that "compositions are the way to go" and that it's possible that document type inheritance may be deprecated in a future Umbraco version, I set up no inheritance among my document types.

My thinking has evolved, and now I'd like to add some common functionality to just some of those document types. Perfect use for inheritance, but I don't have any.

Alternatives I considered, all of which have problems:

It seems to me that the best solution for a problem like this, and indeed a good way to get more value out of compositions in general, is to allow specification of base class on a model-by-model basis.

zpqrtbnk commented 7 years ago

Hey,

There is no way to add inheritance after-the-fact in the backoffice. In can be done by messing with the database, but you really have to know what you're doing. And besides, maybe this inheritance does not make sense from a "content" point of view if it's essentially a technical inheritance used to expose some common functionnality.

Adding the base class to all document types does indeed smell bad.

Which leaves us with changing the base class for some models. The next version should support specifying a base class per model, but it's been in progress forever and is not going to ship soon. That being said, I think that you should be able to specify a base class in your partial:

public partial class MyModel : MyBaseClass
{ ... }

And the ModelsBuilder should detect it and not set the base class in the generated code. Of course, you do need to re-generate the code, so that the ModelsBuilder can do it. But I'm pretty sure it should work. Can you try again?

quiznaut commented 7 years ago

Thank you, Stephan, that worked. It was important to:

  1. Include the file with the partial class (that inherits from the custom base class) in the same folder as the .generated.cs files. In my case, that's the folder I specified via the .ModelsDirectory key.
  2. Generate models before attempting to compile.

I think I did the first bit back in February when I originally posted this question. But it's likely I immediately tried to compile, and did not re-generate the models. That's what tripped me up.

Sorry it's taken me so long to get back to you.

quiznaut commented 7 years ago

I've finally picked this up again and found that, although I can set the base class after-the-fact, and ModelsBuilder picks it up, if my base class needs to implement any of compositions that the derived classes implement, ModelsBuilder doesn't take that into account. It generates all of the composition properties in the derived class without regard to the base class, effectively hiding the base-class properties.

As a side note, for my current project my workaround was to use document type inheritance. Unfortunately, it's not compatible with composition, as documented here: U4-7596