|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionCurrently, the MotivationThe ADO.NET It hence follows that we need some extensions to the The backbone of the solution is the IValidator InterfaceAll validators which participate in the validation of the data in the namespace DataColumnEx
{
/// <summary>
/// Base interface which will be implemented
/// by classes to validate the values
/// in the extended datacolumn
/// </summary>
public interface IValidator
{
bool IsValid(object value);
}
}
The validator takes in an object by performing rules specified and returns a boolean indicating if the validation is successful. As is obvious, the validation can range from something as simple as restricting values to positive integers to something more complex that encapsulates a business rule. The encapsulation of any business logic in a separate class decouples the business logic governing the validation from the extended data column. DataColumnEx classThe using System;
using System.Data;
namespace DataColumnEx
{
/// <summary>
/// Extended data column which Inherits from data column and adds the
/// capability to validate data as per rules specified in the validator
/// </summary>
public class DataColumnEx : DataColumn
{
private IValidator m_validator;
private DataTable m_table;
private bool m_rejectChangesOnError;
private ResourceMgr m_resMgr =
new ResourceMgr("DataColumnEx.ErrorStrings");
/// <summary>
/// Overloaded Constructor
/// </summary>
/// <param name="table">Table to which this column belongs</param>
/// <param name="name">Name of the column</param>
/// <param name="validator">Validator
//// to validate the proposed value in the column</param>
public DataColumnEx(DataTable table, string name, IValidator validator)
{
ColumnName = name;
m_validator = validator;
m_table = table;
DataType = typeof(string);
if (m_table != null)
{
m_table.ColumnChanged +=
new DataColumnChangeEventHandler(OnColumnChanged);
}
}
/// <summary>
/// Overloaded Constructor
/// </summary>
/// <param name="table">Table to which this column belongs</param>
/// <param name="name">Name of the column</param>
/// <param name="validator">Validator to validate
/// the proposed value in the column</param>
/// <param name="columnType">Data type of this column</param>
public DataColumnEx(DataTable table, string name,
IValidator validator, Type columnType)
{
ColumnName = name;
m_validator = validator;
m_table = table;
DataType = columnType;
if (m_table != null)
{
//Subscribe to the column changed event
m_table.ColumnChanged +=
new DataColumnChangeEventHandler(OnColumnChanged);
}
}
/// <summary>
/// Sets or gets the validator which will validate the proposed value
/// </summary>
public IValidator Validator
{
get { return m_validator; }
set { m_validator = value; }
}
/// <summary>
/// Sets or gets whether the changes made to the row
/// should be rejected if the validation
/// fails for the column
/// </summary>
public bool RejectChangesOnError
{
get { return m_rejectChangesOnError; }
set { m_rejectChangesOnError = value; }
}
private void OnColumnChanged(object sender, DataColumnChangeEventArgs e)
{
if (e.Column == this)
{
object obj = e.ProposedValue;
//If not valid,then reject changes if the flag is set
if (!m_validator.IsValid(obj))
{
if (m_rejectChangesOnError)
e.Row.RejectChanges();
throw new Exception(m_resMgr.GetString("S_ERR_INVALID_DATA"));
}
}
}
}
}
ConclusionI have presented some very elementary scenarios in which the data column can be validated to restrict input. This idea may be extended to more complex scenarios to suit the requirements of the business necessity at hand.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||