DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Building and Deploying Microservices With Spring Boot and Docker
  • Redefining DevOps: The Transformative Power of Containerization
  • Chaining API Requests With API Gateway

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Building and Deploying Microservices With Spring Boot and Docker
  • Redefining DevOps: The Transformative Power of Containerization
  • Chaining API Requests With API Gateway
  1. DZone
  2. Data Engineering
  3. Databases
  4. ASP.NET MVC: Converting business objects to select list items

ASP.NET MVC: Converting business objects to select list items

Gunnar Peipman user avatar by
Gunnar Peipman
·
Nov. 11, 11 · News
Like (0)
Save
Tweet
Share
12.64K Views

Join the DZone community and get the full member experience.

Join For Free

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.
ASP.NET MVC ASP.NET Object (computer science) Database

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Building and Deploying Microservices With Spring Boot and Docker
  • Redefining DevOps: The Transformative Power of Containerization
  • Chaining API Requests With API Gateway

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: