我把这个自动装置放在哪里?

时间:2021-01-31 12:26:36

HI

I have 2 projects one for my repos and service layers(data project) and one for my views,controllers(webUI project).

我有两个项目,一个用于repos和服务层(数据项目),另一个用于视图和控制器(webUI项目)。

I am using automapper and I have been doing all my mapping in my controller. Say I had a request to get a item. It would go to my controller it would contact the service layer and any business logic would be done at this time.

我在使用automapper,我在我的控制器中做了所有的映射。比方说我有收到一个项目的请求。它会到达我的控制器,它会联系服务层,任何业务逻辑都将在此时完成。

I would get usually a domain model back and I would take that model and in the controller I would auto map it too a view model. Then send the View model back.

我通常会得到一个域模型然后我将那个模型在控制器中我也会自动映射它作为一个视图模型。然后将视图模型发回。

This has been working quite well as I been able to keep my mvc code(my viewModels and etc) out of my service layer.

这非常有效,因为我可以将mvc代码(我的视图模型等)从服务层中删除。

However there have been 2 cases where I need to use automapper to do mapping in the service layer.

然而,有两种情况我需要使用automapper在服务层中进行映射。

This mapping in the service layer is too other domain objects and has been quite small right now(only a few properties).

服务层中的这个映射是其他的域对象,并且现在非常小(只有几个属性)。

Should I be doing mapping in my service layer?

我应该在服务层中进行映射吗?

If so where do I stick these mappings? Right now I have it just in my project with my controllers that get registered in application start.

如果是这样,我该把这些映射放在哪里?现在我在我的项目中有它我的控制器在application start中注册。

So one option could be to put my mapping where I been putting mapping(in a class in my models folder). The problem with this would be that if I take my service layer and pop it into another project(say a mobile device) then I have to redo all the mapping as it won't exist.

因此,一个选项可以是将映射放在我放置映射的地方(在我的models文件夹中的一个类中)。这样做的问题是,如果我将服务层拖放到另一个项目中(比如移动设备),那么我必须重新执行所有映射,因为它不存在。

So any ideas?

所以任何想法?

1 个解决方案

#1


2  

Have a class in your service layer called "ServiceLayerMappings" and call that from application start.

在服务层中有一个名为“ServiceLayerMappings”的类,从应用程序开始调用它。

If you reuse the service layer just call ServiceLayerMappings.MapThisStuff() or whatever and you're all set.

如果重用服务层,只需调用ServiceLayerMappings.MapThisStuff()或其他,就可以了。

public class ServiceLayerMappings
{
     public void Map()
     {

        Mapper.CreateMap<MyServiceClass, ServiceDto>();
     }
}

Global.asax.cs

Global.asax.cs

    protected void Application_Start(object sender, EventArgs e)
    {
         new ServiceLayerMappings().Map()

You could of course make this static or rename. It doesn't matter.

当然,您可以将其设置为静态或重命名。没关系。

#1


2  

Have a class in your service layer called "ServiceLayerMappings" and call that from application start.

在服务层中有一个名为“ServiceLayerMappings”的类,从应用程序开始调用它。

If you reuse the service layer just call ServiceLayerMappings.MapThisStuff() or whatever and you're all set.

如果重用服务层,只需调用ServiceLayerMappings.MapThisStuff()或其他,就可以了。

public class ServiceLayerMappings
{
     public void Map()
     {

        Mapper.CreateMap<MyServiceClass, ServiceDto>();
     }
}

Global.asax.cs

Global.asax.cs

    protected void Application_Start(object sender, EventArgs e)
    {
         new ServiceLayerMappings().Map()

You could of course make this static or rename. It doesn't matter.

当然,您可以将其设置为静态或重命名。没关系。