Patterns for using linq-to-nh in service layer

0 views
Skip to first unread message

Nieve

unread,
Nov 13, 2009, 5:47:58 PM11/13/09
to nhusers
Hello all,
I'm working on an ASP.NET MVC app, using linq-to-nh and soa.
Thing is, when we get the user entering serach criteria, we have the
search criteria values in the service and need to send them to the DAL
in order to use them on linq where methods and the likes. Before we
also need to verify the values aren't null or empty. Example:
we got an instance of User in our service (Name="Jon", Surname="",
Birthdate, AddressNumber...)
I would like to have a way to send Func<User, bool> to my DAL where
I'll do my Session.Linq<User>.Where(func) instead of having in my DAL
a method per search criteria (GetByName, GetByBirthdate,
GetWhereBornAfter etc').

Does this make any sense? Or would you let the DAL deal with all this
logic?
I was thinking of having a class that verifies the specified
properties aren"t null/empty and then if they're not sends back the
needed Func<T, bool> search criteria (ie user => user.Name == "Jon" &&
user.Birthdate == birthdate && user.AddressNumber == number;)

Any kind of feedback would be highly appreciated!

Fabio Maulo

unread,
Nov 13, 2009, 5:59:11 PM11/13/09
to nhu...@googlegroups.com
btw should be
Expression<Func<TEntity, bool>> predicate
to be used as
Session.Linq<User>.Where(predicate)

2009/11/13 Nieve <niev...@gmail.com>



--
Fabio Maulo

Nieve

unread,
Nov 14, 2009, 9:24:06 AM11/14/09
to nhusers
Hey Fabio,
thanks for that- it made me go and look on the differences between
IEnumerable<T> and IQueryable<T>, so I had a very important lesson as
well :)

As for my question, here's a piece of code I just wrote to express
better what I have in mind.
I'd be, yet again, more than grateful to get any feedback and ideas-
does this make any sense??
Or would having a method in my DAL per search criteria that returns an
IQueryable<TEntity> would be easier to maintain?

public static class ExpressionCriteriaExtensions
{
public static Expression<Func<T, bool>> GetCriteria<T>(this
Expression<Func<T, bool>> ret, T instance, params Expression<Func<T,
object>>[] criteriaProperties)
{
foreach (var expression in criteriaProperties)
{
Func<T, object> func = expression.Compile();
object propertyValue = func(instance);
ret = ret.GetStringEqCriteria(func, propertyValue);
ret = ret.GetDateEqCriteria(func, propertyValue);
}

return ret;
}

private static Expression<Func<T, bool>> GetStringEqCriteria<T>
(this Expression<Func<T, bool>> ret, Func<T, object> func, object
propertyValue)
{
string stringValue = propertyValue as string;
if (!string.IsNullOrEmpty(stringValue))
{
Func<T, object> tmp = func;
if (tmp != null)
{
if (ret == null) ret = t => tmp(t) == stringValue;
else
{
Func<T, bool> currentCriteria = ret.Compile();
ret = t => currentCriteria(t) && tmp(t) ==
stringValue;
}
}
}
return ret;
}

private static Expression<Func<T, bool>> GetDateEqCriteria<T>
(this Expression<Func<T, bool>> ret, Func<T, object> func, object
propertyValue)
{
if (propertyValue is DateTime)
{
DateTime dateTimeValue = (DateTime)propertyValue;
Func<T, object> tmp = func;
if (tmp != null)
{
if (ret == null) ret = t => (DateTime)tmp(t) ==
dateTimeValue;
else
{
Func<T, bool> currentCriteria = ret.Compile();
ret = t => currentCriteria(t) && (DateTime)tmp
(t) == dateTimeValue;
}
}
}
return ret;
}
}

On the service, this would be called like that:
Expression<Func<User,bool>> criteria = null;
criteria = criteria.GetCriteria(user, u => u.Name, u => u.Birthdate);
List<User> list = myDal.GetBy(criteria);

and in MyDal I'll have this:
public IList<User> GetBy(Expression<Func<User, bool>> criteria)
{
return Session.Linq<User>().Where(criteria).ToList();
}

On Nov 13, 11:59 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> btw should be
> Expression<Func<TEntity, bool>> predicate
> to be used as
> Session.Linq<User>.Where(predicate)
>
> 2009/11/13 Nieve <nieveg...@gmail.com>
Reply all
Reply to author
Forward
0 new messages