How to include Models Builder models in package
# help-with-umbraco
m
I'm working in a package that installs a few document types. It also includes some views that use view models mapped from the generated Umbraco models. To avoid building issues, I need to include the generated models in the package, but when I run a test site that uses the package it fails because it tries to generate the existing classes. There used to be a way of indicating, using decorators, that a class was already generated to indicate Models Builder to ignore it. The current documentation for v15 (https://docs.umbraco.com/umbraco-cms/reference/templating/modelsbuilder/understand-and-extend#extending-models) indicates that "Models Builder does not recognize custom partial classes during regeneration. If your custom class conflicts with the generated class (e.g., overriding a constructor), it will cause compilation errors." It also indicates that "For more complex customizations, use the full version of Models Builder." There's a link to the full version but is pretty old. Any idea if it's possible to include the models? Or a workaround?
d
Hey Mario. I was doing something similar a while ago and I ended up programatically creating the document types/data types on load instead and copy views on build or something like that. I'm not sure if there's a better way of doing it now. I hope there is but if not that could be an alternative route.
m
I think most packages avoid a reliance on modelsbuilder for the very reasons you highlighting.. and indeed revert to
Model.Value<T>(PackageConstantAlias)
as you never know what modelbuilder settting the end user might be employing... might even be
none
, custom namespace is also an issue.. only thought we had on that is have you package insist the modelsbuilder using directive is included in the _ViewImports.cshtml. (to allow for any namespace) Your suggestion of prebuilt models would be ok if possible... until the user extends your doctypes??
m
Yeah, I thought of using the Model.Value approach but I lose the strongly typed benefit so I was wondering if there's a better way. I guess there's not 🤷‍♂️
l
So far, I've just quit using generated models in my packages. It never got it to work correctly and I was much too afraid that it might cause issues if multiple packages would have the same models, or cause namespace conflicts. I just get the values by alias.Yes, you don't have the strong typed stuffs which is a shame, but with unit tests and control over the code, I never found it to be an issue.
2 Views