I have given my self the task of creating a true MVC website in our lovely Umbraco 7. But I keep hitting the wall with some missing best practice and documentation.
What are the best way of creating a strongly typed MVC setup and when to do what?
But these infomations is a mix between Umbraco 6 and 7 which is a bit confusing.
My frustration is focused on:
- Should the model have a complete representation of the related document type (no use of GetPropertyValue(propertyName) in the views), or should i only implement new properties and logic?
- If I have a complete representation of the document type where (controller or model) and how do I best populate the model with the data from the document.
I found that my views were starting to contain far too much logic than i'd like. But after reading these posts and reviewing the source code, it really helped me in achieving a more traditional MVC structure.
The models should reflect the structure of your document types in terms of both properties and inheritance. For example, if you have child document types, the model for this child doc type should inherit from the model that represents the parent document type.
I think the controllers are the best place to populate the model before passing it to the view. You can populate the properties of the model by obtaining the property values in the same way you normally would (i.e. CurrentPage.GetPropertyValue("pageTitle"))
Unfortunately, I do not know the username/password for that demo, it's been a while since I last looked at this and if it is not provided along with the source code, then I can't help with that unfortunately.
Also, the demo isn't actually mine, I only used it as a reference for developing my own application.
However, I guess you may be able to change the password for the main admin user using the Umbraco User Service with code like this:
var userService = global::Umbraco.Core.ApplicationContext.Current.Services.UserService;
var user = userService.GetByUsername("admin"); // Change admin to whatever the username is.
userService.SavePassword(user, "Password1!");
Nice articles on using umbraco for pure content display. But what about data input (forms)?
Do you have any thoughts on this? Or perhaps you want to elaborate a bit on my thoughts?
In order to have forms you need a SurfaceController to receive the posted data with typed content (view model). In order to populate a form with typed content you need a RenderMvcController. So in order to have a single contact form (name, email, message) you need 2 controllers (RenderMvc + Surface), 1 view model (inherited from RenderModel), 1 document type (in umbraco) and 1 template (cshtml, also referenced i umbraco).
Is this the right way to go about this?
I would love to see a some code or hear some comments on this. Because it seems that we have one controller too many (compared to asp.net mvc)?
I have seen a few examples of using a surface controller to have a partial. Then you use the @Html.Partial("view", new modeltype model) style of code to generate the form.
Wow it's been a while since I've done any development on Umbraco, but the time has come for me to play with my favourite CMS again.
I'd like to start working with MVC, and wondering if the example at https://umbracomvcdemo.codeplex.com/ is still considered "Best Practice", or if there's a better example around that I can use to learn from?
I think it's not the best practice, it's one of the possible ways.
I like to use ZBU Model builder for mapping data from Umbraco to strongly typed models, in this case you don't need controllers with population models from Umbraco like this :
public ActionResult FrontPage()
{
var model = new FrontPageModel();
model.BodyText = CurrentPage.GetPropertyValue<IHtmlString>("bodyText");
return View(model);
}
Here no logic, just code, I don't know why we need to write it ))
Yeah I actually built my own framework, and shifted all the model population work to the model itself... seems to make more sennse then doing it in the controller.
I also added some extra levels of inheritance, so i now have a pretty nice framework that i can use to kickstart any project I do... and then for more specific project requirements i'll just inherit from my base models and controllers :)
@Jamie no it's not like Ditto (which looks pretty great by the way - i might integrate that into my framework).. it's more around the controllers and views. Together they'd be pretty awesome I think, thanks for showing me that one :)
So on further investigation and experimentation, I've decided it's much nicer to move the model population away from the model, but also keep it out of the controller. Bo Mortensen does this in a "Repository" class, which is basically the interface between the Model and the Data Repository. I really like this approach as it keeps all the data-specific code in just one class (the Repository), and decouples the model from the data.
And if you're super keen, you can use Dependency Injection to decouple the controller from Umbraco too. Probably overkill for your average website, but definitely worthwhile for bigger projects where access to multiple systems may be required (and especially if switching between repositories is a requirement).
Best practice for MVC in Umbraco 7
Hello dear Umbracians
I have given my self the task of creating a true MVC website in our lovely Umbraco 7. But I keep hitting the wall with some missing best practice and documentation.
What are the best way of creating a strongly typed MVC setup and when to do what?
I have read the following articles http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/, http://pwee167.wordpress.com/2014/02/16/umbraco-7-what-ive-learned-so-far-custom-controllers-and-models/ and studied the Hybrid Framework.
But these infomations is a mix between Umbraco 6 and 7 which is a bit confusing.
My frustration is focused on:
- Should the model have a complete representation of the related document type (no use of GetPropertyValue(propertyName) in the views), or should i only implement new properties and logic?
- If I have a complete representation of the document type where (controller or model) and how do I best populate the model with the data from the document.
Thank you for your time
Jacob
Hello,
Check out the following two blog posts:
And also this Demo code at https://umbracomvcdemo.codeplex.com/
I found that my views were starting to contain far too much logic than i'd like. But after reading these posts and reviewing the source code, it really helped me in achieving a more traditional MVC structure.
The models should reflect the structure of your document types in terms of both properties and inheritance. For example, if you have child document types, the model for this child doc type should inherit from the model that represents the parent document type.
I think the controllers are the best place to populate the model before passing it to the view. You can populate the properties of the model by obtaining the property values in the same way you normally would (i.e. CurrentPage.GetPropertyValue("pageTitle"))
Hope this helps.
Chris
Hi Xrisdoc,
Could you share username and password of umbraco back office help me I can't to access to umbraco backoffice.
Thanks Tuan Nguyen.
Hi Tuan,
Unfortunately, I do not know the username/password for that demo, it's been a while since I last looked at this and if it is not provided along with the source code, then I can't help with that unfortunately.
Also, the demo isn't actually mine, I only used it as a reference for developing my own application.
However, I guess you may be able to change the password for the main admin user using the Umbraco User Service with code like this:
Hope this helps.
Thanks, Chris
Thanks Xrisdoc about your post
That is what i need, i have the another way as link below
http://jondjones.com/reset-your-umbraco-7-password-manually-via-the-database/
Have a nice day ^^.
Thanks Tuan Nguyen.
Thank you very much.
This seemed to connect the missing pieces in my head :-)
I am now happily on the MVC road.
Hi
Nice articles on using umbraco for pure content display. But what about data input (forms)?
Do you have any thoughts on this? Or perhaps you want to elaborate a bit on my thoughts?
In order to have forms you need a SurfaceController to receive the posted data with typed content (view model).
In order to populate a form with typed content you need a RenderMvcController.
So in order to have a single contact form (name, email, message) you need 2 controllers (RenderMvc + Surface), 1 view model (inherited from RenderModel), 1 document type (in umbraco) and 1 template (cshtml, also referenced i umbraco).
Is this the right way to go about this?
I would love to see a some code or hear some comments on this. Because it seems that we have one controller too many (compared to asp.net mvc)?
Thank you!
Svenn
I have seen a few examples of using a surface controller to have a partial. Then you use the
@Html.Partial("view", new modeltype model)
style of code to generate the form.Hey all,
Wow it's been a while since I've done any development on Umbraco, but the time has come for me to play with my favourite CMS again.
I'd like to start working with MVC, and wondering if the example at https://umbracomvcdemo.codeplex.com/ is still considered "Best Practice", or if there's a better example around that I can use to learn from?
Cheers Greg
Hi Greg,
I think it's not the best practice, it's one of the possible ways. I like to use ZBU Model builder for mapping data from Umbraco to strongly typed models, in this case you don't need controllers with population models from Umbraco like this :
Here no logic, just code, I don't know why we need to write it ))
Thanks, Alex
Thanks Alex,
Yeah I actually built my own framework, and shifted all the model population work to the model itself... seems to make more sennse then doing it in the controller.
I also added some extra levels of inheritance, so i now have a pretty nice framework that i can use to kickstart any project I do... and then for more specific project requirements i'll just inherit from my base models and controllers :)
MVC rocks!
Hey Greg,
Sounds pretty good. Is it similar to Ditto?
Thanks,
Jamie
Hi Greg,
Interesting to look at your framework )) It would be grat to see it at github or so something like that )
Thanks
Sure I'll put it on github when it's ready - it's still a WIP. Watch this space :)
@Jamie no it's not like Ditto (which looks pretty great by the way - i might integrate that into my framework).. it's more around the controllers and views. Together they'd be pretty awesome I think, thanks for showing me that one :)
So on further investigation and experimentation, I've decided it's much nicer to move the model population away from the model, but also keep it out of the controller. Bo Mortensen does this in a "Repository" class, which is basically the interface between the Model and the Data Repository. I really like this approach as it keeps all the data-specific code in just one class (the Repository), and decouples the model from the data.
And if you're super keen, you can use Dependency Injection to decouple the controller from Umbraco too. Probably overkill for your average website, but definitely worthwhile for bigger projects where access to multiple systems may be required (and especially if switching between repositories is a requirement).
I don't think I could add much to this except post the link: http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/
Definitely the nicest Umbraco MVC implementation I've seen so far :)
I would definite recommend DI and GlassMapper for umbraco :)
Ah yes Glassmapper, i forgot about that. I've worked with it on a Sitecore project before but not with Umbraco.
Any thoughts on Glassmapper vs DItto?
GlassMapper hands down. Again not really used it outside of Sitecore and not having TDS is gonna suck but it's really good :D
is working on a reply...