I’ve been struggling a bit with this tiny problem – and I’m quite sure there’s an “easy” solution.
I have a generic nHibernate base-repository class with the following method:
public IList<T> GetAll()
{
using (var session = SessionProvider.OpenSession())
{
return session.Query<T>().ToList();
}
}
However – I’m trying to control my model by using some very simple interfaces. I have an interface – ISetDeleted:
public interface ISetDeleted
{
bool Deleted { get; set; }
}
In my GetAll()-method I would like to check it the current type implements this interface – and if it does, only return the entities that are not marked as deleted:
public IList<T> GetAll()
{
using (var session = SessionProvider.OpenSession())
{
if (typeof(T) is ISetDeleted)
{
// Only retrieve entities that are not marked as deleted
// WHAT DO I DO HERE?
}
return session.Query<T>().ToList();
}
}
I know I could just retrieve all the entities and loop through these – but I would prefer a cleaner approach – e.g. an expression that implements the check (if possible).
It would be very much appreciated if someone could help me out with this 🙂
First
will only return true if the type parameter is ISetDeleted, not if it implements the interface. You probably want
Second, I think you want