Steve Cook's WebLog

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");

        }

    }

}

Published Thursday, August 23, 2007 6:35 PM by Steve Cook
Filed under:

Comments

 

GarethJ's WebLog said:

I've obviously been coding in a dark cell too much of late as there are a few things I've just noticed.

August 28, 2007 6:32 PM
 

Keith Short said:

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?

August 28, 2007 9:09 PM
 

Noticias externas said:

I've obviously been coding in a dark cell too much of late as there are a few things I've just

August 28, 2007 11:15 PM
New Comments to this post are disabled

This Blog

Syndication

News

Locations of visitors to this page
Disclaimer

The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the author’s discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker