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

  • Home
  • SEARCH
  • 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 7580481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:55:43+00:00 2026-05-30T17:55:43+00:00

I’m building CMS like application. For example my BlogPost page contains several widget areas.

  • 0

I’m building CMS like application.

For example my BlogPost page contains several widget areas. Each widget hosts a serie of “related” blog posts.

typical widget

All my views are pure presentational, i build urls, convert datetime and ints into strings in my service layer. I find this approach easier to maintain, since views have zer0 logic. All logic is consolidated into AutoMapper’s resolvers, converters and custom transformation logic.

So lets come closer to the problem at hand.
To create Url i need 2 parameters: BlogId and BlogSlug, my urls look like b/{id}/{slug}.html
Im quite happy with that.

In my CSM i use so called “source models”, a model that is not view model, but an intermediate representation of it. Why do i have to resort to such wicked solutions ?
Well, lets take a look how a typical data retrieval code can look like in my project:

.Select(x => 
{
    Id = x.Id,
    BlogId = x.Blog.Id,
    BlogSlug = x.Blog.Slug,

    // Here is the trap, LINQ provider will throw an exception, since he doesn't know how to translate function into expression
    BlogUrl = Url.Action("RenderPost", "BlogController", new { Id = x.Blog.Id, slug = x.Blog.Slug }) 
}

So thats not an option.
Luckily, we can do this

.Select(x => new
{
    Id = x.Id,
    BlogId = x.Blog.Id,
    BlogSlug = x.Blog.Slug
}
.ToList()
.Select(x => new
{
    // This works
    BlogUrl = Url.Action("RenderPost", "BlogController", new { Id = x.BlogId, slug = x.BlogSlug })
}

Copy paste this stuff into each and every action method that renders different “intresting blog” parts (they have different visual representation as well cant use same view model) ? Not a good way, so i came up with a solution.
I created “source model”, so the code will be

.Select(x => new BlogPostSourceViewModel
{
    Id = x.Id,
    BlogId = x.Blog.Id,
    BlogSlug = x.Blog.Slug
}
.ToList()
.Select(x => x.ToBlogPostViewModel())  // Extension method { return Mapper.Map<>() }
.ToList();

This surely looks better, but i have many different models like BlogPostSourceViewModel, BlogAuthorSourceViewModel, BlogCommentSourceViewModel. They all need this link building logic.
Ok, i extract the needed source data (BlogId, BlogSlug) into an interface

BlogPostSourceViewModel : IBlogPostUrl
BlogAuthorSourceViewModel: IBlogPostUrl
BlogCommentSourceViewModel : IBlogPostUrl

Then i define the mappings

Mapper.CreateMap<BlogPostSourceViewModel, BlogPostViewModel>
    .ForMember(dest => dest.BlogUrl, opt => opt.ResolveUsing<BlogPostUrlResolver>())
Mapper.CreateMap<BlogAuthorSourceViewModel, BlogAuthorViewModel>
    .ForMember(dest => dest.BlogUrl, opt => opt.ResolveUsing<BlogPostUrlResolver>())
Mapper.CreateMap<BlogCommentSourceViewModel, BlogCommentViewModel>
    .ForMember(dest => dest.BlogUrl, opt => opt.ResolveUsing<BlogPostUrlResolver>())

Resolver:

BlogPostUrlResolver : ValueResolver<IBlogPostUrl, String>
// Here goes the url building logic

As you see the more models i have that need blog url the more identical mappings i have to add. This is ok for now, but as project grows it will be painful.
Ideally i would want to have it like this:

Mapper.CreateMap<IBlogPostUrl, SomeOtherInterfaceWithBlogUrlAsString>
    .ForMember(dest => dest.BlogUrl, opt => opt.ResolveUsing<BlogPostUrlResolver>())

but Automapper doesn’t understand it. And i dont know how if there is other way to do it.

Any ideas ?

  • 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-30T17:55:44+00:00Added an answer on May 30, 2026 at 5:55 pm

    If I understand the question correctly…

    In order to use AutoMapper mapping capabilities across all of your classes you can provide base class that will use Generics:

    public abstract class Base<Entity, ViewModel>   
        where Entity : EntityObject         
        where ViewModel : BaseViewModel     
        {
            // you will call this method from your operations class using base
            public SomeViewModel GetData()
            {
                public Entity entityObject = db.Entity.SingleOrDefault();
                public ViewModel yourViewModelName = base.Map(entityObject);
    
                return yourViewModelName;
            }
            ....
            // this will be defined only once for each mapping direction
            // ie. there will be multiple of these.
            public static Entity Map(ViewModel typeViewModel)
            {
                try
                {
                    Mapper.CreateMap<ViewModel, Entity>();
                    Entity t = Mapper.Map<ViewModel, Entity>(typeViewModel);
    
                    return t;
                }
                catch (Exception exc)
                {
                    throw exc;
                }
            }
        }
    
    // this will be your class for database operations on specific Entity
    // that inherits generic base, with its AutoMapping setup.
    public class DataBaseOperationsClass : Base<SomeEntity, SomeViewModel>
    {
        public SomeViewModel Get()
        { 
            return base.GetData();
        }
    }
    

    Hope this helps !

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.