Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method implicitly after every method call?

Tags:

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

like image 620
Shweta Pathak Avatar asked Oct 12 '15 07:10

Shweta Pathak


People also ask

How do you call a method directly?

We can call a static method by using the ClassName. methodName. The best example of the static method is the main() method. It is called without creating the object.

Can you call a method within itself?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can you call a method more than once?

Yes, you can call multiple methods, one after the other in the same statement. This is sometimes referred to as “method chaining”.

Can you call a method within a method?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.


1 Answers

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.

like image 122
Mathematics Avatar answered Oct 11 '22 20:10

Mathematics