当前位置: 动力学知识库 > 问答 > 编程问答 >

express mapper - ExpressMapper, .NET Core, and Dependency Injection

问题描述:

I'm evaluating different object-to-object mappers for a .NET Core web API. One of the requirements I have is to use the built-in Dependency Injection container of .NET Core. I have tested Automapper, which is successful, but I'm interested in trying ExpressMapper. http://www.expressmapper.org/ The examples to configure are very sparse.

I see from Github page it appears to be .netcore ready.

https://github.com/fluentsprings/ExpressMapper

How can I configure ExpressMapper in startup.cs so that I can DI into a controller, similar to Automapper?

If someone had a quick startup.cs example for ExpressMapper and .NET Core that would be great.

(And as an aside, if someone was using another competitive mapper tool with .NET Core that they would recommend, that would also be helpful).

With Automapper I can add a MappingProfile class then do something like

Startup.cs

 private MapperConfiguration _mapperConfiguration { get; set; }

_mapperConfiguration = new MapperConfiguration(cfg =>

{

cfg.AddProfile(new MappingProfile());

});

ConfigureServices

 services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());

Controller

 public ValuesController(IMapper mapper)

{

_mapper = mapper;

}

网友答案:

The same with Expressmapper you can use IMappingServiceProvider interface and MappingServiceProvider implementation that Mapper static class uses. Please take a look at the following example and here is the link to see it in real:

public class Program
{
    public static void Main()
    {

        var container = new Container();

        container.Register<IMappingServiceProvider, MappingServiceProvider>();

        var mapper = container.GetInstance<IMappingServiceProvider>();

        var result = mapper.Map<Test, TestResponse>(new Test{Name = "Just a test"});

        Console.WriteLine("result is {0} type", result as TestResponse);
        Console.WriteLine("result.Name = {0}", result.Name);
    }

    public class Test{
        public string Name {get;set;}
    }

    public class TestResponse{
        public string Name {get;set;}
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: