- XomegaFrameworkSamples.zip
- MyProject4
- MyProject4.Client.Objects
- MyProject4.Client.Web
- Gen
- MyProject4.Client.WPF
- MyProject4.Entities
- MyProject4.Model
- Framework
- base_types.xom
- operators.xom
- global_config.xom
- Import
- _types.xom
- aw_build_version.xom
- database_log.xom
- error_log.xom
- HumanResources
- department.xom
- employee.xom
- employee_address.xom
- employee_department_history.xom
- employee_pay_history.xom
- job_candidate.xom
- shift.xom
- Person
- address.xom
- address_type.xom
- contact.xom
- contact_type.xom
- country_region.xom
- state_province.xom
- Production
- bill_of_materials.xom
- culture.xom
- document.xom
- illustration.xom
- location.xom
- product.xom
- product_category.xom
- product_cost_history.xom
- product_description.xom
- product_document.xom
- product_inventory.xom
- product_list_price_history.xom
- product_model.xom
- product_model_illustration.xom
- product_model_product_description_culture.xom
- product_photo.xom
- product_product_photo.xom
- product_review.xom
- product_subcategory.xom
- scrap_reason.xom
- transaction_history.xom
- transaction_history_archive.xom
- unit_measure.xom
- work_order.xom
- work_order_routing.xom
- Purchasing
- product_vendor.xom
- purchase_order_detail.xom
- purchase_order_header.xom
- ship_method.xom
- vendor.xom
- vendor_address.xom
- vendor_contact.xom
- Sales
- contact_credit_card.xom
- country_region_currency.xom
- credit_card.xom
- currency.xom
- currency_rate.xom
- customer.xom
- customer_address.xom
- individual.xom
- sales_order_detail.xom
- sales_order_header.xom
- sales_person.xom
- sales_person_quota_history.xom
- sales_reason.xom
- sales_tax_rate.xom
- sales_territory.xom
- sales_territory_history.xom
- shopping_cart_item.xom
- special_offer.xom
- special_offer_product.xom
- store.xom
- store_contact.xom
- MyProject4.Model.xomproj
- MyProject4.Services
- Xomega.Fwk_src_1.3.zip
- Xomega.Framework
- xomega.fwk_src_1.3.zip
|
// Copyright (c) 2010-2012 Xomega.Net. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
namespace Xomega.Framework.Properties
{
/// <summary>
/// A data property that holds integer values.
/// </summary>
public class IntegerProperty : DataProperty<int?>
{
/// <summary>
/// The minimum valid value for the property.
/// </summary>
public int MinimumValue { get; set; }
/// <summary>
/// The maximum valid value for the property.
/// </summary>
public int MaximumValue { get; set; }
/// <summary>
/// Constructs an IntegerProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public IntegerProperty(DataObject parent, string name)
: base(parent, name)
{
MinimumValue = int.MinValue;
MaximumValue = int.MaxValue;
Validator += ValidateInteger;
Validator += ValidateMinimum;
Validator += ValidateMaximum;
}
/// <summary>
/// Overrides the base method to construct a list of non-Nullable integer values
/// for the Transport format, since it's a value type.
/// </summary>
/// <param name="format">The format to create a new list for.</param>
/// <returns>A new list for the given format.</returns>
protected override IList CreateList(ValueFormat format)
{
return format == ValueFormat.Transport ? new List<int>() : base.CreateList(format);
}
/// <summary>
/// Converts a single value to a given format. For typed formats
/// this method tries to convert various types of values to a nullable integer.
/// </summary>
/// <param name="value">A single value to convert to the given format.</param>
/// <param name="format">The value format to convert the value to.</param>
/// <returns>The value converted to the given format.</returns>
protected override object ConvertValue(object value, ValueFormat format)
{
if (format.IsTyped())
{
if (value is int?) return value;
if (value is int) return (int?)value;
if (value is double) return (int?)((double)value);
if (IsValueNull(value, format)) return null;
int i;
if (int.TryParse(Convert.ToString(value), NumberStyles.Number, null, out i)) return i;
}
return base.ConvertValue(value, format);
}
/// <summary>
/// A validation function that checks if the value is an integer and reports a validation error if not.
/// </summary>
/// <param name="dp">Data property being validated.</param>
/// <param name="value">The value to validate.</param>
public static void ValidateInteger(DataProperty dp, object value)
{
if (dp != null && !dp.IsValueNull(value, ValueFormat.Internal)
&& !(value is int) && !(value is short) && !(value is byte))
dp.ValidationErrors.AddError("{0} contains an invalid number.", dp);
}
/// <summary>
/// A validation function that checks if the value is an integer that is not less
/// than the property minimum and reports a validation error if it is.
/// </summary>
/// <param name="dp">Data property being validated.</param>
/// <param name="value">The value to validate.</param>
public static void ValidateMinimum(DataProperty dp, object value)
{
IntegerProperty idp = dp as IntegerProperty;
if (idp != null && (value is int?) && ((int?)value).Value < idp.MinimumValue)
dp.ValidationErrors.AddError("{0} cannot be less than {1}.", dp, idp.MinimumValue);
}
/// <summary>
/// A validation function that checks if the value is an integer that is not greater
/// than the property maximum and reports a validation error if it is.
/// </summary>
/// <param name="dp">Data property being validated.</param>
/// <param name="value">The value to validate.</param>
public static void ValidateMaximum(DataProperty dp, object value)
{
IntegerProperty idp = dp as IntegerProperty;
if (idp != null && (value is int?) && ((int?)value).Value > idp.MaximumValue)
dp.ValidationErrors.AddError("{0} cannot be greater than {1}.", dp, idp.MaximumValue);
}
}
/// <summary>
/// An integer property for positive numbers only.
/// </summary>
public class PositiveIntegerProperty : IntegerProperty
{
/// <summary>
/// Constructs a PositiveIntegerProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public PositiveIntegerProperty(DataObject parent, string name)
: base(parent, name)
{
MinimumValue = 0;
}
}
/// <summary>
/// An integer property for numbers in the range defined by the <c>short</c> CLR type.
/// The <c>short</c> type is used for the transport value format.
/// </summary>
public class SmallIntegerProperty : IntegerProperty
{
/// <summary>
/// Constructs a SmallIntegerProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public SmallIntegerProperty(DataObject parent, string name)
: base(parent, name)
{
MinimumValue = short.MinValue;
MaximumValue = short.MaxValue;
}
/// <summary>
/// Overrides the base method to construct a list of non-Nullable short values
/// for the Transport format.
/// </summary>
/// <param name="format">The format to create a new list for.</param>
/// <returns>A new list for the given format.</returns>
protected override IList CreateList(ValueFormat format)
{
return format == ValueFormat.Transport ? new List<short>() : base.CreateList(format);
}
/// <summary>
/// Converts a single value to a given format. For the transport format
/// the value is converted to a <c>short</c> type.
/// </summary>
/// <param name="value">A single value to convert to the given format.</param>
/// <param name="format">The value format to convert the value to.</param>
/// <returns>The value converted to the given format.</returns>
protected override object ConvertValue(object value, ValueFormat format)
{
if (format == ValueFormat.Transport && value is int?)
return (short?)(int?)value;
return base.ConvertValue(value, format);
}
}
/// <summary>
/// An small integer property for positive numbers only.
/// </summary>
public class PositiveSmallIntProperty : SmallIntegerProperty
{
/// <summary>
/// Constructs a PositiveSmallIntProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public PositiveSmallIntProperty(DataObject parent, string name)
: base(parent, name)
{
MinimumValue = 0;
}
}
/// <summary>
/// An integer property for numbers in the range defined by the <c>byte</c> CLR type.
/// The <c>byte</c> type is used for the transport value format.
/// </summary>
public class TinyIntegerProperty : IntegerProperty
{
/// <summary>
/// Constructs a TinyIntegerProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public TinyIntegerProperty(DataObject parent, string name)
: base(parent, name)
{
MinimumValue = byte.MinValue;
MaximumValue = byte.MaxValue;
}
/// <summary>
/// Overrides the base method to construct a list of non-Nullable byte values
/// for the Transport format.
/// </summary>
/// <param name="format">The format to create a new list for.</param>
/// <returns>A new list for the given format.</returns>
protected override IList CreateList(ValueFormat format)
{
return format == ValueFormat.Transport ? new List<byte>() : base.CreateList(format);
}
/// <summary>
/// Converts a single value to a given format. For the transport format
/// the value is converted to a <c>byte</c> type.
/// </summary>
/// <param name="value">A single value to convert to the given format.</param>
/// <param name="format">The value format to convert the value to.</param>
/// <returns>The value converted to the given format.</returns>
protected override object ConvertValue(object value, ValueFormat format)
{
if (format == ValueFormat.Transport && value is int?)
return (byte?)(int?)value;
return base.ConvertValue(value, format);
}
}
/// <summary>
/// An integer key property to distinguish keys from regular integers. Unlike for regular integers
/// comparison operators other than equality may not be applicable to integer keys.
/// </summary>
public class IntegerKeyProperty : IntegerProperty
{
/// <summary>
/// Constructs a IntegerKeyProperty with a given name and adds it to the parent data object under this name.
/// </summary>
/// <param name="parent">The parent data object to add the property to if applicable.</param>
/// <param name="name">The property name that should be unique within the parent data object.</param>
public IntegerKeyProperty(DataObject parent, string name)
: base(parent, name)
{
}
}
}
|
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.
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