Fool-Proof Business Forms in Silverlight Part I

Silverlight DataForms makes it quick and easy to get started with database applications. However, it has some downsides. The default isn’t particularly customizable, and to do anything more takes a great deal more effort.

Take the following example:

        internal sealed class OrderMetadata
        {

            // Metadata classes are not meant to be instantiated.
            private OrderMetadata()
            {
            }

            [Display(Name = "Order date", GroupName = "Order", Order = 1)]
            public DateTime OrderDate { get; set; }

            [Range(0, 100.0, ErrorMessage = "Please enter more than $0.00")]
            [Display(Name = "Total Amount", GroupName = "Order", Order = 2)]
            public decimal Amount { get; set; }

            [Display(Name = "Purchase Order No", GroupName = "Order", Order = 3)]
            public string PurchaseOrder { get; set; }

            [Display(Name = "Customer Name", GroupName = "Customer", Order = 1)]
            public Customer Customer { get; set; }

            [Display(Name = "Is Delivered", GroupName = "Customer", Order = 2)]
            public byte[] IsDelivered { get; set; }

            [ScaffoldColumn(false)]
            public int CustomerId { get; set; }

            [ScaffoldColumn(false)]
            public int OrderId { get; set; }
            
            public float Weight { get; set; }

            [UIHint("Image", "SL", "ScannedFilename")]
            public Byte[] ScannedImage { get; set; }

            [ScaffoldColumn(false)]
            public String ScannedFilename { get; set; }
        }

This generates a data form that gets us pretty close. See the picture below.

dataform1

However, there are several issues:

  1. The Order date is very ugly. It should have default to a null value
  2. The Customer field should have been bound to a combo box or something more custom
  3. The Total Amount validation message could not be customized. It reports “Input is not in the correct format”. Ideally, the message should be customizable and localizable.
  4. CustomerId and OrderId should be hidden
  5. Scanned file name and Scanned image should be combined into a single image control, showing the image, the file name, and provide a method to replace the image.

Over the next few posts, I’ll discuss how to overcome these problems using a custom view model and avoid unnecessary code duplication.