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 6888559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:02:10+00:00 2026-05-27T06:02:10+00:00

I’m using ASP.NET MVC 3, with Raven DB as a backing data store. I

  • 0

I’m using ASP.NET MVC 3, with Raven DB as a backing data store. I have a set of models, which I’m interested in transforming into ViewModels. To accomplish this, I’m leveraging AutoMapper to take care of the dirty work of mapping each property to the corresponding one in the ViewModel. Let’s say that I’ve got a model like so:

public class FooModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AlphaId { get; set; }
    public int BetaId { get; set; }
}

And then let’s say I want to transform it into a ViewModel like so:

public class FooViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AlphaId { get; set; }
    public Alpha Alpha { get; set; }
    public int BetaId { get; set; }
    public Beta Beta { get; set; }
}

My map is then set up like this at app startup:

Mapper.CreateMap<Foo, FooViewModel>();

Then, in the controller I’m performing the map as so:

public ActionResult FooDetails(string id)
{
    using(var session = this.documentStore.OpenSession())
    {
        var fooInstance = session.Load<Foo>(id);
        var fooViewModel = Mapper.Map<FooViewModel>(fooInstance);
        return this.View(fooViewModel);
    }
}

The problem is that as you can see above, the entity coming out of the repository has 2 properties which are the keys of other objects, of types Alpha and Beta. I’m interested in hydrating Alpha and Beta based upon the AlphaId and BetaId keys.

At first I thought I’d leverage AutoMapper’s custom conversion capabilities, but I don’t think that will work given that we would need a data session to be injected into the mapping (to call the data store to retrieve the Alpha or Beta object).

The other option is to just do all the work in the controller action, but that quickly gets unwieldy (not in the particular example given, but thats just an example to illustrate the point).

Where should the hydration of Alpha and Beta take place, and what’s a good pattern here?

  • 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-27T06:02:11+00:00Added an answer on May 27, 2026 at 6:02 am

    It perhaps isn’t ideal, but could you not just create maps for the two references and map them after the fact. You still have to call them seperately, but you’re letting AutoMapper do the heavy mapping, rather than filling your controller with your own maps.

    public ActionResult FooDetails(string id)
    {
        using(var session = this.documentStore.OpenSession())
        {
            var foo = session.Load<Foo>(id).Include(....);
            var alpha = session.Load<Alpha>(foo.AlphaId);
            var beta = session.Load<Beta>(foo.BetaId);
    
            // null checks
    
            var fooViewModel = Mapper.Map<FooViewModel>(foo);
            fooViewModel.Alpha = Mapper.Map<AlphaViewModel>(alpha);
            fooViewModel.Beta = Mapper.Map<BetaViewModel>(beta);
    
            return View(fooViewModel);
        }
    }  
    

    I haven’t used AutoMapper all that much, but perhaps you can pass in child objects through the Map function (to feed the ForMember chains on CreateMap)? Perhaps Alexandre’s AfterMap suggestion might work, but I don’t see how you’d hydrate the ‘referenced’ children without creating the map inside the controller action (but then you may as well just use ForMember and the Raven session directly and have only one Mapper.Map).

    Addition – The AutoMapper documentation suggests a similar method for nested/child maps – https://github.com/AutoMapper/AutoMapper/wiki/Nested-mappings. Although it’s more akin to an object reference, rather than a object id reference situation.

    I can’t comment on other answers yet, but with regard to Pavel’s answer – you don’t really use Raven in that way, there is no o/r mapping (other than the internal JSON -> object) or data access layer, you just use the Raven session directly (perhaps through a service, but there’s certainly no need for a repository or similar). With regards to referencing, byte has it right and as he commented on one of the other answers, the best approach is to store an id reference and use a Raven include – it’s the same result and still uses only one request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.