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 7619259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:38:30+00:00 2026-05-31T03:38:30+00:00

I have to create a bunch of methods that look like this. The things

  • 0

I have to create a bunch of methods that look like this. The things that change will be the method name, the return type and the lines marked in the middle – the rest will be the same. Is there a clean way to refactor this so that I don’t repeat myself?

private bool CanPerform(WindowsIdentity identity, string applicationName, int operation)
{
    IAzApplication3 application = null;
    IAzClientContext3 context = null;
    try
    {
        application = this.store.OpenApplication(applicationName, null) as IAzApplication3;

        ulong token = (ulong)identity.Token.ToInt64();
        context = application.InitializeClientContextFromToken(token, null) as IAzClientContext3;

        // lines that change go here
    }
    catch (COMException e)
    {
        throw new SecurityException(string.Format("Unable to check operation '{0}'", operation), e);
    }
    finally
    {
        Marshal.FinalReleaseComObject(context);
        Marshal.FinalReleaseComObject(application);
    }
}

I realise this is probably basic stuff but I work alone so there’s no one else to ask.

  • 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-05-31T03:38:32+00:00Added an answer on May 31, 2026 at 3:38 am

    It sounds like a delegate would be appropriate here, with a generic method to cover the return type changing:

    private T ExecuteWithIdentity<T>(WindowsIdentity identity,
        string applicationName, int operation,
        Func<IAzApplication3, IAzClientContext3, T> action)
    {
        IAzApplication3 application = null;
        IAzClientContext3 context = null;
        try
        {
            application = this.store.OpenApplication(applicationName, null) as IAzApplication3;
    
            ulong token = (ulong)identity.Token.ToInt64();
            context = application.InitializeClientContextFromToken(token, null) as IAzClientContext3;
    
            return action(application, context);
        }
        catch (COMException e)
        {
            throw new SecurityException(
                string.Format("Unable to check operation '{0}'", operation), e);
        }
        finally
        {
            Marshal.FinalReleaseComObject(context);
            Marshal.FinalReleaseComObject(application);
        }
    }
    

    Then you put the code for each check in a separate method, or even just use a lambda expression:

    bool check = ExecuteWithIdentity(identity, "Foo", 10,
                             (application, context) => context != null);
    

    or

    string check = ExecuteWithIdentity(identity, "Foo", 10, SomeComplexAction);
    
    ...
    private static string SomeComplexAction(IAzApplication3 application,
                                            IAzClientContext3 context)
    {
        // Do complex checks here, returning whether the user is allowed to
        // perform the operation
    }
    

    You may want to change the delegate type of course – it’s not clear what operation is meant to be used for, for example.

    I would also strongly consider casting instead of using as. If the application or context is returned from OpenApplication/InitializeClientContextFromTokenas a non-null value which just isn’t the right type, do you really want to handle that the same was as a null value being returned?

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

Sidebar

Related Questions

I have a bunch of annotated, interrelated POJOs that I want to create/modify/search over
Let's say we have a class that looks like this: class A { public:
I have a bunch of methods that make use of jquery to basically generate
I want to create a helper method that can take in ANY type of
I have a bunch of Repository classes which all look a bit like the
After I have created a serious bunch of classes (with initialize methods), I am
I have used the Java Wireless Toolkit Stub Generator to create a bunch of
I have some code that creates a bunch of UITextViews and puts them into
I have a bunch of MDI child nodes that are all created in the
I have a collection of objects that I would like to iterate through to

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.