NHibernate QueryOver<T> with Interfaces?
I have a repository object that knows the Type of the mapped hibernate class. But it doesn't have a generic <T>
type but instead a reference to a Type object Type TheMappedType
. So I cannot do something like:
session.QueryOver<typeof开发者_如何学JAVA(TheMappedType)>()...
.
Usually what I can do with hibernate is:
session.Get(typeof(TheMappedType), someId)
.
So what's the big picture: My repository is a generic class with a generic interface type. I want to pass some lambda functions on this interface (for instance some where restrictions in a get method) and I want it to be translated to the actually mapped type... is there some way to do it?
Thank you Max
You can make your referened type generic by using TheMappedType.MakeGeneric(params)and then use it in QueryOver method. And if you pass any lambda function to your repository then it will automatically translate it into your generic type.
I did the following in my repository
System.Linq.Expressions.Expression<Func<Employee, bool>> expression = items => items.Name == "abc";
IList employeeList = Repository<Employee>.FindRecords(expression)
and the repository class is having following method:
Repository<T>
{
IList< T > FindRecords(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
return Session.QueryOver<T>().Where(expression).List();
}
}
精彩评论