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

yguyon28
yguyon28 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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?

Author

Commented:
I would be using them in my service because they are configuration type files,
Senior Developer
Commented:
hmm, did you want to write more?

Otherwise, I referred to your naming: ParameterConfigurationService.GetParameters();

Why is there a parameter service at all?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial