Search code examples
mappingviewmodelvalueinjecter

ValueInjecter injecting between models with same property names


I am trying to inject properties from ProductCategoryViewModel, which has Title and Id properties into ProductViewModel, which also has Title and Id properties (but for Product) and ProductCategoryTitle,ProductCategoryId for ProductCategory.

Nothing I tried so far works. The default injection overwrites Title and Id props in ProductViewModel and so does FlatLoopValueInjection.

This is such a common use case, is there a premade value injection for it?


Solution

  • I solved this using the following value injection

        public class ProductCategoryToProduct : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            return c.TargetProp.Name.Equals(c.Source.Type.Name + c.SourceProp.Name) && c.SourceProp.Type.Equals(c.TargetProp.Type);
        }
    }