CRUD Implementations

Jan 24, 2013 at 5:05 PM

Hi Fabian,

Do you have documentation on how the CRUD functions should be implemented using entities to dtos?

Coordinator
Jan 24, 2013 at 5:30 PM

Hello @pjanssen007

Not at the moment but I will add it soon.

Basic use is as follows:

// e.g.: If at the Business Layer you want to send a DTO to the Presentation Layer, then you should convert it like this:
PersonDTO personDTO = personEntity.ToDTO();

// e.g.: If at the Business Layer you want to send an entity to the Data Layer, then you should convert it like this:
Person personEntity = personDTO.ToEntity();

// e.g.: If at the Business Layer you want to send a collection of DTOs to the Presentation Layer, then you should convert it like this:
ICollection<PersonDTO> personsDTOs = personsEntities.ToDTOs();

// e.g.: If at the Business Layer you want to send a collection of entities to the Data Layer, then you should convert it like this:
ICollection<Person> personsEntities = personsDTOs.ToEntities();

Note that you need to generate Assemblers in order to have those methods available.

Advanced use would be implementing static partial methods provided by Assemblers: OnDTO and OnEntity. Both are invoked after each conversion and are useful if you want to perform additional conversion logic for every conversion performed by the system.

Hope I've been clear enough.


Thanks for using EntitiesToDTOs!

Marked as answer by ffernandez on 12/30/2013 at 6:53 PM
Jan 25, 2013 at 4:55 PM
Edited Jan 25, 2013 at 4:56 PM

Implementing CRUD operations between entity and dto is pretty simple for Create, Read and Delete.

Create:

public Int32 CreateMyDto()
{
    Int32 ret = null;

    using (var Context = new MyContext())
    {
        var entity = new MyEntity();

        Context.MyEntity.AddObject(entity);
        Context.SaveChanges();

        ret = entity.ID;
    }

    return ret;
} 

Read:


public
MyDto GetMyDto(int pID) { MyDto ret = null; using (var Context = new MyContext()) { ret = Context.GetMyEntity(pID).ToDTO(); } return ret; }

 

 

Delete:

public void DeleteMyDto(MyDto pDto)
{
    using (var Context = new MyContext())
    {
        var m = Context.GetMyEntity(pDto.ID);

        Context.DeleteObject(m);
        Context.SaveChanges();
    }
}

 

Update: thats the tricky part, i remembered reading an Article some time ago, that helped me solve this perfectly.

The general approach to updating our entity with data from a dto we received over the webservice would be as follows:

  1. Query MyContext for the existing entity
  2. Copy all values from our dto to the existing entity
  3. SaveChanges

Unfortunately entities don't ship with methods to copy/merge data into them. Here is where the article i mentioned above kicks in.

This Article descries how you can work with entities in detached state. The Problem that arises when using detached entites is, that there is no change tracking., so calling SaveChanges() after Attach() wouldn't do even one damn thing, cause the ObjectStateManager doesn't know the entities contents changed.
Furthermore, simple attaching a detached entity doesn't solve anything, as there may still be navigation properties - these don't get reattached!
The author of this Article implements a solution to all this with his function "AttachUpdated()". All we need to do is copy this neat extension method and make use if it.

What we are going to do is this:

 

public void UpdateMyDto(MyDto pDto)
{
    using (var Context = new MyContext())
    {
        MyEntity oldEntity = Context.GetMyEntity(pDto.ID);
        Context.Detach(oldEntity);

        MyEntity newEntity = pDto.ToEntity();
        newEntity.EntityKey = oldEntity.EntityKey;

        Context.AttachUpdated(newEntity);
        Context.SaveChanges();
    }
}

 So our procedure goes:

  1. Query context for the existing entity
  2. Detach the existing entity
  3. Create a new entity from our dto
  4. set the EntityKey of this new entity to the EntityKey of the old one - AttachUpdated needs an EntityKey to work it's way
  5. Attach the new entity using AttachUpdated() - this will tell the ObjectStateManager that our entity actually got modified after re-attaching it
  6. SaveChanges()

Done :)