tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 585 forks source link

Expression<Func<T, bool>> support #32

Closed holyprin closed 8 years ago

holyprin commented 12 years ago

Out of pure curiosity would this even be possible? considering the fact you can easily convert Expressions to predicates. this would allow for decent query support given:

IEnumerable users = DbConnection.GetList(user => user.Name == "NameHere" && user.IsAuthenticated);

would be the equiv to your:

var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List() }; pg.Predicates.Add(Predicates.Field(f => f.Name, Operator.Eq, "NameHere")); pg.Predicates.Add(Predicates.Field(f => f.IsAuthenticated, Operator.Eq, true)); IEnumerable list = cn.GetList(pg);

pagebrooks commented 12 years ago

It is certainly possible and LINQ to SQL does this. But you have to decide where you are going to draw the line in supporting such functionality. LINQ to SQL has a lot of support for method translations to SQL, but there are also tons of caveats.

I personally feel like it's a leaky abstraction to support this type of functionality because it is so easy to build a predicate that can't be translated to SQL. You kind of just have to know what will work and what won't work. For example, you obviously couldn't write something like this and have it translate to native SQL. However, this would compile without fault:

IEnumerable parts = DbConnection.GetList(part => SomeNativeCSharpLogic(part));

LINQ to SQL has many limitations on what will work in the predicate expression.
http://msdn.microsoft.com/en-us/library/bb882677 http://msdn.microsoft.com/en-us/library/bb882672

holyprin commented 12 years ago

Totally understood, I was just wondering why the boolean aspect wasn't used (where clause) because that is basically what the custom predicate functions create. Joins / inner joins / outer joins are far beyond the scope of a micro orm, those should be handled imo through raw sql or sprocs via dapper itself. I was just a little curious if the boolean portion were possible, I haven't looked into the source code for more then 5 minutes browsing it.