Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:27+00:00 2026-05-27T15:22:27+00:00

I am playing around with valueinjecter and wondering how do I do viewmodels to

  • 0

I am playing around with valueinjecter and wondering how do I do viewmodels to domains when the view model has a collection of viewmodels?

Say I have this domain

   public class MyDomain
   {
       public IList<MyOtherDomain> MyOtherDomains {get; set;}
   }

   public class MyOtherDomain
   {
       public string Name {get; set;}
   }

   public class MyMasterVM
   {
       public IList<MyOtherVm> MyOtherDomains {get; set;}
   }    

   public class MyOtherVm
   {
       public string Name {get; set;}
   }

now how do I do my injecting? Do I need to manually map these with valueinjector?

public ActionResult Method(MyMasterVM vm)
{
   MyDomain d = new MyDomain();
    d.InjectFrom<UnflatLoopValueInjection>(vm);
}

Edit

After some playing around I got the simulator to work. However mine is different than the one in the tests

// sample test
   public class FooBar : TypeMapper<Foo, Bar>
        {
            public override Bar Map(Foo source, Bar target)
            {
                base.Map(source, target);
                target.NoConvention = source.Name + source.Xyz + source.Props;
                return target;
            }
        }

             [Test]
    public void MapShouldMapCollectionPropertiesAndUseFooBarTypeMapper()
    {
        MapperFactory.AddMapper(new FooBar());
        var foo = new Foo
        {
            Foos = new List<Foo>
                       {
                           new Foo{Name = "f1",Props = "v",Xyz = 19},
                           new Foo{Name = "f2",Props = "i",Xyz = 7},
                           new Foo{Name = "f3",Props = "v",Xyz = 3},
                       }
        };

        var bar = Mapper.Map<Foo, Bar>(foo);

        Assert.AreEqual(foo.Foos.Count(), bar.Foos.Count());

        var ffoos = foo.Foos.ToArray();
        var bfoos = bar.Foos.ToArray();

        for (var i = 0; i < ffoos.Count(); i++)
        {
            Assert.AreEqual(ffoos[i].Name, bfoos[i].Name);
            Assert.AreEqual(ffoos[i].Name + ffoos[i].Xyz + ffoos[i].Props, bfoos[i].NoConvention);
        }
    }

        // mine
public class Test : TypeMapper<IList<MyOtherVm>, IList<MyOtherDomain>> 
    {
        public override IList<MyOtherDomain> Map(IList<MyOtherVm> source, IList<MyOtherDomain> target)
        {
            // not sure if I always have to call base
            // mapping would happen here.
            return base.Map(source, target);
        }
    }

       MapperFactory.AddMapper(new Test());
            IList<MyOtherDomain> otherDomains= new List<MyOtherDomain>();
           MapperVj.Map(vm.MyOtherDomains , otherDomains);

I have to specify it is a IList otherwise it never seems to go into my overridden method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:22:28+00:00Added an answer on May 27, 2026 at 3:22 pm

    I think you’d need to implement your own custom IValueInjection injector. I am also basing this answer on a typo in your MyDomain class.

    Assumption of typo:

    MyDomain.MyOtherDomains : IList<MyOtherDomains>
    

    and not

    MyDomain.MyOtherDomains : IList<MyOtherVm>
    

    So, the customer injection class could look like this (not 100% if there’s a better way to do it)

    public class CustomInjecter : IValueInjection
    {
        public object Map(object source, object target)
        {
            MyDomain result = new MyDomain();
            result.MyOtherDomains = new List<MyOtherDomain>();
    
            foreach (MyOtherVm vm in (source as MyMasterVM).MyOtherVMs)
            {
                MyOtherDomain od = new MyOtherDomain();
                // inject commonly named properties of each "child" VM
                od.InjectFrom(vm);
                result.MyOtherDomains.Add(od);
            }
    
            return result;
        }
    }
    

    In your controller, you can use it almost like you specified above (just change the injector type)

    public ActionResult Method(MyMasterVM vm)
    {
        MyDomain d = new MyDomain();
        d.InjectFrom<CustomInjecter>(vm);
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
While playing around with regexps in Scala I wrote something like this: scala> val
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Playing around with MapKit and I'm wondering ... Is it possible to determine where
While playing around with the emulator, I noticed that when trying to view a
I'm playing around with writing a web app. In this case, I'm using scotty
After playing around with AMD/RequireJS I was wondering if it is a good idea
Playing around with Google Maps these days, with some directions. I have a map
Im playing around with TryParse() But lets say the parsing fails, then returns false,
I am playing around with the AVAudioRecorder so I can have it in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.