Skip to main content
added 1 character in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

How to refactor this property's code? View model with properties that represent years and months

I have a view model with properties that represent years and months:

public IEnumerable<SelectListItem> Years
{
    get
    {
        return new SelectList(
            Enumerable.Range(1900, 112)
            .OrderByDescending(year => year)
            .Select(year => new SelectListItem
            {
                Value = year.ToString(CultureInfo.InvariantCulture),
                Text = year.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

public IEnumerable<SelectListItem> Months
{
    get
    {
        return new SelectList(
            Enumerable.Range(1, 12)
            .OrderByDescending(month => month)
            .Select(month => new SelectListItem
            {
                Value = month.ToString(CultureInfo.InvariantCulture),
                Text = month < 10 ? string.Format("0{0}", month) : month.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

Definitely I definitely use copy & paste approach here) How. How can I refactor this code? MabbyMaybe somehow passing the numbers as parameters to some helper method?

How to refactor this property's code?

I have a view model with properties that represent years and months:

public IEnumerable<SelectListItem> Years
{
    get
    {
        return new SelectList(
            Enumerable.Range(1900, 112)
            .OrderByDescending(year => year)
            .Select(year => new SelectListItem
            {
                Value = year.ToString(CultureInfo.InvariantCulture),
                Text = year.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

public IEnumerable<SelectListItem> Months
{
    get
    {
        return new SelectList(
            Enumerable.Range(1, 12)
            .OrderByDescending(month => month)
            .Select(month => new SelectListItem
            {
                Value = month.ToString(CultureInfo.InvariantCulture),
                Text = month < 10 ? string.Format("0{0}", month) : month.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

Definitely I use copy & paste approach here) How can I refactor this code? Mabby somehow passing the numbers as parameters to some helper method?

View model with properties that represent years and months

I have a view model with properties that represent years and months:

public IEnumerable<SelectListItem> Years
{
    get
    {
        return new SelectList(
            Enumerable.Range(1900, 112)
            .OrderByDescending(year => year)
            .Select(year => new SelectListItem
            {
                Value = year.ToString(CultureInfo.InvariantCulture),
                Text = year.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

public IEnumerable<SelectListItem> Months
{
    get
    {
        return new SelectList(
            Enumerable.Range(1, 12)
            .OrderByDescending(month => month)
            .Select(month => new SelectListItem
            {
                Value = month.ToString(CultureInfo.InvariantCulture),
                Text = month < 10 ? string.Format("0{0}", month) : month.ToString(CultureInfo.InvariantCulture)
            }
        ), "Value", "Text");
    }
}

I definitely use copy & paste approach here. How can I refactor this code? Maybe somehow passing the numbers as parameters to some helper method?

Rollback to Revision 2
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Edits:

Besides, I have other SelectListItem with strings, so I decided to create new static class with static method to handle both cases: strings and numbers. Here is my code:

public static class SelectListHelper
{
    public static IEnumerable<SelectListItem> GetSelectList(IEnumerable<string> list)
    {
        return new SelectList(
            list.Select(i => new SelectListItem
            {
                Value = i,
                Text = i
            }
                ), "Value", "Text");
    }
}

And the code from model:

private IEnumerable<string> _years = GetEnumerableRange(1900, 113);

private IEnumerable<string> GetEnumerableRange(int start, int count)
    {
        return Enumerable.Range(start, count)
                         .Reverse()
                         .Select(i => i.ToString(CultureInfo.InvariantCulture));
    }

So, now I can do the following:

public IEnumerable<SelectListItem> Years
    {
        get { return SelectListHelper.GetSelectList(_years); }
    }

public IEnumerable<SelectListItem> Colors
    {
        // _colors is of type List<string>
        get { return SelectListHelper.GetSelectList(_colors); }
    }

I decided to not give a f**k about {0:00} format)

Edits:

Besides, I have other SelectListItem with strings, so I decided to create new static class with static method to handle both cases: strings and numbers. Here is my code:

public static class SelectListHelper
{
    public static IEnumerable<SelectListItem> GetSelectList(IEnumerable<string> list)
    {
        return new SelectList(
            list.Select(i => new SelectListItem
            {
                Value = i,
                Text = i
            }
                ), "Value", "Text");
    }
}

And the code from model:

private IEnumerable<string> _years = GetEnumerableRange(1900, 113);

private IEnumerable<string> GetEnumerableRange(int start, int count)
    {
        return Enumerable.Range(start, count)
                         .Reverse()
                         .Select(i => i.ToString(CultureInfo.InvariantCulture));
    }

So, now I can do the following:

public IEnumerable<SelectListItem> Years
    {
        get { return SelectListHelper.GetSelectList(_years); }
    }

public IEnumerable<SelectListItem> Colors
    {
        // _colors is of type List<string>
        get { return SelectListHelper.GetSelectList(_colors); }
    }

I decided to not give a f**k about {0:00} format)

passing IEnumerable<string> to helper method
Source Link

Edits:

Besides, I have other SelectListItem with strings, so I decided to create new static class with static method to handle both cases: strings and numbers. Here is my code:

public static class SelectListHelper
{
    public static IEnumerable<SelectListItem> GetSelectList(IEnumerable<string> list)
    {
        return new SelectList(
            list.Select(i => new SelectListItem
            {
                Value = i,
                Text = i
            }
                ), "Value", "Text");
    }
}

And the code from model:

private IEnumerable<string> _years = GetEnumerableRange(1900, 113);

private IEnumerable<string> GetEnumerableRange(int start, int count)
    {
        return Enumerable.Range(start, count)
                         .Reverse()
                         .Select(i => i.ToString(CultureInfo.InvariantCulture));
    }

So, now I can do the following:

public IEnumerable<SelectListItem> Years
    {
        get { return SelectListHelper.GetSelectList(_years); }
    }

public IEnumerable<SelectListItem> Colors
    {
        // _colors is of type List<string>
        get { return SelectListHelper.GetSelectList(_colors); }
    }

I decided to not give a f**k about {0:00} format)

Edits:

Besides, I have other SelectListItem with strings, so I decided to create new static class with static method to handle both cases: strings and numbers. Here is my code:

public static class SelectListHelper
{
    public static IEnumerable<SelectListItem> GetSelectList(IEnumerable<string> list)
    {
        return new SelectList(
            list.Select(i => new SelectListItem
            {
                Value = i,
                Text = i
            }
                ), "Value", "Text");
    }
}

And the code from model:

private IEnumerable<string> _years = GetEnumerableRange(1900, 113);

private IEnumerable<string> GetEnumerableRange(int start, int count)
    {
        return Enumerable.Range(start, count)
                         .Reverse()
                         .Select(i => i.ToString(CultureInfo.InvariantCulture));
    }

So, now I can do the following:

public IEnumerable<SelectListItem> Years
    {
        get { return SelectListHelper.GetSelectList(_years); }
    }

public IEnumerable<SelectListItem> Colors
    {
        // _colors is of type List<string>
        get { return SelectListHelper.GetSelectList(_colors); }
    }

I decided to not give a f**k about {0:00} format)

Tweeted twitter.com/#!/StackCodeReview/status/294169627515301888
rm tag from title
Link
svick
  • 24.3k
  • 4
  • 52
  • 89
Loading
Source Link
Loading