在 Automapper 中将字符串值转换为实体

发布于 2024-10-16 09:17:26 字数 1418 浏览 1 评论 0原文

我试图弄清楚当我的实体具有实体类型的字段时如何使用 Automapper。
我有 3 个这样的类:

public abstract class Entity<IdK> 
{
    public virtual IdK Code { get; protected set; }
}

public class Contact : Entity
{
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }
}

public class Company : Entity
{
    public virtual string Name { get; set; }
}

我的 Contact 类包含一个 Company 类型的元素。
我还创建了一个 ViewModel 来将一些信息传输到我的视图:

public ContactViewModel()
{
        public Guid Code { get; set; }
        public int Version { get; set; }

        [DisplayName("Contact")]
        public string Name { get; set; }

        [DisplayName("Company")]
        public string Company { get; set; }
}

在我的视图模型中,我定义了一个字符串类型的字段 Company 。这将包含用户从下拉列表(公司列表)中选择的值。

我在应用程序启动时定义了一个引导程序:

public class AutoMapperConfiguration
    {
        public static void Configure()
        {
            Mapper.Initialize(x => {
                x.CreateMap<Domain.Contact, ViewModels.ContactViewModel>();
                x.CreateMap<ViewModels.ContactViewModel, Domain.Contact>()
            });
        }
    }

当我尝试将 ViewModel 重新映射到我的实体时,出现转换错误 (AutoMapper.AutoMapperMappingException)。 Automapper 无法弄清楚如何将我的 Company (字符串)转换为 Company 类型的对象,Contact 的成员。 是否可以定义一个规则,以便 Automapper 知道如何将字符串(公司)转换为我的 Company 对象(联系人成员)的代码?

I am trying to figure out how to use Automapper when my entity has a field of type entity.
I've got 3 classes like these:

public abstract class Entity<IdK> 
{
    public virtual IdK Code { get; protected set; }
}

public class Contact : Entity
{
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }
}

public class Company : Entity
{
    public virtual string Name { get; set; }
}

My class Contact contain an element of type Company.
I've also created a ViewModel to trasfer some infos to my view:

public ContactViewModel()
{
        public Guid Code { get; set; }
        public int Version { get; set; }

        [DisplayName("Contact")]
        public string Name { get; set; }

        [DisplayName("Company")]
        public string Company { get; set; }
}

In my viewmodel I've defined a field Company of type string. This is going to contain a value the user will chose from a dropdown (list of companies).

I've defined a bootstrapper when my App starts:

public class AutoMapperConfiguration
    {
        public static void Configure()
        {
            Mapper.Initialize(x => {
                x.CreateMap<Domain.Contact, ViewModels.ContactViewModel>();
                x.CreateMap<ViewModels.ContactViewModel, Domain.Contact>()
            });
        }
    }

When I try to remap my ViewModel to my entity I get a conversion error (AutoMapper.AutoMapperMappingException).
Automapper can't figure out how to convert my Company (string) into an object of type Company, member of Contact.
Is it possible to define a rule so that Automapper know how to transform the string (company) into the code of my Company object, member of Contact?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-10-23 09:17:26

您需要使用解析器。像这样:

public class CompanyTypeResolver : ValueResolver<string, Company>
{
    protected override Company ResolveCore(string name)
    {
        return new Company {Name = name};
    }
}

然后在映射代码中你可以这样称呼它:

.ForMember(dto => dto.Company, opt => opt.ResolveUsing<CompanyTypeResolver>().FromMember(src => src.Name))

You need to use a Resolver. Something like:

public class CompanyTypeResolver : ValueResolver<string, Company>
{
    protected override Company ResolveCore(string name)
    {
        return new Company {Name = name};
    }
}

Then in mapping code you call it like:

.ForMember(dto => dto.Company, opt => opt.ResolveUsing<CompanyTypeResolver>().FromMember(src => src.Name))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文