Compartilhar via


Using LINQ to write constraints in OCL style v2

Based on Michael Giagnocavo's feedback, here's a cleaner version of my last post. Thanks Michael.

[ValidationState(ValidationState.Enabled)]

public partial class ExampleElement

{

    [ValidationMethod(ValidationCategories.Menu | ValidationCategories.Save)]

    private void TestExampleElement(ValidationContext context)

    {

        var propnames = this.Properties.Select( p => p.Name );

        var distinctnames = propnames.Distinct();

        if (propnames.Count() != distinctnames.Count())

        {

            context.LogError("Non-unique property names", "Error 1");

        }

        var subpropnames = this.Properties.SelectMany( p => p.SubProperties ).Select( p => p.Name );

        var distinctsubpropnames = subpropnames.Distinct();

        if (subpropnames.Count() != distinctsubpropnames.Count())

        {

            context.LogError("Non-unique sub property names", "Error 2");

        }

    }

}

Comments

  • Anonymous
    August 28, 2007
    I've obviously been coding in a dark cell too much of late as there are a few things I've just noticed.

  • Anonymous
    August 28, 2007
    Steve, this is great, but introduces an issue concerning tool usability.  As far as I can see, using the declarative set operations in LINQ takes away the capability to identify (at least one of) the items failing the constraint. It's a nice feature of the DSL Tool Constraint Framework to identify a failing item by association to the task entry. For example, which name is duplicated? Any thoughts about this?

  • Anonymous
    August 28, 2007
    I've obviously been coding in a dark cell too much of late as there are a few things I've just