.Net templating engine with object graph output, not string
I would like to know if there's any templating engine that is used for object graph creation? There are many to create HTML or other string resulting content. But I would like a templating engine that spits out objects. So to speak a preprocessor.
I have an application that needs some sort of predefined object graphs that's used to render a specific page widget. For instance I have a toolbar on my pages but every page has a different toolbar. It may look very similar. but for instance button Add will point to a different URL on one page than on the other.
Example object graph (part of it):
IList<ToolbarItem> toolbar = new List<ToolbarItem> {
new ToolbarButton {
Text = PageResources.NewText,
Url = Url.Action("Add", "Items"),
Icon = ToolbarIconType.New
},
new ToolbarSeparator(),
new ToolbarDropDown {
Text = PageResources.DropDownText,
Icon = ToolbarIconType.Filter,
Items = new List<ToolbarDropDownItem> {
new ToolbarDropDownItem {
Text = PageResources.FirstItem,
Url = Url.Action("Whatever", "Filter")
},
new ToolbarDropDownItem {
Text = PageResources.SecondItem,
Url = Url.Action("BlahBlah", "Filter"),
Icon = ToolbarIconType.Search
},
...
}
},
...
};
This static definition (with dynamic resources and dynamic URL helper calls) may become quite long and complex. Think of a ribbon-like toolbar similar to Word. Very complex and long.
I would like to have these object generation templates stored in the database and each page would be able to read their own, templating engine would then generate it and use particular resource type + key to put localised data in. The same thing would be with actions' URLs...
Something similar is done at object deserialization (XML or JSON). but built-in (de)serialization technologies don't support variable replacements or resource links or similar. Best thing would be to support resource l开发者_如何学Pythoninks and lambda expressions.
To sum it up: is there any .net object graph templating engine/preprocessor that would provide what I need?
That's a very interesting question.
I think that one option would be to generate XAML and then use XamlReader.Load
(see MSDN) to load the object tree (or graph) described by the XAML. Note that XAML can be used to construct any .NET objects that follow some basic requirements (e.g. have public get/set properties and implement some interface if they should be useable as collections).
Also, XAML is a XML document, so it should be possible to use stnadard text templating engines that can produce XML files to generate your XAML documents.
JSON serialization
I decided to use Json.Net library mostly because writing JSON strings is very human readable or should I rather say web developer readable and is easy to maintain. It's very similar to normal Javascript JSON but with additional syntactic sugars (referential preservation, type preservation etc.)
Json.NET library is a very good upgrade from provided JavaScriptSerializer
or the DataContractSerializer
classes.
精彩评论