Is it possible to create new contraint types?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am looking at using System.Data.ForeignKeyConstraint in my DataSet, but it
does not appear to support all of the constriant testing abilities that I
need. Has anyone managed to create a new constraint type by inheriting from
ForeignKeyConstraint or System.Data.Constraint? If so, what do I need to
know to get this to work. I haven't found any documentation on the
implementation of these classes when used by DataTables to maintain the
constraint. Thanks.
 
Harry Keck said:
I am looking at using System.Data.ForeignKeyConstraint in my DataSet, but
it
does not appear to support all of the constriant testing abilities that I
need. Has anyone managed to create a new constraint type by inheriting
from
ForeignKeyConstraint or System.Data.Constraint? If so, what do I need to
know to get this to work. I haven't found any documentation on the
implementation of these classes when used by DataTables to maintain the
constraint. Thanks.

If you want to know if it's possible, why not just try it?

public class MyConstraint : System.Data.Constraint
{
}

Does this compile?
 
Harry, it is possible to do that .. You can find further details and a lot
other such tricks on my book.

http://www.amazon.com/exec/obidos/t...002-9598073-0765600?v=glance&s=books&n=507846

However, since that doesn't publish until another month or so, here is a
pointer to "How to" in advance.

Both the ForeignKey and UniqueConstraint derive from the abstract Constraint
Class. Right? However, several of the functions in these classes need to be
overridden, but it is not possible to since they are internal or protected
within the System.Data namespace. Maybe in a future version of ADO.NET they
won't be but atleast AFAIK, even in ADODOTNET 2.0 it isn't possible to
(People working at MS please confirm).

So how do you do it?? Well below is a cheat-o example of doing it ----

You don't worry about System.Data.Constraint at all. Create a brand new
class, and name it say "USPhoneNumberConstraint"

Put constructors in it that look like ---
Public Sub New()
And,
Public Sub New(ByVal constrainedColumn As DataColumn)
And,
Public Sub New(ByVal constraintName As String, ByVal constrainedColumn As
DataColumn)

Have a CheckExistingValues function in it that gets called in the second New
... this would use constraintCOlumn.Table.Rows and iterate through the values
to check and see if any existing data violates your rule ... and then at the
very end,
If Not constrainedColumn Is Nothing Then
AddHandler constrainedColumn.Table.ColumnChanged, AddressOf
DataColumn_OnChange
End If

Also throw in a CanApplyConstraint, and a IsViolated (booleans)

.... Thats it .. your customConstraint is ready ... How do you use it??

Easy - just do a Dim mynewconstraint as MyConstraintClass = new
MyConstraintClass("constraintname","columnname") .. that puts your
customConstraint on the given column ...

Thats about how close I can get to divulging copyright info without getting
in trouble .. LOL

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
Back
Top