Dto -> Domain

Jan 7, 2011 at 7:30 AM
Edited Jan 7, 2011 at 7:33 AM

Hi

I'm having some troubling Hydrating my DTO's back to the domain objects.

I have been using the following code but the problem is I'm not sure how best to handle the hydration of a collection properties that themselves require injecting.

Specifically IList<PropertyDto> in the below code.

 

public class ProjectDto : DtoBase
{    
   public static ProjectDto Create(Project project)
    {
            var dto = new ProjectDto();
            dto.InjectFrom<FlatLoopValueInjection>(project);

            dto.Properties =
                project.Properties
                .Select(p => new PropertyDto().InjectFrom(p))
                .Cast<PropertyDto>()
                .ToList();

            return dto;
        }
        
        public Project Hydrate(Project source)
        {
            source.InjectFrom<UnflatLoopValueInjection>(this);
            return source;
        }

        public int Id { get; set; }
        public string Project_Name { get; set; }
        public IList<PropertyDto> Properties { get; set; }

//rest of the dto properties here }
//Hydration is called in my services with the following
private ProjectDto UpdateProjectCommand(ProjectDto dto)
{
   Project p = projectRepository.Get(dto.Id);
   
   // hydrate project from dto
    p = dto.Hydrate(p);

//.......
}
Hopefully this is enough code to highlight what im trying and failing todo.
It's pretty clear I'm missing a necessary step but cant find any references to this type of usage to go from. Basically a Dto with a IList of Dto's as another property that also requires mapping.

The PropertyDto is pretty simple and has exact matching property names as the Property domain object.

Any assistance would be appreciated, Thankyou.

 

Coordinator
Jan 7, 2011 at 12:20 PM
Edited Jan 7, 2011 at 12:21 PM

I didn't noticed, anything wrong,  the way you are mapping collections is absolutely correct,

and in general ValueInjecter is very simple, all you do is

target.InjectFrom(source)

which will copy all the values from properties of source to target that are of the same name and type,

for the rest you make your own injections where you specify how to match types, and how to transform the values

Jan 7, 2011 at 1:28 PM

Will it deep dive into my IList properties and try and map those or do i need to specify them again.

As you can see from above i have a ProjectDto with an IList of <PropertyDto>.

I suspect i need to call InjectFrom again perhaps like this:

       public Project Hydrate(Project source)
        {
            source.InjectFrom<UnflatLoopValueInjection>(this);

            //call for the properties
            source.Properties.InjectFrom(this.Properties);
            return source;
        }

 

The above code yields still the same result, and the values of the IList<PropertyDto> dont update the collection on the domain (source.Properties).

I am observing this directly in debugger.

Perhaps there are type matching issues im not understanding... altho what I do does work for the ParentDto...

 

Jan 7, 2011 at 1:42 PM

The below works but its not at all as clean/elegant as i'd like it...

 public Project Hydrate(Project source)
        {
            source.InjectFrom<UnflatLoopValueInjection>(this);

            foreach (var prop in Properties)
            {
                var p2 = source.Properties.Where(x => x.Id == prop.Id).First();
                if (p2 != null)
                    p2.InjectFrom(prop);
            }

            return source;
        }

Coordinator
Jan 7, 2011 at 3:33 PM

well, as I said, it's a simple copy, it's not cloning

so for the valuetypes the value is copied, for reference types (collections are also references), the reference is copied

if you have IList<Foo>on both sides only the reference will be copied

if you have IList<Foo> on one side and IList<Bar> on the other side the default .InjectFrom will not touch them because they are of different types