Click here to Skip to main content
15,885,985 members
Articles / Web Development / ASP.NET

Take MVC to the Next Level in .NET

Rate me:
Please Sign up or sign in to vote.
4.62/5 (11 votes)
30 Apr 2013GPL315 min read 73.1K   858   75  
How to quickly build reusable and flexible WPF, Silverlight, or ASP.NET applications with the powerful Xomega Framework using the best MVVM principles.
using System;
using System.Collections.Generic;
using System.ServiceModel;
using MyProject4.Services;
using Xomega.Framework;
using Xomega.Framework.Lookup;
using Xomega.Framework.Properties;

namespace MyProject4.Client.Objects
{
    public partial class CustomerProperty : EnumIntProperty 
    {
        /// <summary>
        /// A constant used for the cached lookup table name
        /// </summary>
        public const string EnumTypeCustomer = "customer";

        /// <summary>
        /// Additional attribute for each value to store the formatted id
        /// </summary>
        public const string AttributeFormattedCustomerId = "formatted customer id";

        public CustomerProperty(DataObject parent, string name)
            : base(parent, name)
        {
            EnumType = EnumTypeCustomer;
            
            // display value as 001234 - Joe Doe
            DisplayFormat = String.Format("{0} - {1}",
                string.Format(Header.AttrPattern, AttributeFormattedCustomerId),
                Header.FieldText);

            // add custom validation rule
            Validator += ValidateCustomer;

            bool canUserViewCustomers = true; // TODO: retrieve View Customer privilege
            bool canUserEditCustomers = true; // TODO: retrieve Edit Customer privilege
            AccessLevel = canUserEditCustomers ? AccessLevel.Full :
                canUserViewCustomers ? AccessLevel.ReadOnly : AccessLevel = AccessLevel.None;
        }

        // override to fine-tune the value conversion rules
        protected override object ConvertValue(object value, ValueFormat format)
        {
            int id;
            if (format == ValueFormat.Internal && Int32.TryParse("" + value, out id))
            {
                // try parsing the id to remove leading zeros, etc.
                return base.ConvertValue(id, format);
            }
            return base.ConvertValue(value, format);
        }

        // checks if the value has been resolved to a valid customer in the lookup table
        public static void ValidateCustomer(DataProperty dp, object value)
        {
            Header cust = value as Header;
            if (value != null && (cust == null || cust.Type != EnumTypeCustomer))
                dp.ValidationErrors.AddError("{0} has an invalid customer {1}.", dp, value);
        }

        // Returns possible customer values from the lookup cache.
        // If the values are not yet in the cache, reads them
        // from service layer and stores in the cache.
        protected override LookupTable GetLookupTable()
        {
            LookupTable res = base.GetLookupTable();
            if (res == null)
            {
                List<Header> data = new List<Header>();
                foreach (Store_ReadListOutput row in ReadList())
                {
                    Header h = new Header(EnumType, "" + row.CustomerId, row.Name);
                    // store the formatted id as a separate attribute
                    h[AttributeFormattedCustomerId] = row.CustomerId.ToString("000000");
                    data.Add(h);
                }
                res = new LookupTable(EnumType, data, true);
                LookupCache cache = LookupCache.Get(CacheType);
                if (cache != null) cache.CacheLookupTable(res);
            }
            return res;
        }

        // reads the possible customer values from the service layer
        protected virtual IEnumerable<Store_ReadListOutput> ReadList()
        {
            ChannelFactory<IStoreService> factory = new ChannelFactory<IStoreService>("IStoreService");
            IStoreService svc = factory.CreateChannel();
            IEnumerable<Store_ReadListOutput> output = svc.ReadList(new Store_ReadListInput());
            factory.Close();

            return output;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect Xomega.Net
United States United States
Xomega Team is striving to increase productivity and development quality by utilizing Model Driven Development coupled with Code Generation and the best design practices for application development.
We provide MDD tools, code generators and frameworks for Visual Studio and .Net development.
Visit us at http://www.xomega.net
This is a Organisation

1 members

Comments and Discussions