Click here to Skip to main content
12,823,663 members (66,797 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 26 Aug 2014

Dynamic ASP.NET Web API Controller - Part 1. Dynamic DTO

, 9 Jun 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
How to make dynamic DTO with ExpandoObject


In this article, I'll introduce how to make dynamic DTO.

We don't need to make new DTO every time when you add new stored procedures. I will show how to make DTO dynamically using ExpandoObject class.


Making DTO is somewhat annoying to me. So I utilize ExpandoObject class to make DTO dynamically.

Using the code

GitHub ;

This is my data access layer. I write this sample with enterprise library 6. After fetching data from databse, we can make DTO like  javascript's object. This means we can add new property to C# class in runtime. That's so amazing!

namespace DavidJLee.DAL
    public class QueryExecutor
        private readonly Database _db = default(Database);

        private static readonly Lazy<QueryExecutor> _instance = new Lazy<QueryExecutor>(() =>
            return new QueryExecutor();

        public static QueryExecutor Instance { get { return _instance.Value; } }

        public QueryExecutor()
            DatabaseProviderFactory providerFactory = new DatabaseProviderFactory();
            DatabaseFactory.SetDatabaseProviderFactory(providerFactory, false);
            _db = DatabaseFactory.CreateDatabase();

        public IEnumerable<dynamic> ExecuteStoredProcedure(string procName)
            using (var command = _db.GetStoredProcCommand(procName))
                var dataSet = _db.ExecuteDataSet(command);

                foreach (DataTable table in dataSet.Tables)
                    foreach (DataRow row in table.Rows)
                        ExpandoObject dynamicDto = new ExpandoObject();

                        foreach (DataColumn column in table.Columns)
                            //With expandobject class, we can add new property like javascript's way.                           
                            ((IDictionary<String, Object>)dynamicDto).Add(column.ColumnName, row[column.ColumnName]);

                        yield return dynamicDto;

I use this method with ASP.NET Web API.

namespace DavidJLee.DynamicWebAPI.Controllers
    public class ValuesController : ApiController
        // GET api/values
        public IHttpActionResult Get()
            return Json<IEnumerable<object>>(QueryExecutor.Instance.ExecuteStoredProcedure("usp_User_GetList"));

I try to call this web API with fiddler.

GET http://localhost:50951/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:50951


Points of Interest

With this way, I developed dynamic Web API middle ware. Whenever I develop select command procedure, I don't need to add new controllers or actions. I just need to configure about new procedure (procedure name, parameter info etc).


1. Explanation of basic concept of dynamic web API with dynamic DTO (2014-08-23).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Jonghan Lee
Web Developer Nexon Korea
Korea (Republic of) Korea (Republic of)
I am a web developer in South Korea. I work at Nexon Korea now.

My interest:
- C#
- Etc.

GitHub :

You may also be interested in...

Comments and Discussions

GeneralExcellent Pin
Eka26-Aug-14 22:22
memberEka26-Aug-14 22:22 
GeneralRe: Excellent Pin
David Jonghan Lee26-Aug-14 23:39
memberDavid Jonghan Lee26-Aug-14 23:39 
GeneralNice, but... Pin
Pawel Cioch26-Aug-14 9:08
memberPawel Cioch26-Aug-14 9:08 
GeneralRe: Nice, but... Pin
David Jonghan Lee26-Aug-14 13:13
memberDavid Jonghan Lee26-Aug-14 13:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 9 Jun 2016
Article Copyright 2014 by David Jonghan Lee
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid