Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用自动映射器进行映射_C#_.net_Asp.net Mvc_Asp.net Core_Automapper - Fatal编程技术网

C# 如何使用自动映射器进行映射

C# 如何使用自动映射器进行映射,c#,.net,asp.net-mvc,asp.net-core,automapper,C#,.net,Asp.net Mvc,Asp.net Core,Automapper,我需要映射两个对象。该要求是列车预订详细信息,该信息将包含列车的往返行程详细信息 public class ReservationSource { public string ReservationNumber{get;set;} public TravelS towardsTravelS{get;set;} public TravelS returnTravelS{get;set;} } public class TravelS { public string

我需要映射两个对象。该要求是列车预订详细信息,该信息将包含列车的往返行程详细信息

public class ReservationSource
{
    public string ReservationNumber{get;set;} 
    public TravelS towardsTravelS{get;set;}
    public TravelS returnTravelS{get;set;}
}
public class TravelS
{
   public string travelId{get;set;}
   public ICollection<JourneyS> Journeys{get;set;}
}
这是
ReservationSource
类中的类,用于捕获前往和返回旅程的详细信息

public class ReservationSource
{
    public string ReservationNumber{get;set;} 
    public TravelS towardsTravelS{get;set;}
    public TravelS returnTravelS{get;set;}
}
public class TravelS
{
   public string travelId{get;set;}
   public ICollection<JourneyS> Journeys{get;set;}
}
公共舱旅行
{
公共字符串travelId{get;set;}
公共ICollection旅程{get;set;}
}
上面是保留源对象。此源需要到目标对象的映射。目标对象如下所示

public class ReservationDestination
{
    public string ReservationNumber{get;set;}
    public TravelD towardsTravelD{get;set;}
    public TravelD returnTravelD{get;set;}

}

public class TravelD
{
    public string travelId{get;set;}
    public ICollection<JourneyD> Journeys{get;set;}
}
public class JourneyD
{
    public string JourneyId{get;set;}
}
public class JourneyS
{
    public string JourneyId{get;set;}
}
公共类保留目的地
{
公共字符串ReservationNumber{get;set;}
向Straveld{get;set;}的公共旅行
public TravelD returnTravelD{get;set;}
}
公务舱旅行
{
公共字符串travelId{get;set;}
公共ICollection旅程{get;set;}
}
公务舱
{
公共字符串日志ID{get;set;}
}
公务舱旅程
{
公共字符串日志ID{get;set;}
}
这是我的目标对象。在这里,我想将我的源映射到目标。如何定义映射配置和映射

var config = new mappingConfiguration(cfg=>
{
cfg.CreateMap<ReservationSource,ReservationDestination>()
});

Imapper map = config.CreateMapper();
var config=新映射配置(cfg=>
{
cfg.CreateMap()
});
Imapper map=config.CreateMapper();
这部分代码仅将
reservationNumber
映射到目标对象。有人能帮我映射所有对象吗。即
towardsTravelS
towardsTravelD
returnTravelS
returnTravelD


.net核心版本:3.1

首先,您忘了提到这一点,但我假设还有一个类
TravelS
,看起来像这样:

public class TravelS
{
    public string TravelId { get; set; }
}
var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<ReservationSource, ReservationDestination>()
      .ForMember(dest => dest.ReturnTravelD, opt => opt.MapFrom(src => src.ReturnTravelS))
      .ForMember(dest => dest.TowardsTravelD, opt => opt.MapFrom(src => src.TowardsTravelS));
    cfg.CreateMap<TravelS, TravelD>();
});

var mapper = config.CreateMapper();

var source = new ReservationSource
{
    ReservationNumber = "9821",
    ReturnTravelS = new TravelS
    {
      TravelId = "1"
    },
    TowardsTravelS = new TravelS
    {
      TravelId = "2"
    }
};

var destination = mapper.Map<ReservationDestination>(source);

Console.WriteLine(JsonSerializer.Serialize(destination));
您的配置中缺少一些内容。目前AutoMapper不知道它必须映射具有不同名称的属性(
TowardsTravelS
=>
TowardsTravelD
等),因此我们还必须定义这些属性:

cfg.CreateMap<ReservationSource, ReservationDestination>()
    .ForMember(dest => dest.ReturnTravelD, opt => opt.MapFrom(src => src.ReturnTravelS))
    .ForMember(dest => dest.TowardsTravelD, opt => opt.MapFrom(src => src.TowardsTravelS));
现在我们有了这样的东西:

public class TravelS
{
    public string TravelId { get; set; }
}
var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<ReservationSource, ReservationDestination>()
      .ForMember(dest => dest.ReturnTravelD, opt => opt.MapFrom(src => src.ReturnTravelS))
      .ForMember(dest => dest.TowardsTravelD, opt => opt.MapFrom(src => src.TowardsTravelS));
    cfg.CreateMap<TravelS, TravelD>();
});

var mapper = config.CreateMapper();

var source = new ReservationSource
{
    ReservationNumber = "9821",
    ReturnTravelS = new TravelS
    {
      TravelId = "1"
    },
    TowardsTravelS = new TravelS
    {
      TravelId = "2"
    }
};

var destination = mapper.Map<ReservationDestination>(source);

Console.WriteLine(JsonSerializer.Serialize(destination));

请在此处亲自尝试:

在启动时将其添加到您的服务中:

它是可重复使用和清洁的

 public void ConfigureServices(IServiceCollection services)
{
            services.AddAutoMapper(Assembly.GetExecutingAssembly());
}
在项目中添加这些接口和类

public interface IMapFrom<T>
{
        void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
}
公共接口IMapFrom
{
void映射(Profile Profile)=>Profile.CreateMap(typeof(T),GetType());
}
使用AutoMapper;
使用制度;
使用System.Linq;
运用系统反思;
公共类映射配置文件:配置文件
{
公共映射配置文件()
{
ApplyMappingsFromAssembly(Assembly.getExecutionGassembly());
}
私有void ApplyMappingsFromAssembly(程序集)
{
var types=assembly.GetExportedTypes()
.Where(t=>t.GetInterfaces()
.Any(i=>i.IsGenericType&&i.GetGenericTypeDefinition()==typeof(IMapFrom)))
.ToList();
foreach(类型中的变量类型)
{
var instance=Activator.CreateInstance(类型);
var methodInfo=type.GetMethod(“映射”)
??类型.GetInterface(“IMapFrom`1”).GetMethod(“映射”);
methodInfo?.Invoke(实例,新对象[]{this});
}
}
}
您的源模型如下(将ReservationSource映射到ReservationSource):

公共类保留源:IMapFrom
{
公共字符串名称{get;set;}
公共字符串City{get;set;}
公共空白映射(配置文件)
{
profile.CreateMap()
.ForMember(dest=>dest.ReturnTravelD,opt=>opt.MapFrom(src=>src.ReturnTravelS))
.ForMember(dest=>dest.TowardsTravelD,opt=>opt.MapFrom(src=>src.TowardsTravelS));
}
}

谢谢,它成功了。如果我想更新一些信息,如列车的实时信息(实际列车/发车)(假设我从另一个来源获得这些信息),我可以更新通过automapper映射的数据吗。假设我有一个旅行的集合/列表,我需要来自不同api的旅行类中的一些信息,在映射所有其他字段之后,我想更新一些属性。注意:集合/列表位于innerclassWell中,您必须找到正确的对象来更新自己。但是您可以映射到现有对象(存在
映射(TSource-source,tdestinition)
重载)。这意味着您可以从多个源对象链接贴图(或稍后更新目标对象)。只需确保用作源的每个类型都需要配置到目标类型的映射。祝项目顺利!