如何调用每一个方法调用后隐含的方法? [英] How to call a method implicitly after every method call?

查看:103
本文介绍了如何调用每一个方法调用后隐含的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,该职位了不起的标题。我有点好奇,想知道如果下面的问题没有任何解决方案或没有。这种情况是我有一个名为 SaveSecurity()函数; ,我需要每一个功能之后调用。象下面这样:

 公共无效ADDUSER(字符串ID,字符串名称,字符串密码)
{
/ //一些代码
SaveSecurity();
}
公共无效DeleteUser(用户ObjUser)
{
///一些代码
SaveSecurity();
}
公共无效AddPermission(字符串ID,字符串名称,接入类型接入)
{
///一些代码
SaveSecurity();
}
公共无效DeletePermission(许可ObjPermission)
{
///一些代码
SaveSecurity();
}
公共无效AddRole(字符串ID,字符串名称)
{
Roles.AddRole(ID,姓名);
SaveSecurity();
}
公共无效SaveSecurity()
{
///保存数据
}

等等。因此,如果我们看有一个相似的所有功能,在去年它的函数结束后呼吁 SaveSecurity()。我的问题是:




有没有办法来调用与出每一个功能一次又一次写在同一行后此功能。


我的类图看起来像这样




解决方案

您需要寻找到存储库模式,



单独的课程和有业务,



创建另一个层(称之为业务层)或任何将被调用不同类的不同的方法...



ATM您要遵循OOP,但你正在做的是函数式编程。



在ASP.NET MVC应用程序



实施的工作模式的信息库和单位

修改添加类图后



您的集合类实际上是库类,您需要将您的方法,如deletePermissions,deleteRole到有各自的储备库类像permissionsRepo(把它命名为收藏,如果你想),并roleRepo ..



所以,你已经有一个对象类和库类对象(可以在一起),但我喜欢让他们分开,repostory班会做他们需要做什么,喜欢..

  //进行更改DB 
//自行更改广告
//进行更改Web服务等...

您的经理类可能dulicate库类的方法,但他们只会叫他们,

  PermissionManager.DeletePermissions(PermissionObject) ; 

然后在PermissionManager类,你将有法,

  DeletePermissions(权限pObject)
{
PermissionRepo.Delete(pObject);
}



以上只是添加图层使你的代码看起来更具可读性,面向未来在很短的时间,但如果你有更多的时间投入你可以看看观察者模式太...



在C#中实现Observer模式



每一次你的对象改变它的状态你可以调用SaveSecurity方法(这将是另一个类(命名为改变可能),如果你不想叫 SaveSecurity 为对象的每个变化,则可以添加一个属性到对象如 IsSecurityChanged ?如果是的话,然后调用SaveSecurity。



更多的解释,但如果你看看Observer模式上面,你会得到一个想法。



还有一个方式,但我不会亲自推荐的,使用IDisposable接口,然后在Dispose方法调用SaveSecurity方法对象。但其不推荐使用ME。


Sorry for the terrific Title for the post. I am bit curious to know if below problem does have any solutions or not. The situation is I have a function called SaveSecurity(); which I need to call after every function. Like below:

public void AddUser(string ID, string Name, string Password)
{
    ///some codes
    SaveSecurity();
}
public void DeleteUser(User ObjUser)
{
    ///some codes
    SaveSecurity();
}
public void AddPermission(string ID, string Name, AccessType Access)
{
    ///some codes
    SaveSecurity();
}
public void DeletePermission(Permission ObjPermission)
{
    ///some codes
    SaveSecurity();
}
public void AddRole(string ID, string Name)
{
    Roles.AddRole(ID, Name);
    SaveSecurity();
}
public void SaveSecurity()
{
    ///Saves the data
}

And many more. So now if we look there is a similarity to all the function is that at last it calls for the SaveSecurity() after the end of the function. My question is:

Is there a way to call this function after every function with out writing the same line again and again?

My Class Diagram looks like this

解决方案

You need to look into repository pattern,

Seperate your classes and there operations,

Create another layer (call it business layer) or whatever which will be calling different methods of different classes...

ATM you are trying to follow OOP but all you are doing is functional programming..

Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application

Edit After adding class diagram

Your collection classes are actually repository class, you will need to move your methods like deletePermissions, deleteRole to there respective repository classes like permissionsRepo (keep it named as collections if you want) and roleRepo..

So you already have an object class and a repository class of object (can be together) but I like to keep them separate, repostory classes will do what they need to do, like..

// Make changes to DB
// Make changes to AD
// Makes changes to web services etc...

Your manager class may dulicate methods of repository classes but they will only calling them,

PermissionManager.DeletePermissions(PermissionObject);

Then in PermissionManager Class you will have method,

DeletePermissions(Permissions pObject)
{
     PermissionRepo.Delete(pObject);
}

Above is just adding a layer to make your code look more readable and future proof in very short time, but if you have more time to invest you can look into Observer pattern too...

Implement Observer pattern in C#

Each time your object changes it's state you can call SaveSecurity method (which will be in another class (Name it Changes maybe). If you don't want to call SaveSecurity for each change of object, you can add a property to your object e.g. IsSecurityChanged ? if yes then call SaveSecurity.

More to explain but if you look at Observer pattern above you will get an idea.

One more way but I won't personally recommend is, to use IDisposable interface, then in dispose method call SaveSecurity method for the object. BUT ITS NOT RECOMMENDED BY ME.

这篇关于如何调用每一个方法调用后隐含的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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