We have a project that uses an Interceptor object to tell NHibernate to do some general works before saving an entity.This interceptor has a single task to do.Now there’s another task that should be added to this interceptor (NHibernate doesn’t support multiple interceptors) but I don’t want to make this interceptor complicated instead I would like to use composition pattern that will manage all registered interceptors.something like this :
public bool Onload(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnLoad(entity,id,state,propertyNames,types);
return result;
}
public bool OnFlushDirty(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnFlushDirty(entity,id,state,propertyNames,types);
return result;
}
by looking at this code I realized that there might be a better way preventing me from repeating myself.
the question is can I make this code more simpler and abstract using Lambda expressions and yield keyword?
One way of doing it could look like this:
And in the parent interceptor:
Update
If you want the result type to be generic you could do it like this:
Execution of the generic version would look exactly the same as of the
boolone because ofType inference.Is this what you had in mind?