将实体 iEnumerator 映射到 Dto 枚举器

发布于 2025-01-13 13:38:01 字数 568 浏览 4 评论 0原文

我正在使用 CQRS。我从数据库中选择我的实体 IEnumerator,我想将其映射到我的 Dto 类。

我的 Dto 类:

public class XCollectionDto
{
    public IEnumerable<XReadDto> Entries { get; init; } = Enumerable.Empty<XReadDto>();
}

我的映射器类:

public class XReadMapper : IEntityToDtoMapper<X, XCollectionDto>
{
    public XCollectionDto Map(IEnumerable <X> source, XCollectionDto target)
    {
        //todo

        Here i want to map source to target Entries list
    }
}

如果没有 for 循环,我该如何做到这一点?我没有使用 AutoMaper,映射是手动的

I am using CQRS. I select my Entities IEnumerator from database and i want to map this to my Dto class.

My Dto class:

public class XCollectionDto
{
    public IEnumerable<XReadDto> Entries { get; init; } = Enumerable.Empty<XReadDto>();
}

My mapper class:

public class XReadMapper : IEntityToDtoMapper<X, XCollectionDto>
{
    public XCollectionDto Map(IEnumerable <X> source, XCollectionDto target)
    {
        //todo

        Here i want to map source to target Entries list
    }
}

How can i do that, without a for loop? I am not using AutoMaper, the mapping is manual

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

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

发布评论

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

评论(1

谜兔 2025-01-20 13:38:01

我想你可以用C#反射来实现你的目的

我创建了两个类进行测试:

public class somemodel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<int> Numlist { get; set; }
    }

 public class somemodelDTO
    {
        public int Id { get; set; }
        public string SomeName { get; set; }
        public List<int> Numlist { get; set; }
    }

绑定somemodelDTO的属性的方法,这些属性与somemodel的属性同名:

private static somemodelDTO GetMap<somemodel, somemodelDTO>(somemodel some)
        {
            somemodelDTO somemDTO = Activator.CreateInstance<somemodelDTO>();
            var typesource = some.GetType();
            var typedestination = typeof(somemodelDTO);
            foreach(var sp in typesource.GetProperties())
            {
                foreach( var dp in typedestination.GetProperties())
                {
                     if(sp.Name==dp.Name)
                    {
                        dp.SetValue(somemDTO, sp.GetValue(some, null), null);
                    }
                }
            }
            return somemDTO;
        }

结果:
输入图片此处描述
输入图片此处描述

I think you could accompish your purpose with C# reflection

I created the two class for test:

public class somemodel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<int> Numlist { get; set; }
    }

 public class somemodelDTO
    {
        public int Id { get; set; }
        public string SomeName { get; set; }
        public List<int> Numlist { get; set; }
    }

the method to bind properties of somemodelDTO which have the same name with properties of somemodel:

private static somemodelDTO GetMap<somemodel, somemodelDTO>(somemodel some)
        {
            somemodelDTO somemDTO = Activator.CreateInstance<somemodelDTO>();
            var typesource = some.GetType();
            var typedestination = typeof(somemodelDTO);
            foreach(var sp in typesource.GetProperties())
            {
                foreach( var dp in typedestination.GetProperties())
                {
                     if(sp.Name==dp.Name)
                    {
                        dp.SetValue(somemDTO, sp.GetValue(some, null), null);
                    }
                }
            }
            return somemDTO;
        }

The result:
enter image description here
enter image description here

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