3

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
        }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91

3 Answers3

2

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)

mfeingold
  • 7,094
  • 4
  • 37
  • 43
1

There should be no logic in the controller. Read this for example: Where should I put my controller business logic in MVC3

or this one: https://softwareengineering.stackexchange.com/questions/165444/where-to-put-business-logic-in-mvc-design

I know it's tempting but the less logic you put there the best you will find yourself in the future.

the answer in my opinion is:

You should read your property in a business layer benhead the controller and pass it all the way up to the view in a model object.

Community
  • 1
  • 1
Maurizio In denmark
  • 4,226
  • 2
  • 30
  • 64
1

I agree with Maurizio in general that all business logic should be in some service/business logic layer. However in this case since you're only fetching a value from web.config whether, in your controller action, you do:

var someValue = App.TaxBillLink;

or you do:

var someValue = _linkService.GetTodaysLink();

really doesn't matter much unless there is some sort of logic there that needs to be unit tested.

Marko
  • 12,543
  • 10
  • 48
  • 58