need help on basic contentpart binding ?

Topics: General, Writing modules
May 27, 2012 at 1:59 PM

Hi,

I'm new to orchard, MVC and am following http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-8 tutorials which is great. please go easy on me :D

 

SchemaBuilder.CreateTable("AddressRecord", t => t
                .ContentPartRecord()
                .Column<int>("CustomerId")
                .Column<string>("Type", c => c.WithLength(50))
                );

 ContentDefinitionManager.AlterPartDefinition(typeof(AddressPart).Name, p => p
                .Attachable(false)
                .WithField("Name", f => f.OfType(typeof(TextField).Name))
                .WithField("AddressLine1", f => f.OfType(typeof(TextField).Name))
                .WithField("AddressLine2", f => f.OfType(typeof(TextField).Name))
                .WithField("Zipcode", f => f.OfType(typeof(TextField).Name))
                .WithField("City", f => f.OfType(typeof(TextField).Name))
                .WithField("Country", f => f.OfType(typeof(TextField).Name))
                );

 

why doesn't he create table addressrecord with all fields "name, addr1, addr2, zip, country..."? i just don't understand where these values can store in the database

 

and how in the world dynamic engine could know where to pick up the above fields and bind to addresspart like the below method ?

 

private AddressVM MapAddress(AddressPart addressPart) {
            dynamic address = addressPart;
            var addressVM = new AddressVM();

            if (addressPart != null)
            {
                addressVM.Name         = address.Name.Value;     << where the value comes from ?
                addressVM.AddressLine1 = address.AddressLine1.Value;
                addressVM.AddressLine2 = address.AddressLine2.Value;
                addressVM.Zipcode      = address.Zipcode.Value;
                addressVM.City         = address.City.Value;
                addressVM.Country      = address.Country.Value;
            }

            return addressVM;
        }

 

Thanks,

Nam Vo

Developer
May 27, 2012 at 8:37 PM
Edited May 28, 2012 at 8:19 AM

Welcome to the wonderful world of Orchard :)

About the content fields instead of columns: The reasons I chose to use content fields over columns are flexibility, reusabality and extensibility:

  • Flexibility and extensibility in the sense that users could add, change and remove content fields from the AddressPart without having to modify the source of the module.
  • Reusability in the sense that each content field has a editor, so I don't have to provide an editor for the fields myself. The admin user or developer could attach other content fields, also without having to change the source of the webshop module.

That said, it would be perfectly fine to create columns instead. Just know the advantages and disadvantages of each approach (and mix and match as per your requirement)

The field values are stored in the ContentItemRecord table (or in the ContentItemVersionRecord table if the content item is draftable) in a column called Data, in XML format. Check out the database schema and Orchard source code repeatedly to learn a little bit more each time.

As for the dynamic engine, the way this works is that a ContentPart (from which AddressPart is derived) implements an interface called IContentBehavior, which has a single property called Behavior of type IClayBehavior. Clay behaviors are used by Clay to determine what should happen when a method or property is invoked on an object that is dynamically typed. So, even though the AddressPart class doesn't contain any property called Zipcode or City, when you cast an instance of AddressPart (or even its base ContentPart) to dynamic, the attached Clay behaviors kick in whenever you invoke a method on the instance. There are a variety of Clay behaviors defined, one of which implements looking up attached content fields. Although perhaps mind boggling at first, it's not very complex once you know how it works.

To learn more about how this works, definitely check out http://downplay.co.uk/tutorials/clay/down-the-rabbit-hole

May 28, 2012 at 3:07 AM

Thanks for your prompt reply. Last time i checked orchard v0.8, i felt it's beyond my knowledge. But this time, while looking at umbraco, composite, dnn..., i stopped at Orchard 1.4.2 since these others are not flexible enough to work with and found your tutorials, you should write a book about it,  very helpful for a newbie like me to step in.

I believe this system is a right choice for me to buid my works on, Orchard still lack a good commerce like nopcommerce, my favorite commerce system, to be perfect. Any one know if orchard would have its own commerce module or still reply on community projects. Nopcommerce is integrated into umbraco and composite but don't know when it's on Orchard though many key systems(localization, structure...) in Nop are carried from Orchard.

 

Good day,

Nam Vo