Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
9 Jun 2016 1  
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;
                    }                   
                }
            }
        }
    }
Colourised in 38ms

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"));
        }      
    }
}
Colourised in 7ms

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here