In my previous post I wrote about a small utility that automatically generates Data Transfer Objects from LINQ to SQL Data Classes. Few days ago I had the time to add an important feature – the application can now generate DTO’s from ADO.NET Entity Framework Model as well. I also renamed the application to DTOGenerator and published it in CodePlex.
Data Transfer Objects in 2 Diagrams
There are really a lot of explanations about Data Transfer Objects in the web. You can read about them in the MSDN Magazine. But, Here is a 2 diagrams that show the concept of DTO’s in 2 most common scenarios:
Getting Information: DAL=>BLL=>GUI (click to enlarge)
Insert Information: GUI=>BLL=>DAL (click to enlarge)
How to Use DTOGenerator
Creating Data Transfer Objects is really easy while using DTOGenerator.
- First, you have to download the latest version. You can choose if you want to download executable files only or the full source code.
- After the download completed, extract the application. in the following examples i’ll assume that the application files (/bin/release in the full source code, or the files in the root of the “Executable Files Only” ZIP) located in c:\DTOGenerator.
- In order to use the application, you must have a .NET assembly that include ADO.NET Entity Framework model or LINQ to SQL Data Classes (or both…). Then, you have to use the following command from cmd:
c:\DTOGenerator\DTOGenerator.exe “param1” “param2”
(between parameter1 and parameter2 there is a single space).
param1 is the path for you .net assembly
param2 is the path to a directory where the generated files (.cs files – DTO classes) will be created.
- After the files created, add them to your project. The DTO’s are in different namespace called DTO (you can change it manually).
The following code is an example for the file that DTOGenerator will create for the “Product” entity in the Northwind database:
If we work with LINQ to SQL, then the DTO for “Product” will looks like this:
Using the Data Transfer Objects
The main difference in the way DTOGenerator works with ADO.NET EF and the way it works with LINQ to SQL, is that while working with LINQ to SQL no object association will be created. You can compare line 74 in the first example to line 52 in the second example and see the difference.
As you can see, each DTO include two important method: static method called GetDTOFromDALObject that get as parameter an instance of the ADO.NET EF entity object and return DTO that already has all the data stored in the entity instance.
The second method is GetDALObject that return an instance of the entity object that includes all the data stored in the Data Transfer Object.
If you don’t want to load data into the associated objects (for lazy-loading or for some other reasons) GetDTOFromDALObject and GetDALObject has both Boolean parameter that indicates if you want to fill the associated objects with data (the Boolean parameter is only when the DTO’s was generated from ADO.NET EF Model).
For example, if we work with LINQ to SQL then this how selecting data will looks like:
Selecting data with ADO.NET Entity Framework will looks like this:
as you can see, in line 5 we say that we want to get DTO with associated objects that filled with data. if we called DetDTOFromDALObject with “false” then line 6 will throw an exception, because product2.category would be null.
Adding rows to the table will looks like this (LINQ to SQL Example):
First, we get DTO object from another layer in our application (GUI, for example), then, when we add the data (to LINQ to SQL in this example) we use the “GetDALObject” method to give InsertOnSubmit the correct type as parameter, with all the data that stored in the DTO inside.
DTOGenerator is a small code generator the automatically create Data Transfer Objects from given .NET assembly with LINQ to SQL Data Classes or ADO.NET Entity Framework.
It’s really easy to use the application.
It’s beta version, so please report aboug bugs that you found or features you want.