2015-11-02 67 views
0

我正在开发一个基于模块的WPF应用程序,我很好奇如果有一个选项来控制具有用户权限的模块导出? 我有2个用户使用3个模块的应用程序,每个用户有权查看或不查看这些模块,f.ex. 1.用户有权仅查看前两个模块,而第二个用户有权仅查看1.和3.模块。 权限存储在数据库中,并在同步过程中下载并存储在xml配置文件中(我认为这没有问题)。 模块在每个模块项目中输出为:[Export(typeof(MyBestViewModule))]。 或者也许有一些其他的方式来控制这些模块的用户权限?最好的实践或更好的东西,也许有人已经做了,可以分享经验和想法,因为我没有。通过用户权限在WPF中控制模块导出

+0

这听起来不像一个WPF的问题,但更多的东西做与IOC/DependencyInjection架构 - 统一/ MEF或像棱镜。不太清楚你使用的是什么。 –

回答

0

假设,您使用MEF,还有通过FilteredCatalog一种方法来过滤模块:

public class ViewModulesCatalog : FilteredCatalog 
{ 
    public ViewModulesCatalog(ComposablePartCatalog inner, IPermissionsProvider permissionsProvider) 
     : base(inner, part => part.ExportDefinitions.All(def => permissionsProvider.IsAllowed(def))) 
} 

其中IPermissionsProvider是你的自定义服务,来检查当前用户的权限的合同。事情是这样的:

public interface IPermissionsProvider 
{ 
    bool IsAllowed(ExportDefinition def); 
} 

更新

如果您使用棱镜+ MefBootstrapper,你重写方法ConfigureAggregateCatalog

// suppose assemblies with modules are located in "modules" subdir 
var directoryCatalog = new DirectoryCatalog(@".\modules"); 
// let's filter directoryCatalog to get allowed modules 
var permissionsProvider = // ... obtain provider reference here 
var modulesCatalog = new ViewModulesCatalog(directoryCatalog, permissionsProvider); 

return new AggregateCatalog(modulesCatalog); 
+0

对不起,我真的不知道把它放在我的主项目中的什么地方 – John

+0

某处有一个地方,你正在建造MEF目录。您需要从模块构建目录,对其进行过滤,然后添加到MEF容器中。 – Dennis

+0

我用MefBootstrapper ... 现在我已经引导程序类已overrided ConfigureAggregateCatalog()方法,并选择从项目路径中的所有模块到AggregateCatalog(可能是您意味着) – John