开发者

boolean aggregation patterns in c#

I am writing a little lookup app, where i have a console handy for quick queries against a cache for sanity checks etc..

i.e.

get SomeField=Blue

this will t开发者_如何学编程han get all objects from cache matching that filter.

I can apply more filters

get SomeField=Blue && SomeOtherField < 5

this can get more complex if i decide to support ()'s

what is a good pattern to use here? or possibly a component that can take a string and tokenize it for me?

for example, i'd want to break down the following into subset of filters

get ((field1=x || field1=y) && field2>x)

the only way i can think of doing this, is regex, and than pass off substrings to different routines designed to create a specific filter. (i.e. AndEquals, OrEquals, AndGraterThan etc)


Have a look at IronPython. It's easy to integrate into a c# app and already supports all standard procedural language constructs. I'm using it in a game engine to perform real-time tweaks to the scene state while debugging.


You shouldn't do this with a regex, you need a full-blown parser. Have a look at ANTLR.


You could use something like the Specification pattern here.

public interface ISpecification<T>
{
    bool IsSatisfiedBy(T instance);
    ISpecification<T> And(ISpecification<T> specification);
    ISpecification<T> Or(ISpecification<T> specification);
    ISpecification<T> Not(ISpecification<T> specification);
}

Full working example here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜