Shahar Gvirtz's Weblog

an Open Source Utility that Automatically Create Data Transfer Objects based on LINQ to SQL Data Classes

Few weeks ago I posted in my Hebrew blog a post about using Data Transfer Objects to work with LINQ to SQL and ADO.NET Entity Framework (that currently both of them doesn’t support working with POCO).
One of the comments I got was that using DTO’s takes twice the time than not using them. That’s because you have to write DTO class for each entity and you also have to write  method in the DTO class that return the DAL object (the object created by the ORM and mapped to a table in the DB) from the DTO and vice-versa.

Although I don’t think it’s too much work, and i think that the advantages are significant enough to make the effort worth, I wrote during my job a small code generator that create DTO’s for each entity in the LINQ to SQL DataClasses that exists in the application.

LINQ2SQLDTOCreator

The application I wrote called LINQ2SQLDTOCreator get a path to an assembly (let’s say – the application DLL) and a path to a folder where the application will save the generated files. The application looks in the assembly to find classes that has the attribute System.Data.Linq.Mapping.TableAttribute . Classes with this attributes are actually classes that are part of LINQ to SQL model.
In the classes with this attribute, the application looks for properties with System.Data.Linq.Mapping.ColumnAttribute attribute. These properties, are properties that mapped to a specific column in a table.

Finally, for each object in the LINQ to SQL Data Classes, the application creates a new class (.cs file) in the path the user gave as parameter to the application.
The generated class include definition for the properties that are in the original object created by LINQ to SQL.
In addition to the properties, the generated classes contains two important methods: a static method that get an instance of the object created by LINQ to SQL (the data class) and return an instance of the DTO and a method that return an instance of the LINQ to SQL object with all of the values that stored in the DTO.
That means that if you put the generated DTO’s in a separate project you have to make sure that this project have reference to the assembly that include your LINQ to SQL Data Classes (so the method that work with this data classes will compile).

For example, here is a DTO class created by the Application:

   1: using System;
   2: /*
   3: ------------------------DTO OBjECT-----------------------------
   4: --------------Generated By LINQ2SQLDTOCreator------------------
   5: ----------------Developed by Shahar Gvirtz---------------------
   6: ---------http://blogs.microsoft.co.il/blogs/shahar-------------
   7: ---------------http://weblogs.asp.net/shahar-------------------
   8: */
   9:  
  10: namespace DTO
  11: {
  12:     public class VideoDTO
  13:     {
  14:         public static VideoDTO GetDTOFromDALObject( DAL.Video src )
  15:         {
  16:             VideoDTO obj = new VideoDTO ();
  17:             obj.ID = src.ID;
  18:             obj.Title = src.Title;
  19:             obj.DateAdded = src.DateAdded;
  20:             obj.Code = src.Code;
  21:             obj.ForumLink = src.ForumLink;
  22:             obj.CategoryID = src.CategoryID;
  23:             obj.ThumbnailURL = src.ThumbnailURL;
  24:             obj.Description = src.Description;
  25:  
  26:             return obj;
  27:         }
  28:         public DAL.Video GetDALObject()
  29:         {
  30:             DAL.Video obj = new DAL.Video ();
  31:             obj.ID = ID;
  32:             obj.Title = Title;
  33:             obj.DateAdded = DateAdded;
  34:             obj.Code = Code;
  35:             obj.ForumLink = ForumLink;
  36:             obj.CategoryID = CategoryID;
  37:             obj.ThumbnailURL = ThumbnailURL;
  38:             obj.Description = Description;
  39:  
  40:  
  41:             return obj;
  42:         }
  43:         
  44:         public Int32 ID { get; set; }
  45:         public String Title { get; set; }
  46:         public DateTime DateAdded { get; set; }
  47:         public String Code { get; set; }
  48:         public String ForumLink { get; set; }
  49:         public Int32 CategoryID { get; set; }
  50:         public String ThumbnailURL { get; set; }
  51:         public String Description { get; set; }
  52:  
  53:  
  54:     }
  55: }

How To Use The DTO Generator?

  1. Download the application
  2. The file you downloaded contains the full source. inside the project directory in \bin\Release you’ll find the executable file. extract all of them to another directory.
  3. This is a Console Application. This application get two command line parameters separated by blank space. The first parameter is the path of the assembly that contain your LINQ to SQL Data Classes. The second parameter is the path to a directory where all of the .cs files generated by the application will stored. For example:
     
  4.    1: c:\myApp\LINQ2SQLDTOCreator.exe "c:\Dev\App\bin\debug\logic.dll" "c:\outputfromapp"
       2:  

    (make sure the parameters are separated by a single blank space)
  5. Now, the application will run and in the folder you entered in the second parameter you will find the DTO classes. Now, you can migrate this files into your project, compile and use.
  6. Remember to re-run the application every time you change the LINQ to SQL model in order to get an updated DTO’s.

Known Problems

  • The application doesn’t support any relationships between objects. If there are relationships in the database, the DTO will contain a property for the FK but no object association will be created.
  • The application can’t work with .NET 4.0 assemblies for a simple reason. If you want to work with .NET 4.0 assemblies you have to modify the project settings and set the Target Framework to .NET 4.0, rebuild and enjoy.
  • The application doesn’t put the created files automatically in your project.

If you get any other problems, let me know and I’ll try to fix.

Summary

LINQ2SQLDTOCreator is a really simple application that use Reflection in order to create Data Transfer Objects based on given assembly that include LINQ to SQL model. The application can be really useful if you use LINQ to SQL and want to create DTO’s without write at all :-)

You can download the application, for free, here.

Shahar.

Comments

Eric said:

Nice.

Do you think it would be beneficial if the generated classes used extension methods, so that we could write something like:

videoDALObject.ToDTO()

instead of (or in addition to)

GetDTOFromDALObject(videoDALObject)

# September 24, 2009 10:47 AM

shahargs said:

Hi Eric,

Making the application generate class with an extension method is easy. I'll do it and post a new post when a new version will be available.

Shahar.

# September 24, 2009 1:02 PM

Yousuf said:

Does it works for vb.net?

# May 29, 2011 3:13 AM

Flour Mills said:

I find your website on google, it proved to be useful for me , i like it very much !

# June 10, 2011 3:48 AM

Testking NS0-154 said:

If you're looking for just the right publication to provide your kid, and he/she is considering characters and combat, masters of magic and sorcerers, invisible planets or exclusive creativities on old fairy tales

# May 3, 2012 3:10 AM

http://www.deelsonheels.com said:

I have fun with, lead to I discovered just what I was having a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

# April 1, 2013 5:31 PM