Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1 - You could add an extension method to <code>IEnumerable&lt;Employee&gt;</code> or <code>IQueryable&lt;Employee&gt;</code> that returns your <code>GenericSelectList</code>. Advantages - you can re-use against any collection of employee, i.e. a filtered list, and quite nice calling syntax; you can customise how the select list is formed on a case by case basis. Disadvantages - you'll have to write one of these methods for each type.</p> <p>2 - You could change that extension method to work with a generic, that works against <code>IEnumerable&lt;T&gt;</code> and produces a <code>GenericSetList</code> based on <code>Expression</code> inputs. Advantages - Your method is now truly generic, i.e. write once and re-use. You could do this in such a way that it combines with 1 so that your 1-per-class function uses the generic to save duplication. Disadvantages - assumes comfort in using expressions and the like.</p> <p>3 - You could have factory methods that return ViewModels.</p> <p>These things can also work in combination as well, and should help remove copy/paste code and promote re-use and testability.</p> <p>Edit - here is one way that no. 2 could be implemented (in VB but translation to C# is trivial). I would then add over-loads for the useful permutations of inputs.</p> <pre><code>Imports System.Runtime.CompilerServices Imports System.Linq.Expressions Module IQueryableExtensions &lt;Extension()&gt; Public Function ToSelectList(Of T)(source As IEnumerable(Of T), nameExpression As Expression(Of Func(Of T, String)), valueExpression As Expression(Of Func(Of T, String)), selectedExpression As Expression(Of Func(Of T, Boolean)), additionalItems As IEnumerable(Of SelectListItem)) As IEnumerable(Of SelectListItem) Return additionalItems.Union( source.Select(Function(x) New SelectListItem With { .Text = nameExpression.Compile.Invoke(x), .Value = valueExpression.Compile.Invoke(x), .Selected = selectedExpression.Compile.Invoke(x) } ) ) End Function End Module </code></pre> <p>Sample usage:</p> <pre><code> Dim People As New List(Of Person) From {New Person With {.Name = "Richard", .ID = 1}} Dim sl = People.ToSelectList(Function(p) p.Name, Function(p) p.ID, Function(p) p.ID = 1, {New SelectListItem With {.Value = 0, .Text = "Please Select A Person"}}) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload