What is the best way to define choices / dropdowns to create a view or store db

I'm still not sure how best to save the choices to display on the front panel or db storage.

I am using Enums at the moment, as well as using description decorators ( How do I create a drop-down list from an enumeration in ASP.NET MVC? )

Now I think that I could just create a complete class for this stuff, so I can correctly store the following information with full control:

  • Element name
  • Full description
  • int to store in db
  • order
  • Any methods of obtaining information in any case from the list.

Am I right to think about doing all this manually? I want a really solid way to do this, and the listing really doesn't look like it will cut it.

+6
source share
5 answers

Am I right to think about implementing all of this hand?

Yes. Enumerations are often fuzzy and insufficient abstractions that are not always suitable for the complex domain model that you really want to represent.

Instead of overturning your own, you can consider the Headspring enumeration class (via github , nuget ). We use it all the time instead of enumerations, because it is almost as simple and much more flexible.

An example of the "State" enumeration and its use as a select list:

public class State : Enumeration<State> { public static State Alabama = new State(1, "AL", "Alabama"); public static State Alaska = new State(2, "AK", "Alaska"); // .. many more public static State Wyoming = new State(3, "WY", "Wyoming"); public State(int value, string displayName, string description) : base(value, displayName) { Description = description; } public string Description { get; private set; } } public IEnumerable<SelectListItem> Creating_a_select_list(State selected) { return State.GetAll().Select( x => new SelectListItem { Selected = x == selected, Text = x.Description, Value = x.Value.ToString() }); } 

I'm not trying to sell you in this particular implementation, of course, you can pass your code (Enumeration class - only about 100 lines of code). But I definitely think that you will be able to go beyond the basic listings. This is the right approach, given the scenario that you described in your question.

+4
source

The first place where such information can be presented is the database ... or any "virtual store" such as a web service that offers you the db interface. In fact, if there are other db units that use these values, they MUST be represented in the database, otherwise you will run into big problems. Actually, suppose one of these values ​​is a row .... if you do not define a table containing all possible values ​​+ a key and just write a row, as in other tables ... it will be impossible for you to change the format of the row, since it will “spread” throughout your db ... On the contrary, if you just use a foreign key to refer to such strings ... you can easily change them, since the string is stored only in one place in your dB. Also, in the solution of the enumeration, the problem arises that you cannot add or delete values ​​... therefore, if such operations "conceptually" can make sense, you cannot use the enumeration. You can use the enumeration when all options “conceptually cover” all the possibilities, so you are sure that you will never add / remove other parameters, for example, in the case of an enumeration (yes, no, it is unknown).

However, after you have your options in db, it’s easy ... you will have DTO objects or business entities representing them in exactly the same way as for all other DB entities.

For visualization purposes, you may have a ViewModel version for these parameters, which can contain only the key and description, as well as the "Repository" method, which your controllers can call to have a list of all parameters.

After you have extracted, the controllers put them in a shared ViewViewModel page ... along with all the other information that will be displayed on the page. From the ViewModel ... you can access them to put them in a drop-down list.

To summarize: 1) You need a DB presentation of your options 2) Then you will have a DTO, a business layer and a View version of these entities ... as needed, exactly the same as for all other DB entities.

+3
source

Are you looking for a one-time solution for all selected list options? I personally advocate choosing the option that best suits the particular problem.

In a recent project, I met with the Smart Enum hybrid. Here is an example (I apologize for the typos, I'm typing this cold):

 public class Priority { public enum Types { High, Medium, Low } public Types Type { get; private set; } public string Name { get { return this.Type.ToString(); } } // ToString() with no arguments is not deprecated public string Description { get; private set; } public static High = new Priority{ Type = Types.High, Description = "..."}; public static Medium = new Priority{ Type = Types.Medium, Description = "..."}; public static Low = new Priority{ Type = Types.Low, Description = "..."}; public static IEnumerable<Priority> All = new[]{High, Medium, Low}; public static Priority For(Types priorityType) { return All.Single(x => x.Type == priorityType); } } 

So, in the implementation, you can save the value of Enum, but you must reference the object itself (Priority.For (entity.priority)) for additional metadata when rendering your views.

Is it closer to what you are looking for?

Of course, one of the mistakes - if you need to write a query to a database that relies on metadata in the search, this solution will create several tears along the way.

+2
source

You can use the "repository template" to access the data and use the view modes between your controllers and views. Example:

 //Model public class CustomerViewModel { public Customer customer { get;set; } public IEnumerable<Village> Villages { get; set; } } //Controller public ActionResult Index() { var customerViewModel = new CustomerViewModel { Customer = new Customer(), Villages = _villageService.GetAll() }; return View(customerViewModel); } //View @model ViewModel.RegisterViewModel @Html.DropDownListFor(q => q.Customer.VillageId, new SelectList(Model.Villages, "Id", "Title"), "Please Select") 

I wrote a blog post about the repository template, you can see.

+1
source

I save my settings in the viewing models themselves:

 public class ViewModel { [Required] public int SelectListValue { get; set; } public IDictionary<String,String> SelectListOptions { get { return new Dictionary<String, String>{ { "0", Resources.Option1}, { "1", Resources.Option2}, { "2", Resources.Option3} }; } } 

}

Then I can simply leave the following line in my view to display the select list:

 <%= Html.DropDownListFor(m => m.SelectListValue, new SelectList(this.Model.SelectListOptions, "Key", "Value", "")) %> 
+1
source

Source: https://habr.com/ru/post/910589/


All Articles