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.
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:
How To Use The DTO Generator?
- Download the application
- 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.
- 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:
(make sure the parameters are separated by a single blank space)
- 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.
- Remember to re-run the application every time you change the LINQ to SQL model in order to get an updated DTO’s.
- 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.
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.