1

I am having trouble with avoiding repeating parts in methods. I have class with multiple methods. I have some kind of validation method that should be invoked in the begining of each method or before each method is invoked. For exmple:

public class TestClass
{
     public Test(){}
     private void Validate(){ // some code }

public IList<User> GetUsers()
{
  Validate();
//other content
}

public IList<Product> GetProducts()
{
  Validate();
//other content
}


public IList<Role> GetRoles()
{
  Validate();
//other content
}

public Product CreateProduct()
{
  Validate();
//other content
}

}

Is there better approach for doing that instead of invoking Validate() in each method?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Gamaboy
  • 117
  • 11
  • These methods are not in controller – Gamaboy Aug 16 '17 at 13:50
  • Then this has nothing to do with ASP.NET MVC and it's just a c# question. Look up aspect-oriented programming which aims to solve exactly this – Camilo Terevinto Aug 16 '17 at 13:51
  • Possible duplicate of [How to call a method implicitly after every method call?](https://stackoverflow.com/questions/33075283/how-to-call-a-method-implicitly-after-every-method-call) – Drew Kennedy Aug 16 '17 at 13:56
  • I don't understand why getter methods require validation since the data in the instance _shouldn't change_ and we can assume it has already been validated. – John Alexiou Aug 16 '17 at 14:27

0 Answers0