Using DTO's: Code Examples

LINQ to SQL

DTOGenerator do nothing with relationships between entities. For Example, this is LINQ query that select all the Products.
Because we want to use our DTO's when returning the data from the DAL to the BLL (for example), we need to convert the objects that created by LINQ to SQL to an instance of our DTO - to do this, we use GetDTOFromDALObject method:

            using (var data = new NorthwindDataContext())
            {
                List<DTO.ProductDTO> products = (from p in data.Products
                                                 select DTO.ProductDTO.GetDTOFromDALObject(p)).ToList();
            }

If we want to Insert data, we usually get the data from the GUI as our DTO object, and then we'll want to add it to the DB using LINQ to SQL - That's why we have GetDALObject method that return a instance of the Data Class object (the object we need to use in order to insert the data):

            //Create a new Product in GUI - use DTO
            DTO.ProductDTO newProduct = new DTO.ProductDTO();
            newProduct.CategoryID = 1;
            newProduct.Discontinued = true;
            newProduct.ProductName = "check";
            newProduct.QuantityPerUnit = "10";
            newProduct.UnitPrice = 90;
            newProduct.UnitsInStock = 10;

            //In the DAL, the Insert method get DTO.ProductDTO as parameter,
            //but it has to use the LINQ to SQL object to insert it - that's why we have GetDALObject()
            using (var data = new NorthwindDataContext())
            {
                data.Products.InsertOnSubmit(newProduct.GetDALObject());
                data.SubmitChanges();
            }

Remember, that the FK in Products table to the Categories table, represented as another property (the FK is int, so the property is int too).

ADO.NET Entity Framework

Working with the generated DTO's is pretty much the same in ADO.NET Entity Framework, except for one difference: DTOGenerator create relationships between objects that represents tables with relationships. which means that in the generated DTO of Products object (from Northwind database), in LINQ to SQL the property of ColumnID (which is FK in the DB) looks like public Int32? CategoryID { get; set; }
while in the DTO that the application create from ADO.NET Entity framework model, it looks like this: public DTO.CategoryDTO Category { get; set; }

Because I want to give you the option to avoid loading this properties with the relevant object, to enable lazy-loading or for any reason you want, the methods GetDTOFromDALObject and GetDALObject get an additional boolean parameter that tell indicate if you want to fill this objects with data or not.

This code, for example, will throw an exception:
//BAD CODE
            using (var data = new NORTHWNDEntities())
            {
                var product = data.Products.First(p => p.Category.CategoryName == "Beverages");
                product.Category = data.Categories.First(p => p.CategoryID == 1);
                var product2 = ProductDTO.GetDTOFromDALObject(product, false);
                Console.WriteLine(product2.Category.Description); //product2.Category is null - we passed "false" to the method in the previous line - means the application won't load objects with related data
                Console.Read();
            }

If we change the parameter "false" in the 5th line to "true" it will work just fine - product.Category will not be null.

Last edited Sep 30, 2009 at 3:52 PM by ShaharGS, version 1

Comments

No comments yet.