Easy Filtering

Posts   
 
    
Dordanov
User
Posts: 10
Joined: 28-Apr-2004
# Posted on: 03-May-2004 11:04:27   

Hello,

We are working with llblgen for almost a week now. So far we love the way access to relations is implemented, and are very impressed by the code generated.

However we now have run into a minor disadvantage of the generated code. We have a Users table with Users wich have a unique ID, a name, password, and a relation to a group.

Let's say we want to fetch a user by it's name (for login purposes) now we have to write the following code to achieve this:

IPredicateExpression filter = new PredicateExpression();
filter.Add(PredicateFactory.CompareValue(UserFieldIndex.Name, ComparisonOperator.Equal, "name"));
UserCollection users = new UserCollection();
users.GetMulti(filter);

However, this seems to me a bit much code for such a simple task. Before we started with llblgen, we tested another OR mapper (RapTier for those who are curious) where we edited the template to make Get methods for each column so we could use this:

User[] users = UserCollection.GetByName("name");

Wich is alot faster and less coding work (as programmers are lazy by nature wink )

Is there a reason it's not implemented this simple yet? and if not, will it be implemented in the near future? We would love to see filtering like this implemented.

Thanks in advance.

Regards, Dordanov

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-May-2004 13:43:27   

Dordanov wrote:

However we now have run into a minor disadvantage of the generated code. We have a Users table with Users wich have a unique ID, a name, password, and a relation to a group.

Let's say we want to fetch a user by it's name (for login purposes) now we have to write the following code to achieve this:

IPredicateExpression filter = new PredicateExpression();
filter.Add(PredicateFactory.CompareValue(UserFieldIndex.Name, ComparisonOperator.Equal, "name"));
UserCollection users = new UserCollection();
users.GetMulti(filter);

However, this seems to me a bit much code for such a simple task. Before we started with llblgen, we tested another OR mapper (RapTier for those who are curious) where we edited the template to make Get methods for each column so we could use this:

User[] users = UserCollection.GetByName("name");

Wich is alot faster and less coding work (as programmers are lazy by nature wink )

Is there a reason it's not implemented this simple yet? and if not, will it be implemented in the near future? We would love to see filtering like this implemented.

I don't think it will, for the following reason simple_smile . If you have 100 tables in your project (and a lot have even more) and per table you have an average of 10 fields, you end up with 1000 GetBy... methods, of which a lot will never be used, because it makes more sense to query on 2 fields, or with another operator, or you want sorting, or limit the amount of rows returned.

If you want such a routine, create a utility class, for example for customer, and add the following routine:


public CustomerCollection GetByField(CustomerFieldIndex index, object value)
{
    IPredicateExpression filter = new PredicateExpression();
    filter.Add(PredicateFactory.CompareValue(index, ComparisonOperator.Equal, value));
    CustomerCollection toReturn = new CustomerCollection();
    toReturn.GetMulti(filter);

    return toReturn;
}

But, perhaps you want sorting, limiting, relations etc. as well... then the GetByField is pretty limited.

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 03-May-2004 18:34:03   

If the "Name" column was set up with a unique constraint wouldn't there be a GetBy... method available?

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-May-2004 20:08:34   

No not in the collections. Unique constraints will always result in 1 object, so these are implemented in the entity classes.

Frans Bouma | Lead developer LLBLGen Pro
Dordanov
User
Posts: 10
Joined: 28-Apr-2004
# Posted on: 04-May-2004 16:25:32   

Thanks for explaining that Frans.

Yeah I suppose you are right that most of these fetch methods will never be used, and it might be better to code a few lines more if you really need it then to have a LOT of useless code generated wich basically adds nothing to the whole.

I invested some time to learn work with the filter and sort methods, and I have to admit it works perfectly well. So far making the step to LLBLGen was a very wise decision wink

Regards, Dordanov

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-May-2004 17:55:04   

smile Thanks for the compliments simple_smile

Frans Bouma | Lead developer LLBLGen Pro