值注入器问题

发布于 2024-10-20 04:56:44 字数 1450 浏览 2 评论 0原文

使用 AutoMapper 后,我在这个网站上发现了 ValueInjecter。我正在尝试,但我陷入了一个可能非常简单的场景。

但在我深入研究代码示例之前,有谁知道 ValueInjecter 是否可以在中等信任的 Web 环境中工作? (像 Godaddy 一样?)

好的,进入代码!我有以下模型:

public class NameComponent 
{
    public string First { get; set; }
    public string Last { get; set; }
    public string MiddleInitial { get; set; }
}
public class Person
{
    public NameComponent Name { get; set; }
}

我想映射到以下 DTO:

public class PersonDTO : BaseDTO
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set { NotifyPropertyChanged(() => FirstName, ref _firstName, value); }
    }

    private string _middleInitial;
    public string MiddleInitial
    {
        get { return _middleInitial; }
        set { NotifyPropertyChanged(() => MiddleInitial, ref _middleInitial, value); }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set { NotifyPropertyChanged(() => LastName, ref _lastName, value); }
    }
}

因此,当我想从模型映射到 DTO 时,我需要一个 Model.Name.First -> DTO.名字 当从 DTO 转到模型时,我需要 FirstName ->姓名.第一名.根据我的理解,这不是一个简单的扁平化/取消扁平化,因为这些词也会颠倒过来,即:FirstName <-->姓名.第一名.因此名字和姓氏可以使用相同的规则,但是 MiddleInitial 呢?型号.Name.MiddleInitial -> DTO.MiddleInitial。

我看到有一些插件,但它们似乎都没有达到我想要的效果。还有其他人遇到过这种情况吗?

After working with AutoMapper I came across ValueInjecter on this site. I am trying it out but I am stuck on what is probably a very simple scenario.

But before I dig into the code sample, does anyone know if ValueInjecter works in a Medium-Trust web environment? (Like Godaddy?)

Ok, onto the code! I have the following models:

public class NameComponent 
{
    public string First { get; set; }
    public string Last { get; set; }
    public string MiddleInitial { get; set; }
}
public class Person
{
    public NameComponent Name { get; set; }
}

that I want to map to the following DTO:

public class PersonDTO : BaseDTO
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set { NotifyPropertyChanged(() => FirstName, ref _firstName, value); }
    }

    private string _middleInitial;
    public string MiddleInitial
    {
        get { return _middleInitial; }
        set { NotifyPropertyChanged(() => MiddleInitial, ref _middleInitial, value); }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set { NotifyPropertyChanged(() => LastName, ref _lastName, value); }
    }
}

So when I want to Map from Model to DTO I need a Model.Name.First -> DTO.FirstName
and when going from DTO to Model I need FirstName -> Name.First. From my understanding this is not a simple Flatten/UnFlatten, because the words also reverse themselves, ie: FirstName <--> Name.First. So First and Last names could use the same kind of rule, but what about MiddleInitial? Model.Name.MiddleInitial -> DTO.MiddleInitial.

I see there are some plugins, but none of them seem to do what I would want. Has anyone else come across this scenario?

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

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

发布评论

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

评论(1

二手情话 2024-10-27 04:56:44

基本思想是我将 NameFirstName 相匹配,我将其作为枢轴点,并且在通常将值设置为 1 (FirstName) 的方法中属性 我将其设置为 3 个属性 - 这是用于 ToNameComp

中的 FromNameComp 我匹配相同的属性,但我从 3 中获取值并创建一个并设置它

    public class SimpleTest
    {
        [Test]
        public void Testit()
        {
            var p = new Person { Name = new NameComponent { First = "first", Last = "last", MiddleInitial = "midd" } };
            var dto = new PersonDTO();
            dto.InjectFrom<FromNameComp>(p);

            Assert.AreEqual(p.Name.First, dto.FirstName);
            Assert.AreEqual(p.Name.Last, dto.LastName);
            Assert.AreEqual(p.Name.MiddleInitial, dto.MiddleInitial);

            var pp = new Person();
            pp.InjectFrom<ToNameComponent>(dto);

            Assert.AreEqual(dto.LastName, pp.Name.Last);
            Assert.AreEqual(dto.FirstName, pp.Name.First);
            Assert.AreEqual(dto.MiddleInitial, pp.Name.MiddleInitial);

        }

        public class FromNameComp : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == "Name" && c.SourceProp.Type == typeof(NameComponent)
                    && c.TargetProp.Name == "FirstName"
                       && c.SourceProp.Value != null;
            }

            protected override object SetValue(ConventionInfo c)
            {
                dynamic d = c.Target.Value;
                var nc = (NameComponent)c.SourceProp.Value;
                //d.FirstName = nc.First; return nc.First does this
                d.LastName = nc.Last;
                d.MiddleInitial = nc.MiddleInitial;
                return nc.First;
            }
        }

        public class ToNameComponent : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.TargetProp.Name == "Name" && c.TargetProp.Type == typeof(NameComponent)
                       && c.SourceProp.Name == "FirstName";
            }

            protected override object SetValue(ConventionInfo c)
            {
                dynamic d = c.Source.Value;
                var nc = new NameComponent { First = d.FirstName, Last = d.LastName, MiddleInitial = d.MiddleInitial };
                return nc;
            }
        }

        public class NameComponent
        {
            public string First { get; set; }
            public string Last { get; set; }
            public string MiddleInitial { get; set; }
        }

        public class Person
        {
            public NameComponent Name { get; set; }
        }

        public class PersonDTO
        {
            public string FirstName { get; set; }
            public string MiddleInitial { get; set; }
            public string LastName { get; set; }
        }
}

但在我深入研究代码示例之前,
有谁知道 ValueInjecter
在中等信任度网络中工作
环境? (像 Godaddy 一样?)

它不使用reflection.emit,所以它应该可以工作

the basic idea is that I match the Name with the FirstName, I take this as a pivot point, and in the method that usually sets the value to just one (FirstName) property I set it to 3 properties - that's for the FromNameComp

in the ToNameComp i match the same properties but I take the value from 3 and create one and set it

    public class SimpleTest
    {
        [Test]
        public void Testit()
        {
            var p = new Person { Name = new NameComponent { First = "first", Last = "last", MiddleInitial = "midd" } };
            var dto = new PersonDTO();
            dto.InjectFrom<FromNameComp>(p);

            Assert.AreEqual(p.Name.First, dto.FirstName);
            Assert.AreEqual(p.Name.Last, dto.LastName);
            Assert.AreEqual(p.Name.MiddleInitial, dto.MiddleInitial);

            var pp = new Person();
            pp.InjectFrom<ToNameComponent>(dto);

            Assert.AreEqual(dto.LastName, pp.Name.Last);
            Assert.AreEqual(dto.FirstName, pp.Name.First);
            Assert.AreEqual(dto.MiddleInitial, pp.Name.MiddleInitial);

        }

        public class FromNameComp : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == "Name" && c.SourceProp.Type == typeof(NameComponent)
                    && c.TargetProp.Name == "FirstName"
                       && c.SourceProp.Value != null;
            }

            protected override object SetValue(ConventionInfo c)
            {
                dynamic d = c.Target.Value;
                var nc = (NameComponent)c.SourceProp.Value;
                //d.FirstName = nc.First; return nc.First does this
                d.LastName = nc.Last;
                d.MiddleInitial = nc.MiddleInitial;
                return nc.First;
            }
        }

        public class ToNameComponent : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.TargetProp.Name == "Name" && c.TargetProp.Type == typeof(NameComponent)
                       && c.SourceProp.Name == "FirstName";
            }

            protected override object SetValue(ConventionInfo c)
            {
                dynamic d = c.Source.Value;
                var nc = new NameComponent { First = d.FirstName, Last = d.LastName, MiddleInitial = d.MiddleInitial };
                return nc;
            }
        }

        public class NameComponent
        {
            public string First { get; set; }
            public string Last { get; set; }
            public string MiddleInitial { get; set; }
        }

        public class Person
        {
            public NameComponent Name { get; set; }
        }

        public class PersonDTO
        {
            public string FirstName { get; set; }
            public string MiddleInitial { get; set; }
            public string LastName { get; set; }
        }
}

But before I dig into the code sample,
does anyone know if ValueInjecter
works in a Medium-Trust web
environment? (Like Godaddy?)

it doesn't use reflection.emit so it should work

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