none
DataSet: Adding Custom Constraints? RRS feed

  • Question

  • Hi!

    I'd like to add a custom check constraint to a table in a typed dataset (a value has to be either null or unique).

    So far I found out that there is an abstract base class "constraint" and a Constraint collection on the Table. I can even see how the standard constraints get added, but I don't see how they are enforced or what method could possibly do the evaluation.

    What I did find was a few events that could be abused to do some checking but how do I add them automatically if someone calls new MyTypedDataSet()?

    Or can I make the constructor protected or internal? Then I could add a static Create method but I don't see yet how I can redefine the visibility of the DataSet constructor.

    Are Custom constraints possible at all?

    Lots of Greetings!


    Volker
    • Moved by SamAgain Friday, June 18, 2010 2:46 AM betterfit (From:.NET Base Class Library)
    Thursday, June 17, 2010 4:11 PM

Answers

  • Hi Volker,

    I'm afraid that you can't define your own custom constraints. 

    ADO.NET has basically two constraint types: ForeignKeyConstraint (to enforce foreign-key constraints) and UniqueConstraint (to enforce data integrity). Both derive from System.Data.Constraint. But although Constraint is an abstract public class, we cannot derive our own custom constraints from it because it contains abstract methods that are marked as internal. (There are other, implicit constraints, that are implemented as properties of the DataColumn like MaxLength and AllowDBNull. But these are all wired up in the .NET framework's code without any direct way to interact with.)

    But you can extend the generated classes of the strongly typed dataset. Typed data sets are implemented as partial classes. This means that you can write your own partial class file, without having to fear that your usercode will get overwritten on the next dataset definition update.

    How to: Extend the Functionality of a Dataset
    http://msdn.microsoft.com/en-us/library/ms171896.aspx

    Partial Classes and Methods (C# Programming Guide)
    http://msdn.microsoft.com/en-us/library/wa80x488.aspx

     

    Marcel

    Thursday, June 17, 2010 8:49 PM

All replies

  • Hi Volker,

    I'm afraid that you can't define your own custom constraints. 

    ADO.NET has basically two constraint types: ForeignKeyConstraint (to enforce foreign-key constraints) and UniqueConstraint (to enforce data integrity). Both derive from System.Data.Constraint. But although Constraint is an abstract public class, we cannot derive our own custom constraints from it because it contains abstract methods that are marked as internal. (There are other, implicit constraints, that are implemented as properties of the DataColumn like MaxLength and AllowDBNull. But these are all wired up in the .NET framework's code without any direct way to interact with.)

    But you can extend the generated classes of the strongly typed dataset. Typed data sets are implemented as partial classes. This means that you can write your own partial class file, without having to fear that your usercode will get overwritten on the next dataset definition update.

    How to: Extend the Functionality of a Dataset
    http://msdn.microsoft.com/en-us/library/ms171896.aspx

    Partial Classes and Methods (C# Programming Guide)
    http://msdn.microsoft.com/en-us/library/wa80x488.aspx

     

    Marcel

    Thursday, June 17, 2010 8:49 PM
  • Yes, I did that and used the RowChanging-Event for some additional checking.

    I defined an event handler but how can I register it when someone calls the constructor?

    Normally, when I design a Form I get to fill in the constructor and can hook up any events I like. Ist here something comparable for DataSets?

    If I could make the construcrot protected I could inherit from it and do my own constructor but the dataset designer doesn't let me change the constructor visibility only.

    Right now I have to rely on the object containing that dataset to do this.

    Is there a better way?

    Lots of Greetings!

    Volker


    Volker
    Friday, June 18, 2010 12:07 PM