Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

Creating a service, Should I include my method in there.

I running into a pittfall I have an action that I'm trying to refactor so that it call a service that will populate a model that I created.

 My only issue is that I have a bunch of method that I'm not sure if they should belong to the service or I just should leave them on the controller level. My assumption is to not implement these method in my new service that I will create.  The methods that I'm referring to are as follow:
 PopulateSecurityParameters(parameters, viewModel);
  PopulateCancelSoldParameters(parameters, viewModel);
  PopulateCallLogParameters(parameters, viewModel);

[  private ViewModel GetViewModel(Employee employee)
        {
            var viewModel = new ViewModel();
            var parameters = ParameterConfigurationService.GetParameters();

            viewModel.CommonSmtpSettingsOptions = GetCommonSmtpSettingsOptions();

            viewModel.StatusInfoList = GetStatusInfoList(parameters);

            PopulateSecurityParameters(parameters, viewModel);
            PopulateCancelSoldParameters(parameters, viewModel);
            PopulateCallLogParameters(parameters, viewModel);
            PopulateGeneralParameters(parameters, viewModel);
            PopulateAppointmentCallParameters(parameters, viewModel);
            PopulateSmtpParameters(viewModel);

            viewModel.CountryOptions = GetViewModelCountryOptions(viewModel.SelectedCountry);
            viewModel.QuickNoteOptions = GetViewModelQuickNoteOptions(viewModel.SelectedQuickNote);

            return viewModel;
        }
/code]


 [code]  private List<Item> GetViewModelCountryOptions(char? selectedCountry)
        {
            var items = new List<Item>();
            var country = CountryCodes.Country.UnitedStates;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });
            country = CountryCodes.Country.Canada;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });
            country = CountryCodes.Country.UK;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });
            country = CountryCodes.Country.Australia;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });
            country = CountryCodes.Country.Ireland;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });
            country = CountryCodes.Country.France;
            items.Add(new Item
            {
                Text = country.GetText(),
                Value = country.GetCode().ToString(),
                IsSelected = selectedCountry == country.GetCode()
            });

            return items;
        }

Open in new window

Avatar of ste5an
ste5an
Flag of Germany image

The Populate* methods belong to the same "level" as your ViewModel class. Cause they are coupled to it.

Patternwise they are part of the builder/factory (method) to create the ViewModel. So when you refactor it, create a builder or factory and place them there.

The only interesting point is your parameters service. This has a code smell. What are these parameters? Why are you using a service to get them? Are they configuration values or a kind of user context?
Avatar of Stacie

ASKER

I would be using them in my service because they are configuration type files,
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial