question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-static version of Mapper

See original GitHub issue

Things to do:

  • Introduce non-static API
  • Obsolete existing static API
  • Provide bridge for LINQ #1038
  • Read-only configuration #1039

I want to move to a non-static version of AutoMapper, that would get rid of so many headaches on my part of threading and junk. I’m thinking of something like:

var mapper = new Mapper(cfg => cfg.CreateMap<Source, Dest>());

var dest = mapper.Map<Source, Dest>(new Source());

I’d have overloads for the ctor:

public Mapper(Action<IConfiguration> configure);

public Mapper(IConfigurationProvider configuration);

And the configuration provider, if you wanted to initialize that by itself, could have a ctor too:

public class Configuration(Action<IConfigurationProvider> configure) {
   configure(this);
}

Technically configuration is still mutable after Seal(), since you can still go into TypeMaps and do whatever, grab the instance of the configuration and so on.

Motivation

The static has been haunting me for years. Threading issues, order-of-operations, I can’t fix that until I get rid of the statics. The changes in inheritance in 4.x have opened enough issues that I want to be deliberate about this move.

Upgrade path

If you’re using Mapper.Initialize, it’s a matter of replacing the outer path with the real thing. I want to use the Mapper class and a Configuration class. I would obsolete all the static methods, obsolete the MappingEngine class and make the Mapper class inherit from it for 4.2. Then in 5.0, remove all the static.

Users, instead of calling Mapper.Map, would use a mapper instance directly, which they can make static all they like. Or configure in a container, whatever.

Static compromise

Another option is to keep Mapper.Initialize and Mapper.Map and nothing else. Still include static field for the mapper and the configuration instances. I would still remove the static keyword from Mapper and move the MappingEngine pieces inside of Mapper, with the clear signal that “you might want to use the instance methods”.

I want to move to a class called “Mapper” and in C# a static method hides an instance method of the same name. It’s basically static or nothing. I could do something like Mapper.Engine.Map but that seems hokey.

@TylerCarlson1 @lbargaoanu thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:38 (29 by maintainers)

github_iconTop GitHub Comments

2reactions
long2knowcommented, Feb 4, 2016

I have no qualms with the new approach, but some things do not work the same and was wondering if you could provide some guidance.

One aspect that I am unsure as I migrate code from 3.x is mapping custom collections. In particular, I have a PaginatedList<T> which inherits from List<T>, but includes paging properties like page number, sortby, etc.

The old way to deal with this was to have an extension method that would map the custom members and then use AfterMap to map the enumerable elements.

It looked something like this:

Mapper.CreateMap<Type1, Type2>().IncludePagedResultMapping();

public static IMappingExpression<TSrc, TDest> IncludePagedResultMapping<TSrc, TDest>(this IMappingExpression<TSrc, TDest> expression)
{
  Mapper.CreateMap<PaginatedList<TSrc>, PaginatedList<TDest>>()
.ForMember(dest => dest.PageNumber, opt => opt.MapFrom(src => src.PageNumber))
.ForMember(dest => dest.SortBy, opt => opt.MapFrom(src => src.SortBy))
.AfterMap((s, d) => Mapper.Map<List<TSrc>, List<TDest>>(s, d));

return expression;
}

However, changing this to create a Mapper in the AfterMap (.AfterMap(s,d) => _config.CreateMapper().Map(....)) causes a stackoverflow.

Is there a preferred, working approach to these types of scenarios in 4.x? I looked at the wiki regarding Open Generics, and that didn’t seem to be the right path.

0reactions
lock[bot]commented, May 7, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automapper Object reference is required for the non static ...
I recently Upgraded my .net core to 3.0 and Automapper from 6.2 to 9.0. Now the automapper is throwing the following compile time...
Read more >
How can I fix this? - Microsoft Q&A
I looked at Mapper's Map method and it doesn't seem to be static. Test3. Actually, this has to do with the version of...
Read more >
AutoMapper and the Static Class Debate
One of my biggest regrets was that I made AutoMapper have a static API from literally the very beginning. The first tests/prototypes of ......
Read more >
C# Error: How to Resolve an object reference is required ...
How to fix: Object Reference is required for the non-static field? Track, Analyze and Manage Errors With Rollbar; References. The C# “an object ......
Read more >
Upgrading AutoMapper - CraigTP's Blog
The problem here is that, in a large enough codebase, moving from purely static mappings as was done with AutoMapper prior to v4.2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found