【发布时间】:2016-04-15 22:37:12
【问题描述】:
大家下午好!我正在使用存储库模式来访问 EDM,我想使用自定义属性 AccessByRole 开发某种权限检查,如下所示:
public abstract class RepositoryBase: IRepository
{
...
[AccessByRole]
public virtual void Add(T entity)
{
....
}
[AccessByRole]
public virtual void Update(T entity)
{
...
}
[AccessByRole]
public virtual void Delete(T entity)
{
...
}
[AccessByRole]
public virtual T GetById(long id)
{
...
}
}
存储库的使用(我使用 Autofac 进行 IoC):
public class Service
{
private readonly IRepository repository;
public Service(IRepository repository)
{
this.repository = reporitory;
}
....
public UpdateUserEntities(...)
{
...
reporitory.Update(T); // There is a need for check user rights before calling this method.
}
}
在调用 CRUD 操作之前有必要检查用户的权限。 所以我的问题是:属性源代码应该是什么样子,所以在权限检查后调用的 CRUD 操作?
【问题讨论】:
-
您应该检查服务中的角色和权限,而不是存储库。通常,存储库只是处理数据请求的有用接口。服务用于您的业务逻辑,检查权限是其中的一部分
-
好吧,我不同意你的方法——在我看来,检查权限更接近数据层而不是服务层。我在这里找到了解决方案:docs.autofac.org/en/latest/advanced/interceptors.html,在这种情况下我只是实现了接口 IInterceptor
标签: c# .net attributes repository-pattern access-rights