Dofs
Dofs

Reputation:

Data Transfer Objects and Entity Framework

I am working on a 3-tier web application where I am using the microsoft Entity Framework. To make a loose coupling between the different layers I using data transfer objects to transfer between the Entity Framework objects and my custom objects, but I have a problem with the speed of translating between the entity framework and my custom DTO's. I use this method to transfer from EF to DTO:

public List Transform(List carModelDefinition) {

        List<CarDefinitionDTO> cdDTOList = new List<CarDefinitionDTO>();
        foreach (DataLayer.CarModelDefinition cmd in carModelDefinition)
        {
            CarDefinitionDTO cdDTO = new CarDefinitionDTO();
            cdDTO.CarDefinitionId = cmd.CarModelDefinitionId;
            cdDTO.Compagny = cmd.Company;
            cdDTO.Model = cmd.Model;
            cdDTOList.Add(cdDTO);
        }
        return cdDTOList;

}

But when I try to transfer a list of e.g. 600 elements i takes about 10+ seconds. Am I doing something wrong, or is the speed simply this slow? NB. I am working on a pretty fast PC so it is not the speed of my pc which slows it down.

Upvotes: 1

Views: 3588

Answers (5)

You can try to zip and unzip the data in binary form (compress and decompress).

Upvotes: 0

kzfabi
kzfabi

Reputation: 2075

DRY! Try using a DTO/Assembler generator like EntitiesToDTOs. It generates DTOs and Assemblers from your Entity Framework EDMX file. That way you won't have to code every Entity/DTO mapping, Assemblers will add extension methods to your Entities and generated DTOs so you end up coding like this:

var myCar = new Car();
CarDTO dto = myCar.ToDTO();
myCar = dto.ToEntity();

or:

ICollection<Car> carCol = new List<Car>();
ICollection<CarDTO> carDTOs = carCol.ToDTOs();
carCol = carDTOs.ToEntities();

Pretty simple and the tool is really easy to use.

Upvotes: 0

Dofs
Dofs

Reputation:

I found the error. In the constructor I create the instance of the Entity manager, and when I created a new object it would create a new instance all the time, which was quite time consuming.

Upvotes: 2

Dofs
Dofs

Reputation:

I load it by saying:

        public List<CarDefinitionDTO> LoadAll()
    {
        List<DataLayer.CarModelDefinition> carList = (from cd in mee.CarModelDefinition select cd).ToList();
        CarDefinitionDTO cdDTO = new CarDefinitionDTO();
        List<CarDefinitionDTO> carDefList = cdDTO.Transform(carList);
        return carDefList;
    }

Upvotes: 0

John Saunders
John Saunders

Reputation: 161831

The code you posted does more than translate between the EF type and the DTO type - you're also fetching the data from the database. Try to separate the two for measurement purposes. Chances are that it's the data retrieval that takes 10 seconds, not the time spent moving data around in memory.

Upvotes: 1

Related Questions