65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.95/5 (10 votes)

Aug 26, 2014

CPOL
viewsIcon

40638

How to make dynamic DTO with ExpandoObject

Introduction

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.

Background

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

Using the code

GitHub ; https://github.com/thinkit-software/WebProjects/tree/master/src/Common

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.

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


Response

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).

History

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

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