Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

c# - How to call a method implicitly after every method call?

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...