Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...