Note that there are some explanatory texts on larger screens.

plurals
  1. POLibraries for Pattern Matching against POCOs
    text
    copied!<p>Is anyone aware of a .NET library that provides good pattern matching (think regexes for objects instead of text) against an object graph?</p> <p>The closest thing that I can think of is <a href="http://www.tinlizzie.org/ometa/" rel="nofollow">Ometa</a>, but the .NET version seems to be moribund.</p> <p><strong>EDIT</strong></p> <p>This question relates to an idea I am working on that is more in the R&amp;D stage than anything. The application in question accepts <a href="http://en.wikipedia.org/wiki/Specification_pattern" rel="nofollow">specifications</a> and passes them off to a repository, which then translates them (using their lambdas) into SQL using Entity Framework.</p> <p>As a generic technique, this works all right. For some queries, though, the results are extremely slow. The idea would be to have some optimization rules in the repository which checks whether the incoming specification matches one of the optimized hand-written queries. </p> <p>As an example, let us say that we are using specifications for a model that looks like this:</p> <pre><code> class Person { public string FirstName { get; set; } public string LastName { get; set; } List&lt;PhoneNumber&gt; PhoneNumbers { get; set; } } class PhoneNumber { public string Number { get; set; } public PhoneNumberType PhoneNumberType { get; set; } } enum PhoneNumberType { Cell, Landline } </code></pre> <p>And then our basic specifications are:</p> <pre><code> interface ISpecification&lt;T&gt; { Expression&lt;Func&lt;T,bool&gt;&gt; Predicate { get; } } class GetPersonByPhoneNumberTypeSpecification : ISpecification&lt;Person&gt; { public PhoneNumberType PhoneNumberType { get; set; } public Expression&lt;Func&lt;Person,bool&gt;&gt; Predicate { return a =&gt; a.PhoneNumbers.Any(b =&gt; b.PhoneNumberType == PhoneNumberType); } } class GetPersonRequest { ISpecification Specification { get; set; } } </code></pre> <p>In the repository, we might have a hand-written query to select people who have a specific phone number type associated with them and want to use it if the query matches.</p> <p>The whole point would be able to say without writing a ton of code to check an entire composable specification, that a given request has a given "shape". Imagine an object matching language like this.</p> <p>Another way to think of this might well be as an XPath query for objects instead of XML.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload