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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:19:49+00:00 2026-05-17T23:19:49+00:00

In the method below there are numerous case statements (many have been removed) that

  • 0

In the method below there are numerous case statements (many have been removed) that make calls to Manager classes. For example, the first one calls ApplicationManager.GetByGUID. Any time a “manager” class is used, security checks occur.

Problem: I have entities that may be permitted to some of these but not all. So when this method gets run, if one of them craps out it’ll throw a security exception and crash the whole report.

Someone has suggested to me that I could just throw try-catch blocks around each case but the more I read the more I feel like that might be sloppy. I admittedly am not very knowledged about exceptions…I was hoping someone could suggest a way to do this with more finesse…I need to be able to get back good data and ignore the ones that throw security exceptions….or maybe try-catches are ok in this case?

Hope that makes sense…thanks


private string GetLookup(string value, string type)
{
    MySqlConnection mconn = new MySqlConnection(ConfigurationSettings.AppSettings["UnicornConnectionString_SELECT"]);

    try
    {
        mconn.Open();

        lock (reportLookups)
        {
            if (reportLookups.ContainsKey(type+value))
                return reportLookups[type+value].ToString();
            else if (reportLookups.ContainsKey(value))
                return reportLookups[value].ToString();
            else
            {
                switch (type)
                {
                    case "ATTR_APPLICATIONNAME":
                        if (value != Guid.Empty.ToString())
                        {
                            reportLookups.Add(type + value, applicationManager.GetByGUID(value).Name);
                        }
                        else
                        {
                            reportLookups.Add(type + value, "Unknown");
                        }
                        mconn.Close();
                        return reportLookups[type + value].ToString();
                        break;
                    case "ATTR_CITYNAME":
                        reportLookups.Add(type + value, UMConstantProvider.UMConstantProvider.GetConstant<UMString64>(int.Parse(value), UMMetricsResourceLibrary.Enumerations.ConstantType.CITY_NAME, ref mconn));
                        mconn.Close();
                        return reportLookups[type + value].ToString();
                        break;
                    case "ATTR_COUNTRYNAME":
                        reportLookups.Add(type + value, UMConstantProvider.UMConstantProvider.GetConstant<UMString2>(int.Parse(value), UMMetricsResourceLibrary.Enumerations.ConstantType.COUNTRY_NAME, ref mconn));
                        mconn.Close();
                        return reportLookups[type + value].ToString();
                        break;
                    case "ATTR_ITEMDURATION":

                        MediaItem mi = mediaItemManager.GetMediaItemByGUID(value);
                        if (mi.MediaItemTypeID == (int)MediaItemType.ExternalVideo || mi.MediaItemTypeID == (int)MediaItemType.ExternalAudio)
                        {
                            reportLookups.Add(type + value, mediaItemManager.GetMediaItemByGUID(value).ExternalDuration);
                            mconn.Close();
                            return reportLookups[type + value].ToString();
                        }
                        else
                        {

                            List<BinaryAsset> bins = fileSystemManager.GetBinaryAssetsByMediaItemGuid(value, mi.DraftVersion);
                            var durationasset = from d in bins
                                                where d.Duration != 0
                                                select d.Duration;

                            if (durationasset.Count() > 0)
                            {

                                reportLookups.Add(type + value, durationasset.ToList()[0]);

                            }
                            else
                            {
                                reportLookups.Add(type + value, 0);
                                mconn.Close();
                                return reportLookups[type + value].ToString();
                            }


                        }

                        break;
                }
            }
            return string.Empty;
        }
    }
    finally 
    {
        mconn.Close();
    }
}
  • 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-17T23:19:50+00:00Added an answer on May 17, 2026 at 11:19 pm

    As a rule, Exceptions should indicate that something went wrong. If you’re expecting exceptions during the course of a typical run through this method, you should change your APIs to allow you to avoid that exception:

    if (mediaItemManager.CanAccessMediaItem(value))
    {
        MediaItem mi = mediaItemManager.GetMediaItemByGUID(value);
        ....
    }
    

    Here’s a quick attempt on my part to refactor this code into something more reasonable:

    private string GetLookup(string value, string type)
    {
        var lookupKey = type + value;                        
        using (MySqlConnection mconn = new MySqlConnection(ConfigurationSettings.AppSettings["UnicornConnectionString_SELECT"]))
        {
            mconn.Open();
            lock (reportLookups)
            {
                if (reportLookups.ContainsKey(lookupKey))
                {
                    return reportLookups[lookupKey].ToString();
                }
                var value = GetLookupValue(type, value);
                reportLookups[lookupKey] = value;
                return value;
            }
        }
    }
    
    private string GetLookupValue(string type, string value)
    {
        switch (type)
        {
            case "ATTR_APPLICATIONNAME":
                return value == Guid.Empty.ToString() 
                    ? "Unknown"
                    : applicationManager.CanGetByGUID(value)
                        ? applicationManager.GetByGUID(value).Name
                        : string.Empty;
            case "ATTR_CITYNAME":
                return UMConstantProvider.UMConstantProvider.GetConstant<UMString64>(int.Parse(value), UMMetricsResourceLibrary.Enumerations.ConstantType.CITY_NAME, ref mconn);
            case "ATTR_COUNTRYNAME":
                return UMConstantProvider.UMConstantProvider.GetConstant<UMString2>(int.Parse(value), UMMetricsResourceLibrary.Enumerations.ConstantType.COUNTRY_NAME, ref mconn);
            case "ATTR_ITEMDURATION":
                if(mediaItemManager.CanGetMediaItemByGUID(value)) {
                    MediaItem mi = mediaItemManager.GetMediaItemByGUID(value);
                    if (mi.MediaItemTypeID == (int)MediaItemType.ExternalVideo || mi.MediaItemTypeID == (int)MediaItemType.ExternalAudio)
                    {
                        return mediaItemManager.GetMediaItemByGUID(value).ExternalDuration;
                    }
                    else
                    {
                        List<BinaryAsset> bins = fileSystemManager.GetBinaryAssetsByMediaItemGuid(value, mi.DraftVersion);
                        var durationasset = from d in bins
                                            where d.Duration != 0
                                            select d.Duration;
                        return durationasset.FirstOrDefault() ?? "0";
                    }
                }
                else 
                {
                    return string.Empty;
                }
            default:
                return string.Empty;
        }
    }
    

    Since I don’t understand the full scope of this code, I probably oversimplified some aspects of it, but you can see that there is a lot of refactoring to be done here. In the future, you might want to run some code by http://refactormycode.com/, until you get accustomed to using best practices.

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

Sidebar

Related Questions

Why does the first method below work? I would have thought that the variable
Given the jQuery dropdown plugin below. Is there a way to add a method
Consider the Java code below, what would happen if there were no paintComponent method
I have the method below, which in my Blackjack app will get the value
Hi I have this method below which should insert values into my database. However
The sample method below is intended to detect whether or not it has been
Hey all, I seem to have these types of methods everywhere. The method below
I have an error for this method below def mymethod cat = Cat.find_by_cat_id_and_animal_id_and_name(catid, haustierid,
** Update I have a solution below, but it's rather manual... if there is
In the method below is there a way to know if the type T

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.