Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:43:54+00:00 2026-06-05T03:43:54+00:00

I have a class public class MyService { public IList<Exception> ExList {get; private set;}

  • 0

I have a class

public class MyService  
{  
   public IList<Exception> ExList {get; private set;}

   public bool HasErrors { get { return ExList.Count > 0; } }

   public MyMethod()
   {
       ExList.Clear();
       //- do some logic ---
   }

}

And I want to call MyMethod() and check if errors was occured. Something like this

var service = new MyService();

service.MyMethod();

if(service.HasErrors)
{
    // - do some logic
}

service.MyMethod();

if(service.HasErrors)
{
    // - do some logic
}

But I have to write “ExList.Clear();” row manualy for every method in MyService class.
And the question is – Are any solution to avoid this?

I need something like

public class MyService  
{  
   public IList<Exception> ExList {get; private set;}

   public bool HasErrors { get { return ExList.Count > 0; } }

   private void Precondition()
   {
       ExList.Clear();
   }

   public MyMethod()
   {           
       //- do some logic ---
   }

}

And Precondition() would be invoked automatically for each method’s call.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:43:55+00:00Added an answer on June 5, 2026 at 3:43 am

    Another tool you could look into is PostSharp. PostSharp is an AOP frawework that allows you to define custom aspects (attributes).

    You’ll be interested in OnMethodBoundaryAspect. Something like below should do the trick. args.Instance will be “the instance on which the method is being executed”

    public class ClearListPrecondition : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            MyService service = args.Instance as MyService;
            if (service == null)
            {
                throw new InvalidOperationException(
                  "This aspect can only execute on types of MyService");
            }
    
            service.ExList.Clear();
            base.OnEntry(args);
        }
    }
    

    You would then decorate your service methods with this aspect:

    [ClearListPrecondition]
    public void MyMethod()
    {
    }
    

    If you choose to take the path of AOP for such a trivial task, I strongly recommend you re-thinking your design.

    Firstly, throwing exceptions should be preferred, but if you insist, you could return a result from your method.

    E.g.

    public MethodResult MyMethod()
    {
        ....
         if(errorHasOccured)
         {
              return new MethodResult() {Exceptions = exception};
         }
        ....
         return new MethodResult() {ResultOfMethod = ...};
    }
    
    public class MethodResult
    {
       public IList<Exception> Exceptions {get; set;}
       public bool HasErrors { get { return ExList.Count > 0; } } 
       public string ResultOfMethod {get;set;}
    
    }
    

    Your consumers can check the result of a method call:

    var service = new MyService();
    
    var result1 = service.MyMethod();
    
    if(result1.HasErrors)
    {
        // - do some logic
    }
    
    var result2 = service.MyMethod();
    
    if(result2.HasErrors) 
    {
        // - do some logic
    }
    

    This removes the need to reset the state of your service as it becomes stateless (and therefore threadsafe)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class: public class CarList { public int quantity{get;set;} public List<Car> Cars
I have class Student { public List<Degree> Degrees {get;set;} } class Degree { public
I have the following code: @Name(myService) @Scope(ScopeType.APPLICATION) @Stateless public class MyService { private Service
Hi I was thinking about such case: public class MyService { private IList<Entity> data;
I have a service class : public class MyService : IService { private IRepoA
I have this class: public class Friend { private String name; private String location;
I have a class: public class MyClass { public int MyMethod() { Random rand
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following code: public interface IService { } public class MyService :
Given I have a Spring bean configured as @Service(myService) public class DefaultService extends MyService

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.