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

  • Home
  • SEARCH
  • 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 8148125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:35:10+00:00 2026-06-06T14:35:10+00:00

How to avoid a pair of repetitive lines before and after invocations in sample

  • 0

How to avoid a pair of repetitive lines before and after invocations in sample below ?
Details: This is compileable mock of what is real larger code. Generally it is a layer of proxy classes containing service clients with variety of APIs. The repetitive part is pre- and post- invocation for every method of every client. Unfortunately there is no single signature for all possible methods, the pre- and post- parts need a pointer to client’s channel and context.
Is it possible to apply something advanced like AOP, Generics, Delegates, Attributes etc. ? Thank you

using System;

namespace ConsoleApplication
{
class ClassServiceClient: IDisposable
{ 
    public Object channel()
    {
        return "something";
    }

    public Object context()
    {
        return "something other";
    }
}

class ClassA : ClassServiceClient
{
    public Object methodA()
    {
        return "something other";
    }
}

class ClassB : ClassServiceClient
{
    public void methodB(string param)
    {
        return;
    }
}

class ClassAProxy 
{
    public Object methodA()
    {
        using (ClassA client = new ClassA())         
        {
            Program.preparation(client.channel());   //<---- repetitive part
            Object result = client.methodA();
            Program.postinvocation(client.context());//<---- repetitive part
            return result;
        }
    }
}

class ClassBProxy
{
    public void methodB(string param)
    {
        using (ClassB client = new ClassB())
        {
            Program.preparation(client.channel());   //<---- repetitive part
            client.methodB(param);
            Program.postinvocation(client.context());//<---- repetitive part
            return;
        }
    }
}


class Program
{
    public static void preparation(Object channel)
    {
        // Do something with channel
    }

    public static void postinvocation(Object context)
    {
        // Do something with context
    }

    static void Main(string[] args)
    {

    }
}
}
  • 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-06T14:35:12+00:00Added an answer on June 6, 2026 at 2:35 pm

    If you can use a common base class, you can easily use a public sealed method that does the invocation and a protected abstract method that does the logic, e.g.

    class ProxyBase{
        public void Method(params object[] args){
            PreConditions();
            Invoke(args);               
            PostConditions();
        }
    
        protected abstract void Invoke(object[] args);
    }
    
    class ClassAProxy{
        protected override void Invoke(object[] args){
            client.Method(args[0]);
        }
    }
    

    You can achieve similar results functionally by declaring a InvocationHandler in your Program class that takes an action:

    class Program{
        public static void Invoke(object[] args, Action action){
            PreConditions();
            action();
            PostConditions();
        }
    }
    
    class ClassAProxy{
        public void MethodA(int i){
            Program.Invoke(() => client.Something(i));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to avoid this scary messages when users install my application: I understand
how to avoid mysql doing this (by itself or php script) , even if
I am trying to avoid using intents and activities within tabhost and tabwidget This
To avoid the long class path issue in windows, i modified my build.xml like[1]
To avoid crashing anything I specify the version number for every gem in my
I'd like to avoid doing any needless HTTP requests on my site to improve
I am trying to avoid C6011 warning because an abort function is calling exit().
in order to avoid copy/paste, i can use a unique view for different actions,
How can I avoid NullPointerExceptions ? I have tried using try-catch blocks but that
Also trying to avoid any use of plugins, (apart from jQuery of course, whether

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.