I was chatting with Lee on Friday about uSiteBuilder and I explained that I thought it was great.
The one issue I had not overcome until now is strong typed razor views which adds intelesense to VS :))
Anyway this is questions about how to do this as I have a solution but wanted to check if this was the best way to do it.
First I tried to cast the @Model object to the type I wanted to use:
var basicPageData = (BasicPage)@Model;
This returned an error so I figured I would create a Cast method in the BasicPage DocumentType class to convert the @Model to a type
public static BasicPage Cast(DynamicNode node) { BasicPage basicPage = new BasicPage(); basicPage.PageTitle = ((dynamic)node).PageTitle; basicPage.SeoDescription = ((dynamic)node).SeoDescription; return basicPage; }
This works great in Razor and I get intelesense and should alos be able to do lambda queries on the object with no magic strings :)
var basicPageData = BasicPage.Cast(@Model);
Anyone got any comments on this? Could I do it better convert is easier? Have I coded something thats already built in to uSiteBuilder or .net that I dont know about?
This looks nice a tidy now and if you have all the properties above or below in the code you know exactly what properties you're looking for.
I was wondering about writing a T4 template to create this everytime the page was saved so as new properties are added they are automatically added to the Constructor. Anyone done anything like this before?
Hey guys, we found a nice way to support Razor in uSiteBuilder and offer stongly typed views with uSiteBuilder & Razor! Soon we'll post more details. I'm sure you'll like it and I hope that some of you guys will be able to do test before we officially release it!
OK so yesterday after my last post I wondered how difficult it would be to write a T4 template to do the coding for me to create the constructors. Here is the output, let me know what you think.
Yes you can use @Model.Id as well but I mentioned @Node because that's not dynamic property - you can access it as well and @Node has intellisense. Your solution with custom constructor works but you have to write constructor for each of your document types (and maintain it in the future). You will also lose possibility for using custom data types in properties (e.g. you can define a property in your document type as List<RelatedLink> MyLinks etc...).
uSiteBuilder and strongly-typed views with razor
I was chatting with Lee on Friday about uSiteBuilder and I explained that I thought it was great.
The one issue I had not overcome until now is strong typed razor views which adds intelesense to VS :))
Anyway this is questions about how to do this as I have a solution but wanted to check if this was the best way to do it.
First I tried to cast the @Model object to the type I wanted to use:
This returned an error so I figured I would create a Cast method in the BasicPage DocumentType class to convert the @Model to a type
This works great in Razor and I get intelesense and should alos be able to do lambda queries on the object with no magic strings :)
Anyone got any comments on this? Could I do it better convert is easier? Have I coded something thats already built in to uSiteBuilder or .net that I dont know about?
I look forward to your comments
Jon
Hi Jon,
You can also create a constructor on your documenttype class that has a dynamic node parameter.
Dave
Hey Jon,
Good to see you Friday :)
I don't have the answer for you but very interesting that it would be possible to have intellisense on doc type properties....
Rich
Hi Dave
Yeah converting it to a constructor makse sense I've just changes it and it works great.
So thats simplified it I did however have to add an empty public constructor too though as I got build errors without it.
It also simplifies the code in the razor to
Which is nice.
Rich, was great to see you again on Friday. Really hope this is helping and look forward to you input ;)
Cheers
Jon
I've made another change. The parameter node can be set to dynamic to save all the casting:
This looks nice a tidy now and if you have all the properties above or below in the code you know exactly what properties you're looking for.
I was wondering about writing a T4 template to create this everytime the page was saved so as new properties are added they are automatically added to the Constructor. Anyone done anything like this before?
Maybe through reflection would be a solution. Have to try that later on.
Interesting, I was just thinking of using regular expressions or something like that on the text file. I'm not sure which would be quicker.
Hey guys, we found a nice way to support Razor in uSiteBuilder and offer stongly typed views with uSiteBuilder & Razor! Soon we'll post more details. I'm sure you'll like it and I hope that some of you guys will be able to do test before we officially release it!
OK so yesterday after my last post I wondered how difficult it would be to write a T4 template to do the coding for me to create the constructors. Here is the output, let me know what you think.
I didn't see your original question before but you can consider the following (I assume that your BasePage is an uSiteBuilder document type):
BasePage basePage = (BasePage)ContentHelper.GetByNodeId(@Node.Id);
(you can write this directly in your razor script).
You can check out also other methods in ContentHelper.
So you can always get instance of an uSiteBuilder document type class if you know node id (of course, with all properties set with actual content).
Hi Vladan
I'm just using the @Model object in Razor at the moment but I guess I could do as you say and do
At the moment i have
It works well and is much clearer for a developer to know what they are working with as there is no need for casting.
Yes you can use @Model.Id as well but I mentioned @Node because that's not dynamic property - you can access it as well and @Node has intellisense. Your solution with custom constructor works but you have to write constructor for each of your document types (and maintain it in the future). You will also lose possibility for using custom data types in properties (e.g. you can define a property in your document type as List<RelatedLink> MyLinks etc...).
BTW, you can also use the following:
I may look in to implementing the generic lists in to the T4 template as I'm aware it's quite harsh limiting everything to a string.
can we create the strongly typed views as those in mvc
If no then can anyone tell the steps to create the strongly typed views in umbraco 5
is working on a reply...