Asp.net mvc 3 在何处放置子命名空间共享的类?

Asp.net mvc 3 在何处放置子命名空间共享的类?,asp.net-mvc-3,automapper,Asp.net Mvc 3,Automapper,传统智慧认为不要从父母那里调用子命名空间 假设我正在使用类似于AutoMapper的东西将ASP.NET MVC 3站点的内容模型转换为viewmodels 我的目录结构与此类似: - Stuff.Content -Foo.cs - Stuff.Content.Public -Controllers -FooController.cs -Models -FooViewModel.cs -Views -Foo

传统智慧认为不要从父母那里调用子命名空间

假设我正在使用类似于AutoMapper的东西将ASP.NET MVC 3站点的内容模型转换为viewmodels

我的目录结构与此类似:

- Stuff.Content
    -Foo.cs
- Stuff.Content.Public
    -Controllers
        -FooController.cs
    -Models
        -FooViewModel.cs
    -Views
        -Foo
            -Index.cshtml
 - AutoMapperConfig.cs
 - Global.asax
AutoMapperConfig.cs在本例中,它只是一个简单的静态类,使用静态方法设置映射,如下所示:

public static class AutoMapperConfig
{
    public static void Configure()
    {
        Mapper.CreateMap<Foo, FooViewModel>();
    }
}
公共静态类AutoMapperConfig
{
公共静态void Configure()
{
CreateMap();
}
}
您会注意到我在公共项目的根目录中有AutoMapperConfig,但它实际上是在调用子命名空间(Stuff.Content.public.Models

调用该子命名空间是否可以接受?AutoMapperConfig是否应该与viewmodels一起存在于Models命名空间中

由于控制器名称空间中的控制器调用其同级模型名称空间被认为是正常的,因此在这个领域似乎变得模糊起来


期待着您的想法。谢谢。

我认为你目前的设计没有任何问题。我个人将映射配置放入
Mappings
子文件夹:

- Stuff.Content
    -Foo.cs
- Stuff.Content.Public
    -Controllers
        -FooController.cs
    -Models
        -FooViewModel.cs
    -Views
        -Foo
            -Index.cshtml
    -Mappings
        -AutoMapperConfig.cs
 - Global.asax
此外,我倾向于为每个域模型定义一个单独的映射文件

-Mappings
   -MappingRegistry.cs
   -FooProfile.cs
   -BarProfile.cs
   -...
下面是
FooProfile.cs
的一个示例:

public class FooProfile: Profile
{
    protected override void Configure()
    {
        CreateMap<Foo, FooViewModel>();
    }
}
public static class MappingRegistry
{
    public static void Configure()
    {
        Mapper.Initialize(
            x => typeof(MappingRegistry)
                .Assembly
                .GetTypes()
                .Where(type => !type.IsAbstract && typeof(Profile).IsAssignableFrom(type))
                .ToList()
                .ForEach(type => x.AddProfile((Profile)Activator.CreateInstance(type)))
        );
    }
}

这很酷,但如果我理解正确的话,您已经为要映射的每种类型创建了一个单独的配置文件。与只在一个类中的单个configure中调用createmap相比,进行该设置(并使用反射)有什么好处?