Subclassing entity object at runtime

A

Andrus

My instantiated entity object base type (entityBase) contains virtual empty
validation methods.

To implement runtime validation I need to overriide those validation methods
after object is created.

I need to instantiate new object which is based in existing object but has
validation methods overridden.

How to implement this ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

Instead of doing that, why not create a delegate/lambda expression which
is passed to your object and evaluated at that point? The other option
would be to use the classes in the Reflection Emit namespace in order to
create a new class derived from the base and then create the code
dynamically at runtime.
 
I

Ignacio Machin ( .NET/ C# MVP )

My instantiated entity object base type (entityBase) contains virtual empty
validation methods.

To implement runtime validation I need to overriide those validation methods
after object is created.

I need to instantiate new object which is based in existing object but has
validation methods overridden.

How to implement this ?

Andrus.

r those objects add something else than validations?
if not you better use an event to do it. in that case is the code that
instantiate your class the one who decide who is going to do the
validation.
 
A

Andrus

r those objects add something else than validations?

Custom validation logic code should do the following:

1. Validates entered data, may show prompts or ask confirmation from user or
block this field data enty.
2. Calculates some values, e.q sum of order row, sum of whole document in
doc header
3. Shows prompts to user, e.q when quantity entered is more than stock
status, it may ask confirmation from user.
if not you better use an event to do it. in that case is the code that
instantiate your class the one who decide who is going to do the
validation.

Sometimes script writer can deside not to allow to run base validation. If I
create overridden validation methods,
script writen can not call base.OnValidating( sender, e ) method if he/she
desides not to invoke built-in validation.

For this validation script may be

class CustomValidator: StandardOrderValidator {

protected override OnValidating( ValidatingEventArgs e ) {
base.OnValidating( e );
if (e.Cancel)
return;

// standard validation passed, now run custom validation code.
if ( e.Entity.Quantity<0)
MessageBox.Show("Warning: quantity below zero");

}

}



Andrus.
 
A

Arne Vajh¸j

Andrus said:
My instantiated entity object base type (entityBase) contains virtual
empty validation methods.

To implement runtime validation I need to overriide those validation
methods after object is created.

I need to instantiate new object which is based in existing object but
has validation methods overridden.

How to implement this ?

Generate a new sub class dynamically, instantiate and copy all data
does not seem very practical.

You should split data and validation in two separate classes. One
of them can have a reference to the other.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top