Question ValueInjecter

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

But before I delve into the sample code, does anyone know if ValueInjecter works in a medium trust web environment? (How is Godaddy?)

Ok, on 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; }
}

what 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); }
    }
}

, Map from Model to DTO, Model.Name.First → DTO.FirstName DTO FirstName → Name.First. , Flatten/UnFlatten, , : FirstName ↔ Name.First. First Last , MiddleInitial? Model.Name.MiddleInitial → DTO.MiddleInitial.

, , , , , . - ?

+3
1

, Name FirstName, , , (FirstName), 3 - FromNameComp

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 ? ( Godaddy?)

reflection.emit,

+1

Source: https://habr.com/ru/post/1795931/


All Articles