Search code examples
c#valueinjecter

ValueInjecter different types/property names


is it possible to inject from something like this :

public class DomainEntity
{
    public Resource DescriptionResource { get; set; }
}

public class Resource
{
    public List<ResourceLocalization> Localizations { get; set; }
}

public class ResourceLocalization
{
    public string Culture { get; set; }
    public string Value { get; set; }
}

to something like this:

public class DomainEntityViewModel
{
    public string Description { get; set; }
}

(DomainEntity.DescriptionResource.Localizations.First().Value => DomainEntityViewModel.Description)

using ValueInjecter.

Thank You!


Solution

  • you could use this injection :

        public class MyInj : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.TargetProp.Name == c.SourceProp.Name 
                && c.TargetProp.Type == typeof (string) 
                && c.SourceProp.Type == typeof (List<ResourceLocalization>);
            }
            protected override object SetValue(ConventionInfo c)
            {
                return ((List<ResourceLocalization>) c.SourceProp.Value).First().Value;
            }
        }