Click here to Skip to main content
11,813,974 members (62,632 online)
Click here to Skip to main content

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

, 26 Aug 2014 CPOL 9.4K 11
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

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.

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 26 Aug 2014
Article Copyright 2014 by David Jonghan Lee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid