Over the years of development time on a number of small and big projects, I personally have always found myself in a position of asking myself “Do I use DTO’s?”.
I wrote this article to share a blog post from SamsCode.com
For those that are not tech-heads or geeks and are not sure what a DTO is; A DTO is the acronym for Data Transfer Objects, and they aid in making a web or software experience lighter, faster and easier to implement new features straight from your Database storage model.
So when Microsoft brought out the new paradigm of Database to -> Object & Object to -> Database mapping techniques called LINQtoSQL and LINQtoEntities a lot of people stopped creating DTO’s and started using the direct Entity Database object straight from your Data Access Layer….
Over time myself and other users have realised that this is a BAD! BAD! BAD!, Yes BAD! way of moving data around, due highly to these little boxes of data being transferred around your Web applications or Software applications and clogging up your memory allocation.
What made me ask myself the question of using DTO’s again was in Development of a project on the ASP.NET MVC With Entity Framework 4.1 platform was being stuck with the dreaded Error Exception of “Unable to serialize and object, as there is a circular reference“.
What Entity Framework does is it maps out your Database Structure tables and creates a persistent class for each table and the constraints and associations each table has from each other.
For example in a database you might have a Table called “Employee” and you then have another table called “JobRole” and there is a linke between these two tables to show that an Employee can have multiple Job Roles. Entity Framework in .NET will create an object called Employee and create a Property on Employee with the name JobRoles, and this for each record in the Employee list will state an Employees Job Roles list.
So as I am personally a passionate .NET Developer, and I also love the idea of some automation, when used correctly if stumbled across maybe one of the best articles of the past 3 years in Database Object Mapping.
In this article from SamsCode.com he gives you some amazing and time saving approached to Entity Framework Templates & object classes to automate your whole Data Transfer Object Model directly from your Entity Framework Model. But more importantly his concepts are “Real World” and there is not one template that he talks about that won’t help you out with making your Data Layer & Business Layers of Web Applications and Software clean and nice.