C# windows窗体中的权限
我们在Windows窗体中开发了一个ERP系统,用户需要在整个系统中拥有粒度权限。为了实现这一点,每个表单几乎都有自己的exe。我们有一个主菜单,在其中向数据库添加一个菜单项,然后我们可以授予用户访问此菜单的权限。然后,我们将Guid作为“会话”的id传递给每个exe。在启动exe之前检查权限,而不是exe本身 所以我们的“主菜单”是由这些exe组成的,表格是“Id,Title,exe,…” 因此,随着项目的发展,我们现在有了一个解决方案,其中包含800多个小项目和4个共享dll(数据、BL、UI、打印)。我们的VS解决方案在性能方面确实存在困难 我正在考虑将表单合并到几个项目中,使用自定义属性使用反射来构建菜单。我想,我们不需要启动exe,而是使用反射来实例化表单并显示它?这是最好的方法吗?或者是否有为Windows表单设计的框架可以处理此问题? 这还会提高VS性能吗?相同数量的代码只是更少的项目C# windows窗体中的权限,c#,winforms,security,permissions,C#,Winforms,Security,Permissions,我们在Windows窗体中开发了一个ERP系统,用户需要在整个系统中拥有粒度权限。为了实现这一点,每个表单几乎都有自己的exe。我们有一个主菜单,在其中向数据库添加一个菜单项,然后我们可以授予用户访问此菜单的权限。然后,我们将Guid作为“会话”的id传递给每个exe。在启动exe之前检查权限,而不是exe本身 所以我们的“主菜单”是由这些exe组成的,表格是“Id,Title,exe,…” 因此,随着项目的发展,我们现在有了一个解决方案,其中包含800多个小项目和4个共享dll(数据、BL、U
除此之外,我们还有用于启用/禁用控件的应用程序内权限。尽管我认为它们不会受到影响。我不确定您的中间步骤,但我最近在erp系统中使用的一种方法是使用命令模式的变体。如果您将逻辑放入命令,以检查安全性并启动e表单或其变体,然后您可以使用反射来定位命令,或在启动时定位所有命令并将它们存储在列表中。无论哪种方式,都相对简单
public interface ICommand
{
Guid GetUniqueId();
bool Execute();
}
那么实际的命令可以是
public class YourWinformCommand: ICommand
{
private User _user;
private Guid _securityCode = new Guid(".......");
public Guid GetUniqueId()
{
return _securityCode;
}
public bool Execute()
{
if (_user.HasAccessTo(_securityCode))
{
//Load the winform here.
return true
}
return false;
}
}
使用每个入口点的命令,您应该能够将winforms合并到逻辑集中,并将它们放在DLL中。完成后,使用反射通过接口定位给定命令并测试标识符
/// <summary>
/// This class contains methods to construct a concrete object from an interface.
/// </summary>
public static class ServiceFactory
{
/// <summary>
/// Builds a concrete instance of an interface
/// </summary>
/// <typeparam name="T">The interface of the object</typeparam>
/// <returns>If found, a concrete object is returned that implements the interface</returns>
public static T Build<T>() where T : class
{
string applicationPath = AppDomain.CurrentDomain.BaseDirectory;
string[] files = Directory.GetFiles(applicationPath, "*.dll");
foreach (string file in files)
{
// Dynamically load the selected assembly.
Assembly theAssembly = Assembly.LoadFrom(file);
// See if the type implements T.
foreach (Type type in theAssembly.GetTypes())
{
if (ImplementsInterface(type, typeof(T)))
{
// Use late binding to create the type.
return (T) theAssembly.CreateInstance(type.FullName);
}
}
}
return null;
}
public static bool ImplementsInterface(this Type type, Type ifaceType)
{
return type.GetInterfaces().Any(x => x == ifaceType);
}
}
//
///此类包含从接口构造具体对象的方法。
///
公共静态类服务工厂
{
///
///构建接口的具体实例
///
///对象的接口
///如果找到,将返回实现接口的具体对象
公共静态T Build(),其中T:class
{
字符串applicationPath=AppDomain.CurrentDomain.BaseDirectory;
string[]files=Directory.GetFiles(applicationPath,*.dll);
foreach(文件中的字符串文件)
{
//动态加载选定的程序集。
Assembly=Assembly.LoadFrom(文件);
//查看该类型是否实现了T。
foreach(assembly.GetTypes()中的类型)
{
if(实现接口(类型,类型(T)))
{
//使用后期绑定创建类型。
返回(T)Assembly.CreateInstance(type.FullName);
}
}
}
返回null;
}
公共静态bool实现接口(此类型,类型ifaceType)
{
返回类型.GetInterfaces().Any(x=>x==ifaceType);
}
}
根据该代码,修改它以将guid包含在表达式中,或者修改它以返回所有命令,这是您的选择
希望能有帮助