ASP.NET MVC: Converting business objects to select list items
Some of our business classes are used to fill dropdown boxes or select lists. And often you have some base class for all your business classes. In this posting I will show you how to use base business class to write extension method that converts collection of business objects to ASP.NET MVC select list items without writing a lot of code.
BusinessBase, BaseEntity and other base classes
I prefer to have some base class for all my business classes so I can easily use them regardless of their type in contexts I need.
NB! Some guys say that it is good idea to have base class for all your business classes and they also suggest you to have mappings done same way in database. Other guys say that it is good to have base class but you don’t have to have one master table in database that contains identities of all your business objects. It is up to you how and what you prefer to do but whatever you do – think and analyze first, please. :)
To keep things maximally simple I will use very primitive base class in this example. This class has only Id property and that’s it.
public class BaseEntity
{
public virtual long Id { get; set; }
}
Now we have Id in base class and we have one more question to solve – how to better visualize our business objects? To users ID is not enough, they want something more informative. We can define some abstract property that all classes must implement. But there is also another option we can use – overriding ToString() method in our business classes.
public class Product : BaseEntity
{
public virtual string SKU { get; set; }
public virtual string Name { get; set; }
public override string ToString()
{
if (string.IsNullOrEmpty(Name))
return base.ToString();
return Name;
}
}
Although you can add more functionality and properties to your base class we are at point where we have what we needed: identity and human readable presentation of business objects.
Writing list items converter
Now we can write method that creates list items for us.
public static class BaseEntityExtensions
{
public static IEnumerable<SelectListItem> ToSelectListItems<T>
(this IList<T> baseEntities) where T : BaseEntity
{
return ToSelectListItems((IEnumerator<BaseEntity>)
baseEntities.GetEnumerator());
}
public static IEnumerable<SelectListItem> ToSelectListItems
(this IEnumerator<BaseEntity> baseEntities)
{
var items = new HashSet<SelectListItem>();
while (baseEntities.MoveNext())
{
var item = new SelectListItem();
var entity = baseEntities.Current;
item.Value = entity.Id.ToString();
item.Text = entity.ToString();
items.Add(item);
}
return items;
}
}
You can see here to overloads of same method. One works with List<T> and the other with IEnumerator<BaseEntity>. Although mostly my repositories return IList<T> when querying data there are always situations where I can use more abstract types and interfaces.
Using extension methods in code
In your code you can use ToSelectListItems() extension methods like shown on following code fragment.
// ...
var model = new MyFormModel();
model.Statuses = _myRepository.ListStatuses().ToSelectListItems();
// ...
You can call this method on all your business classes that extend your base entity. Wanna have some fun with this code? Write overload for extension method that accepts selected item ID.
Are there any reason you didn’t write extension method for IEnumerable type?
Like:
public static IEnumerable ToSelectListItems(this IEnumerable baseEntities)
where T : BaseEntity
{
return baseEntities
.Select(x => new SelectListItem {
Value = x.Id.ToString(),
Text = x.ToString() });
}
I agree with Siim. His solution is better if you use IQueryable
Thanks for update, Siim. Using LINQ it looks more elegant :)
yes, I prefer the linq ones.