I am not able to find which namespace contains what for methods.
NHibernate.IQueryOver
does not contain a definition for 'Add'
and no extension method 'Add' accepting a first argument of type .Visual studio is not helping to get appropriate method on using because of extension method.
How can I know which methods, namespaces should be included?
In case, that we want to pass QueryOver into another method, and execute some filtering over it, we MUST know the type, which is passed.
Below snippet shows, that we have some known interface IBusinessObject
, which has ID and Name. That could help use to create where
conditions for our generic params T and U - and apply some stuff related to that interface:
using NHibernate.Criterion;
namespace MyNamespace
{
public interface IBusinessObject
{
int ID { get; }
string Name { get; }
}
public static QueryOver<T, U> AddSomeFilter<T, U>(QueryOver<T, U> queryOver)
where T: IBusinessObject
where U: IBusinessObject
{
// here we can play with ID, and Name
// because we know that U is of a type IBusinessObject
queryOver
.Where(x => x.ID > 1)
.Where(x => x.Name == "Abc");
return queryOver;
}
}
}
This could be fine, but it could lead to some dependency. That's why I honestly do like to use Criteria API. We can pass some Metadata, and create more dynamic processors:
public static class MyExtension
{
public static ICriteria AddLike(ICriteria criteria, string property, string likeValue)
{
if (property.IsNotEmpty())
{
criteria.Add(Restrictions.Like(property, likeValue));
}
return criteria;
}
To process the method in the comment, we can do it like this:
public class SearchCriteria
{
public string PropertyName { get; set; }
public string LikeValue { get; set; }
}
public static class MyExtension
{
public static IQueryOver<Employee, Employee> ConstructQueryConditions(
this IQueryOver<Employee, Employee> query
, SearchCriteria criteria)
{
if (criteria.PropertyName.IsNotEmpty())
{
query.Where(Restrictions.Like(criteria.PropertyName, criteria.LikeValue));
}
return query;
}