将Mapster映射代码放在EF Core 3和ASP.Net MVC Core中的哪里? [英] Where to put Mapster mapping code in EF Core 3 and ASP.Net MVC Core?

查看:589
本文介绍了将Mapster映射代码放在EF Core 3和ASP.Net MVC Core中的哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用ASP.Net MVC Core,并试图了解与ASP.Net MVC Framework之间的某些区别。

I'm just starting to work with ASP.Net MVC Core and am trying to understand some of the differences between that and ASP,Net MVC Framework.

I使用Mapster库来组织数据对象和视图模型之间的映射。在旧世界中,我将使用DTO创建一个Mapping Configuration文件来查看模型映射,然后在启动时调用它。在核心世界中,是否有最佳做法来做到这一点?我认为启动类会调用某些东西?

I use the Mapster library to organize the mappings between my data objects and the view models. In the old world I would create a Mapping Configuration file with my DTO to viewmodel mappings and then call that at startup. Is there a best practice way to do this in the Core world? I assume something that gets called in the startup class?

任何建议或示例都将不胜感激。

Any suggestions or examples would be appreciated.

推荐答案

在asp.net核心中,您可以将映射代码放入启动 Configure 方法

In asp.net core, you could put your mapping code in startup Configure method

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        TypeAdapterConfig<Employee, EmployeeViewModel>.NewConfig()
                        .Map(dest => dest.Name, src => src.FirstName + " " + src.LastName);
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

请参阅 https://www.codeproject.com/Articles/1249355/Mapster-Your-Next-级别的对象到对象的映射T

这篇关于将Mapster映射代码放在EF Core 3和ASP.Net MVC Core中的哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆