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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:44:20+00:00 2026-05-26T21:44:20+00:00

How do I avoid requiring code like this: public static class BusinessLogicAutomapper { public

  • 0

How do I avoid requiring code like this:

public static class BusinessLogicAutomapper
{
    public static bool _configured;

    public static void Configure()
    {
        if (_configured)
            return;

        Mapper.CreateMap<Post, PostModel>();
        _configured = true;
    }
}

in my BL assembly, and having to call Configure() from my Global.asax in my MVC application?

I mean, I expect a call like this:

    public PostModel GetPostById(long id)
    {
        EntityDataModelContext context = DataContext.GetDataContext();
        Post post = context.Posts.FirstOrDefault(p => p.PostId == id);

        PostModel mapped = Mapper.Map<Post, PostModel>(post);
        return mapped;
    }

to Mapper.Map<TIn,TOut> to produce the mapper if it isn’t in existance, instead of having to create it myself manually (I shouldn’t even know about this inner working). How can I work around declaratively creating mappers for AutoMapper?

A solution that’s natural to AutoMapper would be desired, but an extension or some architectural change in order to avoid this initialization would work too.

I’m using MVC 3, .NET 4, and no IoC/DI (yet, at least)

  • 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-26T21:44:21+00:00Added an answer on May 26, 2026 at 9:44 pm

    I completely misunderstood what you were trying to do in my original answer. You can accomplish what you want by implementing part of the functionality of AutoMapper using reflection. It will be of very limited utility and the more you extend it, the more like AutoMapper it will be so I’m not sure that there’s any long term value to it.

    I do use a small utility like what you are wanting to automate my auditing framework to copy data from a entity model to its associated audit model. I created it before I started using AutoMapper and haven’t replaced it. I call it a ReflectionHelper, the below code is a modification of that (from memory) — it only handles simple properties but can be adapted to support nested models and collections if need be. It’s convention-based, assuming that properties with the same name correspond and have the same type. Properties that don’t exist on the type being copied to are simply ignored.

    public static class ReflectionHelper
    {
          public static T CreateFrom<T,U>( U from )
              where T : class, new
              where U : class
          {
                var to = Activator.CreateInstance<T>();
                var toType = typeof(T);
                var fromType = typeof(U);
    
                foreach (var toProperty in toType.GetProperties())
                {
                    var fromProperty = fromType.GetProperty( toProperty.Name );
                    if (fromProperty != null)
                    {
                       toProperty.SetValue( to, fromProperty.GetValue( from, null), null );
                    }
                }
    
                return to;
          }
    

    Used as

        var model = ReflectionHelper.CreateFrom<ViewModel,Model>( entity );
    
        var entity = ReflectionHelper.CreateFrom<Model,ViewModel>( model );
    

    Original

    I do my mapping in a static constructor. The mapper is initialized the first time the class is referenced without having to call any methods. I don’t make the logic class static, however, to enhance its testability and the testability of classes using it as a dependency.

    public class BusinessLogicAutomapper
    {
        static BusinessLogicAutomapper
        {
            Mapper.CreateMap<Post, PostModel>();
            Mapper.AssertConfigurationIsValid();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to avoid code like this when reusing the same ViewUserControl in ASP.NET
I have a C++ application that can be simplified to something like this: class
I just stumbled across this bug in some legacy code: class MyAPIHandler { private:
I have a Base Class with two constructors, requiring a parameter: public abstract class
I always try to avoid to return string literals, because I fear they aren't
This might sound like a weird idea and I haven't thought it through properly
I wanted to draw special characters like this one ( http://www.fileformat.info/info/unicode/char/2605/index.htm ) in JLabel.
This is a recurring question for me, but I'd like to reiterate. Quickly explain
(This question is not about programming, but about how to avoid doing any programming.
From MSDN : This method prints the current document without requiring further user input.

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.