I'm refreshing my knowledge about Automapper API.
Exploring IMapper
interface, I found several posts about dependency injection and Automapper, e.g. this and this (they are about .NET Core, but this doesn't matter in context of the question).
Assuming, that typical use of mappers like Automapper is:
public class Person
{
// person properties here
}
public class PersonDto
{
// person DTO properties here
}
public class SomeApi
{
// other code here
public PersonDto FindByName(string name)
{
var person = dbContext.People.FirstOrDefault(_ => _.Name == name);
// mapper is IMapper
return mapper.Map<PersonDto>(person);
}
}
why we may want to inject and/or mock IMapper
?
I understand, why injecting things like dbContext
could be useful - when writing a unit test, you have to setup test environment somehow to make testing code isolated from outer world.
But what's the point for mapper?
I mean, mapping is a part of method's logic. It's not an "outer world". This is what method was written for, and this is what must be tested, but not mocked.
If I rewrite FindByName
without using Automapper, the code will look like this:
public PersonDto FindByName(string name)
{
var person = dbContext.People.FirstOrDefault(_ => _.Name == name);
if (person == null)
return null;
return new PersonDto
{
Name = person.Name,
// etc
};
}
So, what do we injecting/mocking here? Assignment operators? Seriously?
It looks like dependency injection for dependency injection without any benefit.
Am I missing something? Maybe, there are cases, when injecting/mocking Automapper is really useful (I'm interested in cases found in practice, not in theoretic ones)?