在 C# 中将一个对象转换为另一个对象

我是 C# 的新手,dotnet 核心。我正在从数据库中获取数据,如下所示。这个数据很平淡。我需要转换为另一个对象。


public class Lead

    {

        public long Id { get; set; }


        public short LeadCreatorId { get; set; }


        public short LeadOwnerId { get; set; }


        public string ProductId { get; set; }


        public LeadPriority Priority { get; set; }


        public LeadStatus Status { get; set; }


        public long LeadCustomerId { get; set; }  


        public string FirstName { get; set; }


        public string LastName { get; set; }


        public string EmailAddress { get; set; }


        public string MobileNo { get; set; }


        public DateTime CreatedOn { get; set; }

    }

我需要将其转换为以下对象。


public class LeadDto

    {

        public long Id { get; set; }


        public short LeadCreatorId { get; set; }


        public short LeadOwnerId { get; set; }


        public string ProductId { get; set; }


        public LeadPriority Priority { get; set; }


        public LeadStatus Status { get; set; }


        public LeadCustomer LeadCustomer { get; set; }


        public DateTime CreatedOn { get; set; }

    }

LeadCustomer 如下所示 -


public class LeadCustomer{


            public long Id { get; set; }


            public string FirstName { get; set; }


            public string LastName { get; set; }


            public string EmailAddress { get; set; }


            public string MobileNo { get; set; }

}

我怎样才能轻松做到这一点?我可以使用 dto 进行转换吗?


慕桂英3389331
浏览 336回答 4
4回答

慕婉清6462132

它们是互联网上的许多映射器库。仅举几例自动映射器映射器发射映射器他们都有自己的优点和缺点。我个人觉得编写自己的映射器方法是值得的,因为您将完全控制自己编写的内容。我同意这可能需要一些时间,但不会花这么长时间。

白板的微信

由于LeadDto并LeadCustomer取决于Lead:一种选择是添加一个构造函数,并将LeadDto对象作为参数并映射到属性。LeadCustomerLead另一种选择是创建静态扩展方法。public static LeadDto ToLeadDto(this Lead lead){   return new LeadDto(){      this.Id = lead.Id;      // Etc..   }}然后你可以使用像LeadDto myObj = someLead.ToLeadDto();另一种选择是使用诸如 AutoMapper 之类的库。我从来没有使用过它,并且对于您声明的需求来说太过分了,IMO。

跃然一笑

一个简单直接的方法可能是在 Lead 类上有一个函数来返回您需要的对象。public class Lead{    //// your properties    public LeadDto GetOtherObject()    {        LeadDto object = new LeadDto();        ////Map properties from this to object        return object;    }}否则,如果上述方法在给定的上下文中似乎不正确,则您可以使用静态实用程序功能。public static class Utility{    public static LeadDto GetOtherObject(Lead leadObject)    {        LeadDto object = new LeadDto();        ////Map properties from leadObject to object        return object;    }}如果 的耦合LeadDto严格只与 Lead 耦合,则可以Lead在 的构造函数中有一个对象作为参数本身LeadDto。在将属性从 Lead 映射到 LeadDto 时,请确保您考虑LeadPriority并LeadStatus谨慎,因为它们可能会根据它们是结构还是类而以不同方式工作。

幕布斯7119047

正如@G_S 所建议的那样,我使用了如下所示的自动映射器 -&nbsp; &nbsp; &nbsp; &nbsp; CreateMap<Lead, LeadDto>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(desc => desc.LeadCustomer, opt => opt.ResolveUsing(src =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new LeadCustomer()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id = src.LeadCustomerId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstName = src.FirstName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LastName = src.LastName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EmailAddress = src.EmailAddress,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MobileNo = src.MobileNo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));
打开App,查看更多内容
随时随地看视频慕课网APP