3

在使用 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); }
    }
}

因此,当我想从 Model 映射到 DTO 时,我需要 Model.Name.First -> DTO.FirstName,而从 DTO 到 Model 时,我需要 FirstName -> Name.First。据我了解,这不是一个简单的 Flatten/UnFlatten,因为这些词也会自行反转,即:FirstName <--> Name.First。所以名字和姓氏可以使用相同的规则,但是 MiddleInitial 呢?Model.Name.MiddleInitial -> DTO.MiddleInitial。

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

4

1 回答 1

1

基本思想是我将NameFirstNameFromNameComp

ToNameComp我匹配相同的属性,但我从 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 是否可以在中等信任的 Web 环境中工作?(像Godaddy?)

它不使用反射。发射所以它应该工作

于 2011-03-03T07:47:23.453 回答