Oleg
Oleg

Reputation: 1755

Universal type for return data from database

I want to write a function, which should take SQL query as argument and give data as result. So, I want that this function will give universal structure as result. May be the best way to return DataTable or SqlDataReader ?
And after that to extract data from them and put it to the object with special type (different classes of models)?

May be exist some pattern for this?

Upvotes: 0

Views: 237

Answers (3)

Roy Ashbrook
Roy Ashbrook

Reputation: 854

You can return the values directly to objects. But you will still need to map them eventually to something.

Normally for this sort of 'generic' use, I see everything being transferred as strings. Kind of how JSON gets serialized to some kind of string everywhere until it's ultimately used somewhere as something else.

Whether you use DataTable or something else, it really doesn't matter as it will ultimately end up being completely dependent on how you turn that data into something useful.

you can take a look at the http://en.wikipedia.org/wiki/NoSQL page if you want to see some more info about that method.

Martin Fowler gave a presentation on schemaless and NoSql earlier this year that you may find interesting. http://www.youtube.com/watch?v=8kotnF6hfd8

I would echo some of the other answers on here though and most likely advocate not doing something 'generic' or 'universal'. Just put in some work up front to make the system conform to what you definitely want to do and change it as needed.

Upvotes: 2

Lars Udengaard
Lars Udengaard

Reputation: 1267

You could use c# dynamic as return type. I would recommend Dapper as ORM, it is fast and easy to use, and it has the functionality to return dynamics from an query, see the section:'Execute a query and map it to a list of dynamic objects'.

Upvotes: 0

Chris Moschini
Chris Moschini

Reputation: 37947

It sounds like you're just starting out. You may want to consider an ORM like Entity Framework Code First or NHibernate, or a MicroORM of which there are too many to list, rather than just running bare SQL against a db.

I find EF Code First to be great for beginners.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

ORMs let you define classes that map to the tables in your database, and ask for lists of those objects explicitly (solving the problem of what to return).

Upvotes: 1

Related Questions