Convenience Method Feature Request

Posts   
 
    
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 01-Apr-2005 01:00:44   

Hi, Frans. This is just a nicety, but how about a feature that allows you to fetch entity collections using "template" entities. Something like:


Dim customer as New CustomerEntity()
customer.LastName = "Smith"

Dim matchedCustomers as New EntityCollection(New CustomerEntityFactory())
Dim adapter as New DataAccessAdapter()
adapter.FetchEntityCollection(matchedCustomers,customer.CreatePredicateFromSuppliedFields())

Basically, just some code that would iterate through an entity's fields, find fields with values that are supplied, then construct a predicate to match. A bonus would be if the code could travers the related entities that are supplied and discover supplied fields there as well.

You wouldn't be able to get full-featured predicates out of this, but it would be useful as a means of finding data that matches supplied fields.

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 01-Apr-2005 12:45:18   

Something like this?


/// <summary>
/// Creates a predicate expression using the fields passed in. Each changed field ends up in a
/// FieldCompareValuePredicate, and-ed together and if an object alias is set, it will be used
/// as well.
/// </summary>
/// <param name="fields">fields object with fields of which each field will end up in a FieldCompareValuePredicate</param>
public IPredicateExpression CreatePredicateFromMatchFields(IEntityFields2 fields)
{
    return CreatePredicateFromMatchFields(fields, PredicateExpressionOperator.And)
}


/// <summary>
/// Creates a predicate expression using the fields passed in. Each changed field ends up in a
/// FieldCompareValuePredicate, placed into the predicate expression to return using the operator supplied.
/// If an object alias is set, it will be used as well.
/// </summary>
/// <param name="fields">fields object with fields of which each field will end up in a FieldCompareValuePredicate</param>
/// <param name="operatorToUse">predicate expression operator to use to chain the different FieldCompareValuePredicate
/// instances together in the PredicateExpression to return.</param>
public IPredicateExpression CreatePredicateFromMatchFields(IEntityFields2 fields, PredicateExpressionOperator operatorToUse)
{
    IPredicateExpression toReturn = new PredicateExpression();
    
    foreach(IEntityField2 field in fields)
    {
        if(!field.IsChanged)
        {
            continue;
        }
        
        IPredicate filterElement = new FieldCompareValuePredicate(
            field, null, ComparisonOperator.Equal, field.CurrentValue, field.ObjectAlias);
        
        switch(operatorToUse)
        {
            case PredicateExpressionOperator.And:
                toReturn.AddWithAnd(filterElement);
                break;
            case PredicateExpressionOperator.Or:
                toReturn.AddWithOr(filterElement);
                break;
        }
    }
    
    if(toReturn.Count<=0)
    {
        return null;
    }
    else
    {
        return toReturn;
    }
}

Frans Bouma | Lead developer LLBLGen Pro