Results 1 to 2 of 2

Thread: [RESOLVED] Automapper and ViewModels (destinations) with dependencies

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Resolved [RESOLVED] Automapper and ViewModels (destinations) with dependencies

    Note: I just want to be clear that while I personally hate the idea of ViewModels having dependencies, this one is out of my hands. Actually it's worse. It's Viewmodels having a base class with a dependency. So your controller resolve that dependency, pass it to the ViewModel, which pass it to it's base class, without even using it. Out of my hands!

    Using automapper to map domain and viewmodels (the latter designed as above).

    So CreateMap<source, destination>(); won't work, since "destination" have a dependency that need to be supplied when constructing it (terrible, isn't it?)

    I guess it would be something in the line of this ugly code:

    Code:
    var dependency = ObjectFactory.GetInstance<IWhatever>();  
    CreateMap<Source, Destination>().ConstructUsing(dest => new Destination(dependency));
    Any advice here?


  2. #2

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Automapper and ViewModels (destinations) with dependencies

    Got really desperate on this one, and figured it out thanks to begging from help to ppl I was sure won't have time to help out.

    var krok = new DemoDomainModel {Name = "Krokonoster"};
    var model = new DemoModel(damnedDependecy);
    model = Mapper.Map<DemoDomainModel, DemoModel>(krok, model);
    return View(model);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width