Dynamic filter

Posts   
 
    
banusi
User
Posts: 43
Joined: 08-Jul-2006
# Posted on: 11-Jul-2006 15:04:57   

Dear llblgen

I have now spend some time how I can make a webserice, with parameters for construction of a generel filter. I use version 2 of llblgen, adapter twoclasses pattern, c#.

I wan't the following webservice

The problem is that the client, that will call the webservice must not make a reference to ORMSupportClasses, only the <mynamespace>.EntityClasses, FactoryClasses, HelperClasses and RelationClasses.

I need a filter like <field> <op> <value> (e.q. the functionality of FieldCompareValuePredicate class)

[WebMethod]
public EntityCollection getFirmsByField(int fieldindex,int op,object value,int? MaxNumberOfRowsToReturn)

op is a int version of ComparisonOperator enum

or

public EntityCollection getFirmsByField(string fieldname,int op,object value,int? MaxNumberOfRowsToReturn)

Please help a beginner

banusi
User
Posts: 43
Joined: 08-Jul-2006
# Posted on: 11-Jul-2006 16:46:07   

This is my first version. It works, but the switch statement is not so elegant


    [WebMethod]
    public EntityCollection getFirmsByField(Genitime.API.FirmsFieldIndex field,ComparisonOperatorAPI operation,object value,int? MaxNumberOfRowsToReturn)
    {
        EntityCollection firms = new EntityCollection(new FirmsEntityFactory());
        // make the dynamic filter
        IPredicate filterElement;
        IRelationPredicateBucket filter = new RelationPredicateBucket();
        IPredicateExpression expression = new PredicateExpression();
        EntityField2 entityfield = (EntityField2)EntityFieldFactory.Create(field);
        switch (operation)
        {
            case ComparisonOperatorAPI.Equal:
                filterElement = (entityfield == value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.LessEqual:
                filterElement = (entityfield <= value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.LesserThan:
                filterElement = (entityfield < value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.GreaterEqual:
                filterElement = (entityfield >= value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.GreaterThan:
                filterElement = (entityfield > value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.NotEqual:
                filterElement = (entityfield != value);
                expression.Add(filterElement);
                break;
            case ComparisonOperatorAPI.Like:
                expression.Add(entityfield % value.ToString());
                break;
            default:
                filterElement = (entityfield == value);
                expression.Add(filterElement);
                break;
        }
        filter.PredicateExpression.Add(expression);

        using (DataAccessAdapter adapter = new DataAccessAdapter())
        {
            if (MaxNumberOfRowsToReturn.HasValue)
            {
                adapter.FetchEntityCollection(firms, filter,MaxNumberOfRowsToReturn.Value);
            }
            else
            {
                adapter.FetchEntityCollection(firms,filter);
            }
            return firms;
        }
    }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 12-Jul-2006 20:57:36   

You have to reference the ormsupportclasses if you want to compile code which uses the entity classes (.net requirement)

Also, you could create a FieldCompareValuePredicate and cast the ComparisonOperatorAPI to int and then to ComparisonOperator and pass that as the operator. That will eliminate the switch/case simple_smile (keep the same order in the operator values of course between ComparisonOperatorAPI and ComparisonOperator

Frans Bouma | Lead developer LLBLGen Pro
banusi
User
Posts: 43
Joined: 08-Jul-2006
# Posted on: 14-Jul-2006 09:36:37   

Otis wrote:

You have to reference the ormsupportclasses if you want to compile code which uses the entity classes (.net requirement)

Also, you could create a FieldCompareValuePredicate and cast the ComparisonOperatorAPI to int and then to ComparisonOperator and pass that as the operator. That will eliminate the switch/case simple_smile (keep the same order in the operator values of course between ComparisonOperatorAPI and ComparisonOperator

Thanks. I will try that