# 객체 매핑 - Object To Object Mapping

Object To Object Mapping

#### 설명

지루하고 반복적인 매핑 작업을 자동으로 매핑해서 객체 변환 하는 기능을 AutoMapper과 통합하여 기본 제공한다.

![Alt text](/files/qs7Fmn8gEJy8uzQumQSZ)

"AutoMapper"은 .NET 개발 환경에서 객체 간 데이터 매핑을 자동화하기 위한 도구입니다. 이 라이브러리는 C# 및 .NET 언어로 개발된 애플리케이션에서 데이터 전송 객체(DTO)와 엔티티 객체(Entity) 간의 속성 매핑을 단순화하는 데 도움을 줍니다. AutoMapper를 사용하면 반복적이고 번거로운 코드 작성을 줄일 수 있으며, 코드 유지보수를 개선할 수 있습니다.

#### 예제

```csharp
public class SampleAppController : AppController
{
    private readonly IRepository<Sample> _sampleRepository;
    private readonly IObjectMapper _objectMapper;

    public UserAppController(IRepository<Sample> sampleRepository, IObjectMapper objectMapper)
    {
        _sampleRepository = sampleRepository;
        _objectMapper = objectMapper;
    }

    // 사용자에게 올라온 파라메터를 CreateSampleDto의 Dto로 바인딩한다.
    public void CreateSample(CreateSampleDto input)
    {
        // 사용자단에서 올라온 Dto 객체를 엔티티 객체로 변환한다.
        var sample = _objectMapper.Map<Sample>(input);
        // 변환된 엔티티 객체의 값을 DB에 저장한다.
        _userRepository.Insert(sample);
    }
}
```

#### AutoMapper와 통합

IObjectMapper를 통해서 AutoMapper과 통합하였고, 추가 기능을 제공하고 있습니다.

**매핑 만들기**

매핑을 사용하기 전에 AutoMapper에서는 클래스 간의 매핑을 정의해야합니다. 매핑에 대한 자세한 내용은 AutoMapper의 자체 설명서([documentation](http://automapper.org/))를 참조하십시오. Axs를 사용하면 좀 더 쉽게 모듈화 할 수 있습니다.

**자동 매핑**

대부분의 경우 클래스를 직접 매핑 할 수 있습니다. 이 경우 **AutoMap**, **AutoMapFrom** 및 **AutoMapTo** 속성을 사용할 수 있습니다. 예를 들어 위 샘플에서 **CreateSampleDto** 클래스를 **Sample** 클래스에 매핑하려면 아래와 같이 **AutoMapTo** 특성을 사용할 수 있습니다 :

* 중요: **AutoMapper.AutoMap** 아님 `using`문 확인 해야함

```csharp
using Axs.AutoMapper;

[AutoMap(typeof(Sample))]
public class CreateSampleDto
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
}
```

AutoMap 속성은 두 클래스를 양방향으로 매핑합니다.

**매핑 커스터마이징**

경우에 따라 단순 매핑이 적합하지 않을 수 있습니다. 예를 들어 두 클래스의 속성 이름이 약간 다르거나 매핑하는 동안 일부 속성을 무시할 수 있습니다. 이러한 경우 AutoMapper의 API를 직접 사용하여 매핑을 정의해야합니다.

매핑 시 Security 프로퍼티를 무시하고 사용자에게 이름이 약간 다른 Email 프로퍼티가 있다고 가정 할때 아래와 같이 매핑을 정의 할 수 있습니다.

```csharp
[DependsOn(typeof(AxsMapperModule))]
public class MySampleModule : AxsModule
{
    public override void PreInitialize()
    {
        Configuration.Modules.AxsAutoMapper().Configurators.Add(config =>
        {
            config.CreateMap<CreateSampleDto, Sample>()
                    // 매핑이 무시되도록 하여 값이 복사가 안되게 한다.
                    .ForMember(u => u.Security, options => options.Ignore())
                    // 속성값이 다를때 매핑시켜 준다.
                    .ForMember(u => u.Email, options => options.MapFrom(input => input.EmailAddress));
        });
    }
}
```

#### 확인

SampleAppController 서비스에서 CreateSample 메소를 호출하여 아래와 같이 객체를 매핑하여 사용할 수 있습니다.

![picture 1](/files/TXEMwdwWgnENm0j7iCgA)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.axuslab.com/1.-common-structures/object_to_object_mapping.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
