Clay Usage

Oct 7, 2010 at 1:44 PM

Hi,

I think I am getting confused abou the usage of clay in the dev branch of Orchard.

I thought clay would use dynamic trickery to create a ViewModel from a domain model much like automapper only without having to specify the mapping and not specifying the ViewModel.

I wrote the following test to check this assumption:

        [Test]
        public void Should_be_different_type_from_domain()
        {
            var shape = _shapeHelperFactory.CreateHelper();

            var domain = StructureHelper.GetBusinessUnitList();

            var list = shape.List();

            list.AddRange(domain);

            Assert.That(list.Items[0].GetType().FullName, Is.Not.EqualTo(domain[0].GetType().FullName));
        }



obviously the assertion failed or I would not be writing this post :-).



Is it that clay combines ViewModels or could somebody explain the use case for clay?

Cheers

Paul






Coordinator
Oct 7, 2010 at 5:03 PM

Good question. the answer is that it lets you choose and combine dynamic shapes and static view models into a single object graph. We have a lot of pre-clay code that was using strongly-typed view models (sometimes for good reason by the way) and we want to be able to continue doing that. There is a familiarity factor there for existing MVC developers that is neat. And again sometimes it's just what you want. We also alow for completely dynamic shapes that can be enriched on the fly and themed in very flexible ways. Does that help?

Oct 7, 2010 at 8:39 PM

Hi,

Thank you for the answer, is there any example in the orchard source that shows a complex viewmodel composition?  This might clarify what is possible.

I could only find a couple of examples in AdminController and BlogController that pretty much mirror what I did in the test above and I am not sure why you would use clay as really a wrapper around the model.

I would love to see an example of what is trying to be achieved.

The Shape object model and surrounding factories is quite complex and interesting code, I am still trying to properly understand it.

Do you think you will be adding to the 2 blog posts you have done on clay?  I think it very interesting to see what is properly possible with the new dynamic features of C# in the same way as lamdas opened up new paradigms for coding in .NET 3.5.

Thanks

Paul 

Coordinator
Oct 7, 2010 at 10:50 PM

Yes, there will be more articles on Clay, inparticular in the context of Orchard. You won't find any examples of complex view models in Orchard, and that is by design: the composition that's going on is the aggregation of a multitude of very small and simple objects. So the Page Object Model as a whole in the end is quite complex, but none of its parts are. The result is that any given partial view has a very small responsibility, is easily swappable (that's the theme engine) and is easy to read and to write.

One way to see a relatively complex POM that is the result of complex aggregation is to use enable the Orchard.Experimental feature. You should then have a little additional piece of UI on the top-left of the screen. Click on it, it should deploy and show "shape templates". Now click on that and you should see additional frames in the page showing you where there are shapes and also gives you hints about how to override their rendering.

Oct 8, 2010 at 11:31 AM

Hi,

By complex, I did not mean complicated, I meant a composition.

I am just looking for a good example in the Orchard source of creating such a composition of objects.

Cheers

Paul 

Coordinator
Oct 8, 2010 at 8:52 PM

Well, that's how I understood it. I'm not sure what to answer because there is nothing but that in Orchard. If you look at a blog post for example, it's the composition of the common part, the publish later part, the route part, the body part, the tags part and the localization part. Each of those will take part in the rendering independently, and you can look at the drivers for each of those as well as their rendering templates. If you add widgets to that, you have a fairly complex composition going on. Does that answer your question? If you are looking for the code that actually does the composition, the thing is that most of this composition is meta-data-driven, not code driven, so that it can be modified at runtime, but you can look at the SetupService.cs file which sets up the initial meta-data for the built-in content types. Does this help?