Не можете ли вы добиться этого с помощью фильтров?
Более подробную информацию можно найти здесь
Я использовал это в сочетании с перехватчиками в моем проекте:
У меня есть некоторые объекты, из которых каждый пользователь может создавать экземпляры, но только пользователь, создавший их, должен иметь возможность видеть / изменять эти экземпляры. Другие пользователи не могут видеть экземпляры, созданные пользователем X.
Для этого я создал интерфейс IUserContextAware
. Объекты, которые «осведомлены о контексте пользователя», реализуют этот интерфейс.
При создании фабрики сессий я создал необходимые фильтры:
var currentUserFilterParametersType = new Dictionary<string, NHibernate.Type.IType> (1);
currentUserFilterParametersType.Add (CurrentUserContextFilterParameter, NHibernateUtil.Guid);
cfg.AddFilterDefinition (new FilterDefinition (CurrentUserContextFilter,
"(:{0} = UserId or UserId is null)".FormatString (CurrentUserContextFilterParameter),
currentUserFilterParametersType,
false));
Когда это было сделано, мне нужно было определить дополнительные критерии фильтра:
foreach( var mapping in cfg.ClassMappings )
{
if( typeof (IUserContextAware).IsAssignableFrom (mapping.MappedClass) )
{
// The filter should define the names of the columns that are used in the DB, rather then propertynames.
// Therefore, we need to have a look at the mapping information.
Property userProperty = mapping.GetProperty ("UserId");
foreach( Column c in userProperty.ColumnIterator )
{
string filterExpression = ":{0} = {1}";
// When the BelongsToUser field is not mandatory, NULL should be taken into consideration as well.
// (For instance: a PrestationGroup instance that is not User-bound (that can be used by any user), will have
// a NULL value in its BelongsToUser field).
if( c.IsNullable )
{
filterExpression = filterExpression + " or {1} is null";
}
mapping.AddFilter (CurrentUserContextFilter, "(" + filterExpression.FormatString (CurrentUserContextFilterParameter, c.Name) + ")");
break;
}
}
Теперь, когда я создаю экземпляр ISession
, я указываю, что должен использоваться определенный перехватчик:
Этот перехватчик обеспечивает заполнение параметра фильтра:
internal class ContextAwareInterceptor : EmptyInterceptor
{
public override void SetSession( ISession session )
{
if( AppInstance.Current == null )
{
return;
}
// When a User is logged on, the CurrentUserContextFilter should be enabled.
if( AppInstance.Current.CurrentUser != null )
{
session.EnableFilter (AppInstance.CurrentUserContextFilter)
.SetParameter (AppInstance.CurrentUserContextFilterParameter,
AppInstance.Current.CurrentUser.Id);
}
}
}