Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Controllers read custom app settings from web.config?

In my mvc application during certain times of the year we want to show one of two links. Basically I have to switch the link when I get a call from management. So, I thought instead of having to recompile the app I would add a custom app setting to the web.config file. Then I created a wrapper so that it is strongly typed. Now, my problem is I don't know where to execute the logic. Should add a property to my view model and set it in the controller based on the configuration setting value? Or should I read it directly in my View and toggle between the two links? I'm pretty sure this only belongs in the view or the controller, and not the service layer, since it is used specifically for UI stuff.

Details.cshtml //current code

 @if(Search.App.ParcelDetailDisplayMode == Search.App.DisplayMode.Tax ){
     <a id="tax-link" href="@taxlink" title="View Tax Bill on Tax Collectors Website">Tax Bill</a>
 }
 else if(Search.App.ParcelDetailDisplayMode == Search.App.DisplayMode.Trim ){
        <a id="trim-link" href="@trimlink" title="View your TRIM notice online">Trim Notice</a>
 }  

web.config

<add key="ParcelDetailDisplayMode" value="Tax"/>

config wrapper

namespace Search
{
    /// <summary>
    /// The app.
    /// </summary>
    public static class App
    {
        /// <summary>
        /// Gets the tax bill link.
        /// </summary>
        public static string TaxBillLink
        {
            get
            {
                return ConfigurationManager.AppSettings["TaxBillLink"];
            }
        }

        /// <summary>
        /// Gets the trim notice link.
        /// </summary>
        public static string TrimNoticeLink
        {
            get
            {
                return ConfigurationManager.AppSettings["TrimLink"];
            }
        }

        /// <summary>
        /// Gets the map link.
        /// </summary>
        public static string MapLink
        {
            get
            {
                return ConfigurationManager.AppSettings["MapLink"];
            }
        }

        /// <summary>
        /// Gets the update address link.
        /// </summary>
        public static string UpdateAddressLink
        {
            get
            {
                return ConfigurationManager.AppSettings["UpdateAddressLink"];
            }
        }

        /// <summary>
        /// Gets the release name.
        /// </summary>
        public static string ReleaseName
        {
            get
            {
                return ConfigurationManager.AppSettings["ReleaseName"];
            }
        }

        /// <summary>
        /// Gets the parcel detail display mode.
        /// </summary>
        public static DisplayMode ParcelDetailDisplayMode
        {
            get
            {
                var r = DisplayMode.Tax;
                DisplayMode.TryParse(ConfigurationManager.AppSettings["ParcelDetailDisplayMode"], out r);
                return r;
            }
        }

        /// <summary>
        /// The display mode.
        /// </summary>
        public enum DisplayMode
        {
            /// <summary>
            /// The trim.
            /// </summary>
            Trim, 

            /// <summary>
            /// The tax.
            /// </summary>
            Tax
        }
    }
}
like image 669
Doug Chamberlain Avatar asked Dec 06 '25 15:12

Doug Chamberlain


1 Answers

I would say it does not really matter. Adding it as a property of your model feels to give a little bit more separation.

What does matter though is that your wrapper is static. This will make it really difficult to mock it for the purpose of unit testing (or any other purpose)

like image 149
mfeingold Avatar answered Dec 08 '25 03:12

mfeingold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!