I have been working with Umbraco since a while and haven't faced such case. I hope I might get some reply / suggestion / advise from the community.
I am working on a News/Media Doctypes where project requirement say about few properties that are mandatory on some types of page and some might not be madatory.
Ex: News pages with Sports type should have reporter field mandatory but for Politics type reporter field as non-mandatory.
So I ended up creating 2 doc types to match the requirements. I also think that we can use same template(view) for both doctype.
I am trying to find a way or a approch to do the same in case of controllers coz I have to do extra content processing before rendered to UI.
My question:
Is there a way I can route all calls from SportsNewsDocType and PoliticsNewsDocType to one Controller called "NewsDocType"?
If you've created your doctypes using compositions, and are using modelsbuilder, you are able to just check if a document implements any of the compositions, by just checking of the document "is ISportsNews".
The compositions could be:
StandardArticle (normal doctype, with template)
SportsArticle (no template)
PoliticsArticle (no template)
The models build from the above will StandardArticle (class), ISportsArticle (interface) and IPoliticsArticle (interface)
In your controller you can then do (not tested):
if(content is StandardArticle article) {
if(article is IPoliticsArticle as politics) return view("polistics", politics)
if(article is ISportsArticle as sports) return view("sports", sports)
return view("article", article)
}
I thank you for your reply and was a quick one. :)
I am not using modelbuilder and we have created our own model (VM and Dtos) to move data from custom service classes to Controllers and later to views.
So what would be the other aproach I can go with coz we are almost in mid-way project and cannot switch to modelbuilder.
Also please advise how can I implement Interfaces where I can find out which type of Article is called from in my only controller like StandardArticleController
The structure of the content/doctypes would be the same.
I guess you are doing some mapping yourself in your controllers / services. They should just be able to handle the fields. If you don't have modelsbuilder, you will not be able to use the benefits it gives
Another option is to abstract the "extra content processing" to a shared method. So you can still have two controllers, with different models, but the processing is performed by a new method.
You'd need to either make your models inherit from a base class or implement an interface and then you could pass either of them to this processing method before you returned them to the same view.
This would also ensure your controllers don't get too bloated, as generally a controller shouldn't perform much logic.
Thank you very much for replies and suggestions. As the business requirements were very specific about the properties and their mandatory behaviors we had to go with creating separate document-type with multiple controllers but our templates remained the same. As advised all the processing logic is moved to single place instead of bloating controllers.
Multiple DocType using One Controller
Hello Community,
I have been working with Umbraco since a while and haven't faced such case. I hope I might get some reply / suggestion / advise from the community.
I am working on a News/Media Doctypes where project requirement say about few properties that are mandatory on some types of page and some might not be madatory.
Ex: News pages with Sports type should have reporter field mandatory but for Politics type reporter field as non-mandatory.
So I ended up creating 2 doc types to match the requirements. I also think that we can use same template(view) for both doctype.
I am trying to find a way or a approch to do the same in case of controllers coz I have to do extra content processing before rendered to UI.
My question: Is there a way I can route all calls from SportsNewsDocType and PoliticsNewsDocType to one Controller called "NewsDocType"?
Please suggest..
Thank you in advance!
Regards,
Ranjit J. Vaity
Hi,
You can override the default controller, with your own, and then add the logic you need - have a look at this https://our.umbraco.org/documentation/Implementation/Default-Routing/Controller-Selection/
If you've created your doctypes using compositions, and are using modelsbuilder, you are able to just check if a document implements any of the compositions, by just checking of the document "is ISportsNews".
The compositions could be: StandardArticle (normal doctype, with template) SportsArticle (no template) PoliticsArticle (no template)
The models build from the above will StandardArticle (class), ISportsArticle (interface) and IPoliticsArticle (interface)
In your controller you can then do (not tested):
(maybe some c# 7+ features in the above code)
Hello Søren,
I thank you for your reply and was a quick one. :)
I am not using modelbuilder and we have created our own model (VM and Dtos) to move data from custom service classes to Controllers and later to views.
So what would be the other aproach I can go with coz we are almost in mid-way project and cannot switch to modelbuilder.
Also please advise how can I implement Interfaces where I can find out which type of Article is called from in my only controller like StandardArticleController
Thank you! :)
Regards, Ranjit J. Vaity
The structure of the content/doctypes would be the same.
I guess you are doing some mapping yourself in your controllers / services. They should just be able to handle the fields. If you don't have modelsbuilder, you will not be able to use the benefits it gives
Another option is to abstract the "extra content processing" to a shared method. So you can still have two controllers, with different models, but the processing is performed by a new method.
You'd need to either make your models inherit from a base class or implement an interface and then you could pass either of them to this processing method before you returned them to the same view.
This would also ensure your controllers don't get too bloated, as generally a controller shouldn't perform much logic.
Hello,
Thank you very much for replies and suggestions. As the business requirements were very specific about the properties and their mandatory behaviors we had to go with creating separate document-type with multiple controllers but our templates remained the same. As advised all the processing logic is moved to single place instead of bloating controllers.
I thank you all again! :)
Regards, Ranjit J. Vaity
is working on a reply...