如何在Windows窗体应用程序项目中调用类上的任何方法之前调用特定方法? [英] How to call an specific method before calling any methods on a class in Windows Form Application Project?

查看:87
本文介绍了如何在Windows窗体应用程序项目中调用类上的任何方法之前调用特定方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我想在一个类的第一个方法中调用一个特殊的方法来授权。


是否具有自定义属性。例如,有一个名为TestClass的类,其中有两个方法:


公共类TestClass  
{
public void TestMethod1()
{           
}

public void TestMethod2()
{           
}
}


并且,还有另一个类用于授权和检查当前用户的方法权限:


公共类UserAuthentication 
{         
public static bool CheckPermission(string activityName)
        {           
DataService.UserManagment us = new
DataService.UserManagment();            var
hasPermission = us.HasPermission(Properties.Settings.Default.UserID,activityName); 
          if(!hasPermission&&!Properties.Settings.Default.IsAdmin)           
{               
抛出新的AuthenticationException(" Permission Error");           
}
       返回true;
        }
}

所以,我想在TestClass上运行任何方法之前调用CheckPermission并将方法的名称作为activityname传递。


它可能有反射和自定义属性,但我对此没有任何线索。 



解决方案

在我看来,这真的不是正确的做法。它有太多问题。首先,它是所有方法还是只是一些方法?如果是全部,那么你可以看一下在类型的构造函数中做某事。但由于权限问题而未能创建
对象只是感觉不对。此外,你将不得不破解解决方案,以使单元测试工作。


如果它是一些,那么属性将起作用,但除非你这样做,否则不使用属性显然所以每次调用"受保护"的方法首先需要检查属性。拥有一个属性调用服务将会对性能产生不良影响,更不用说属性是元数据这样的事实,因此他们无法访问编译时设置的任何数据。所以注入服务或其他什么都行不通。因此,现在您需要定义静态/单例
来提供对东西的访问权限,以便属性可以使用它们。它会变得复杂。


在我看来,最好的方法是使用上下文并让每个方法检查上下文是否需要强制执行权限。这假设您还没有使用DI容器或者您也可以使用等效逻辑的东西。通常,
权限检查应该在架构中高位进行,因此从技术上讲,你是UI或控制器应该处理安全性,而你的业务逻辑的其余部分不应该担心它。但是可以将上下文类型传递给方法以允许您"检查"。如果需要,可以在业务层中使用
权限。上下文可以像绑定到运行代码的线程的IPrincipal一样简单,也可以是您定义的自定义类型,用于存储作为用户主体一部分无意义的数据。


Hi everybody

i wanna call an special method at the first of any methods of a class in order to authorizing.

whether with a custom attribute or not. for example there is a class named TestClass and two methods in there:

public class TestClass 
{
       public void TestMethod1()
       {           
       }
        
       public void TestMethod2()
       {           
       }
}

and, there is another class for authorizing and check method permission for current user:

public class UserAuthentication
{         
       public static bool CheckPermission(string activityName)
        {            
               DataService.UserManagment us = new 
               DataService.UserManagment();            var 
               hasPermission = us.HasPermission(Properties.Settings.Default.UserID, activityName); 
               if (!hasPermission && !Properties.Settings.Default.IsAdmin)            
               {                
                    throw new AuthenticationException("Permission Error");            
               }
               return true;
        }
}

so, i wanna call CheckPermission before running any methods on TestClass and pass method's name as activityname.

it maybe possible with reflections and custom attribute but i don't have a clue about that. 

解决方案

This really isn't the correct approach in my opinion. There are simply too many issues with it. Firstly, is it all methods or just some methods? If it is all then you could look at doing something in the constructor for the type. But failing to create an object because of a permissions issue just feels wrong. Furthermore you're going to have to hack the solution in order to get unit tests to work.

If it is some then an attribute would work but attributes aren't used unless you do so explicitly so every call to the "protected" methods would first need to check the attribute. Having an attribute make a call to a service is going to be bad on performance not to mention the fact that attributes are metadata so they won't have access to any data outside what is set up at compilation. So injecting services or whatnot isn't going to work. So now you're going to need to define statics/singletons to provide access to stuff so the attribute can use them. It'll get complicated.

The best approach, in my opinion, is to use a context and have each method check the context if it needs to enforce permissions. This is assuming you're not already using a DI container or something where you could also put the equivalent logic. In general permission checks should occur high up in the architecture so technically you're UI or controller should handle security and the rest of the your business logic should not worry about it. But a context type can be passed to methods to allow you to "check" permissions in the business layer if you need it. A context could be something as simple as the IPrincipal tied to the thread running the code or it could be a custom type that you've defined to store data that doesn't make sense as part of the user principal.


这篇关于如何在Windows窗体应用程序项目中调用类上的任何方法之前调用特定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆