Cinch : Major change alert

I have been quite busy with a few other ideas and research lately, but there has always been one thing that has bothered me about my Cinch MVVM Framework which was how the business rules were declared on the business objects.

Previously the rules were instance based, and due to the added complications of me using a specialized Decorater pattern for some of the core business object properties (which is cool as it allow the VM to dictate the editability of the data) I have been struggling to see how to fix this for a while now.

The other day I had a chat with my mate at work a extremely smart chap by the name of Fredrik Bornander who I admire a lot (he is way smarter than I will ever be), and he helped me come up with something cool.

So now what we have is the best of both worlds we declare the rule instances statically per Type, but we add the static rule declarations to instance based fields to validate against.

Here is what we do now:

using System;

using Cinch;
using MVVM.DataAccess;
using System.ComponentModel;
using System.Collections.Generic;

namespace MVVM.Models
{


    public class OrderModel : Cinch.EditableValidatingObject
    {
        #region Data
        private Cinch.DataWrapper<Int32> quantity;

        //rules
        private static SimpleRule quantityRule;

        #endregion

        #region Ctor
        public OrderModel()
        {
            #region Create DataWrappers

            Quantity = new DataWrapper<Int32>(this, quantityChangeArgs);


            #endregion

            #region Create Validation Rules

            quantity.AddRule(quantityRule);

            #endregion

        }

        static OrderModel()
        {
            quantityRule = new SimpleRule("DataValue", 
                 "Quantity can not be < 0",
                      (Object domainObject)=>
                      {
                          DataWrapper<Int32> obj = 
                           (DataWrapper<Int32>)domainObject;
                          return obj.DataValue <= 0;
                      });
        }


        #endregion

        #region Public Properties


        /// <summary>
        /// Quantity
        /// </summary>
        static PropertyChangedEventArgs quantityChangeArgs =
            ObservableHelper.CreateArgs<OrderModel>(x => x.Quantity);

        public Cinch.DataWrapper<Int32> Quantity
        {
            get { return quantity; }
            private set
            {
                quantity = value;
                NotifyPropertyChanged(quantityChangeArgs);
            }
        }



        #endregion

       
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

I am most pleased with this now, as it will sure decrease the amount of memory required by each business object, but it still allows the instance fields to be validated. Nice.

I will be updating the codebase and relevant articles that cover Cinch MVVM Framework some time today.

Thanks Fredrik, you crazy Swede. You rock

Advertisements

3 thoughts on “Cinch : Major change alert

  1. Gerhard Kreuzer says:

    Hi Sacha,

    nice framework. I currently check out your demo app.
    You transfer the data from a db-object to a ui-object.
    I use OpenAccess from Telerik, a very nice ORM-Framework with transparent persistency and lazy loading. If I try to follow your technique, I nearly lost all of that goodies.
    On the other hand, using your data wrapper isn’t possible and so I loose all Cinch-goodies.
    Have you any idea how we can combine this two things?

    With best regards

    Gerhard

    • sacha says:

      Yeah I would not actually use the OpenAccess objects I would convert those in WPF objects and back again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: