Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Where to put a class shared by child namespace?
  2. - Stuff.Content
  3.     -Foo.cs
  4. - Stuff.Content.Public
  5.     -Controllers
  6.         -FooController.cs
  7.     -Models
  8.         -FooViewModel.cs
  9.     -Views
  10.         -Foo
  11.             -Index.cshtml
  12.  - AutoMapperConfig.cs
  13.  - Global.asax
  14.        
  15. public static class AutoMapperConfig
  16. {
  17.     public static void Configure()
  18.     {
  19.         Mapper.CreateMap<Foo, FooViewModel>();
  20.     }
  21. }
  22.        
  23. - Stuff.Content
  24.     -Foo.cs
  25. - Stuff.Content.Public
  26.     -Controllers
  27.         -FooController.cs
  28.     -Models
  29.         -FooViewModel.cs
  30.     -Views
  31.         -Foo
  32.             -Index.cshtml
  33.     -Mappings
  34.         -AutoMapperConfig.cs
  35.  - Global.asax
  36.        
  37. -Mappings
  38.    -MappingRegistry.cs
  39.    -FooProfile.cs
  40.    -BarProfile.cs
  41.    -...
  42.        
  43. public class FooProfile: Profile
  44. {
  45.     protected override void Configure()
  46.     {
  47.         CreateMap<Foo, FooViewModel>();
  48.     }
  49. }
  50.        
  51. public static class MappingRegistry
  52. {
  53.     public static void Configure()
  54.     {
  55.         Mapper.Initialize(
  56.             x => typeof(MappingRegistry)
  57.                 .Assembly
  58.                 .GetTypes()
  59.                 .Where(type => !type.IsAbstract && typeof(Profile).IsAssignableFrom(type))
  60.                 .ToList()
  61.                 .ForEach(type => x.AddProfile((Profile)Activator.CreateInstance(type)))
  62.         );
  63.     }
  64. }