Ok refactored my code like so:

 public void Update(AgentUpdateDTO agentUpdateDTO) {

            // Use the assembler to map from the dto using the assembler.
            Agent agent = this.agentAssembler.MapFromDTO(agentUpdateDTO);

            // Validate the model and use repository to update.
            agent.Validate();
            this.agentRepository.Update(agent);

        }

The agentAssembler is injected into my service by Dependancy injection
now which works a charm, my assembler has a MapFromDTO method which
handles the mapping, the assembler itself is injected the required
repositories to do the mappings the method would look like so:

public override Agent MapFromDTO(AgentUpdateDTO dto) {

            // If id is 0 create a new agent model, otherwise
            // use repository to select the agent.
            var agent = (dto.Id == 0)
                ? new Agent()
                : this.agentRepository.SelectById(dto.Id);

            // Map across properties from DTO.
            agent.Enabled = dto.Enabled;
            agent.FirstName = dto.FirstName;
            agent.Surname = dto.Surname;
            agent.UserName = dto.UserName;
            agent.RowVersion = dto.RowVersion;

            // Map across country if the code is set.
            if (!string.IsNullOrEmpty(dto.CountryCode))
                agent.Country =
this.countryRepository.SelectById(dto.CountryCode);

            // Return the mapped agent model.
            return agent;

        }


Do you find this better than my previous solution or do you have more
tips for me :), I am pretty happy with the way it is working now
though.


Cheers

On Mon, Oct 6, 2008 at 12:01 AM, Stefan Sedich <[EMAIL PROTECTED]> wrote:
> Excellent looks like I am not totally off the rails then :)
> Cheers
> Stefan
>
> On 05/10/2008, at 10:59 PM, "Gustavo Ringel" <[EMAIL PROTECTED]>
> wrote:
>
> I give the translators the access to the repositories they
> require...(injection)
>
> Gustavo.
>
> On Sun, Oct 5, 2008 at 4:51 PM, Stefan Sedich <[EMAIL PROTECTED]>
> wrote:
>>
>> Thanks the idea was to have a mapper to do this, one question I had I
>> guess on this was would a mapper be able to talk to the say
>> CountryRepository to do the attaching of the Country on our agent when
>> mapping from a DTO?
>>
>>
>> Cheers
>>
>> On Sun, Oct 5, 2008 at 11:44 PM, Gustavo Ringel
>> <[EMAIL PROTECTED]> wrote:
>> > Your Update now knows how to convert to a DTO and how to validate and
>> > update...i think this is a lot of responsibilities for a single
>> > method...
>> >
>> > I like more having a Translation/Convertion Class which knows both
>> > ways...this helps you doing thinks like list.ConvertAll<AgendtDTO> ...
>> > and
>> > inverse...
>> >
>> > Gustavo.
>> >
>> > On Sun, Oct 5, 2008 at 3:38 PM, codemonkey <[EMAIL PROTECTED]>
>> > wrote:
>> >>
>> >> Hello,
>> >>
>> >> Just some tips really my current Service update method accepts my
>> >> updateDTO object, uses the repository to do a select of the object by
>> >> ID, maps across the properties and then does an update. Is this a good
>> >> way to do this? or could I be looking at a better way? It works fine I
>> >> am just looking for some constructive criticism.
>> >>
>> >> public void Update(AgentUpdateDTO agentUpdateDTO) {
>> >>
>> >>            // Select from the repository by id.
>> >>            var agent =
>> >> this.agentRepository.SelectById(agentUpdateDTO.Id);
>> >>
>> >>            // Map across properties from DTO.
>> >>            agent.Code = agentUpdateDTO.Code;
>> >>            agent.Commission = agentUpdateDTO.Commission;
>> >>            agent.CompanyName = agentUpdateDTO.CompanyName;
>> >>            agent.Enabled = agentUpdateDTO.Enabled;
>> >>            agent.FirstName = agentUpdateDTO.FirstName;
>> >>            agent.Gender = agentUpdateDTO.Gender;
>> >>            agent.Password = agentUpdateDTO.Password;
>> >>            agent.Surname = agentUpdateDTO.Surname;
>> >>            agent.UserName = agentUpdateDTO.UserName;
>> >>            agent.RowVersion = agentUpdateDTO.RowVersion;
>> >>
>> >>            if (!string.IsNullOrEmpty(agentUpdateDTO.CountryCode))
>> >>                agent.Country =
>> >> this.countryService.SelectByCountryCode(agentUpdateDTO.CountryCode);
>> >>
>> >>            // Validate the model and use repository to update.
>> >>            agent.Validate();
>> >>            this.agentRepository.Update(agent);
>> >>
>> >>        }
>> >>
>> >> Cheers
>> >>
>> >>
>> >>
>> >>
>> >
>> >
>> > >
>> >
>>
>>
>>
>> --
>> Stefan Sedich
>> Developer
>> Microsoft Certified Professional (MCP)
>>
>>
>
>
> >
>



-- 
Stefan Sedich
Developer
Microsoft Certified Professional (MCP)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to