2012-08-06 26 views
5

Có phải là một thực tế xấu hoặc có mùi mã để sử dụng một thùng chứa IoC trong khi cài đặt các phụ thuộc không?Có phải là một thực tế xấu hoặc có mùi mã để sử dụng một thùng chứa IoC trong khi cài đặt các phụ thuộc không?

Đây là gốc thành phần của tôi:

public void Install(IWindsorContainer container, IConfigurationStore store) 
{ 
    Assembly modelAssembly = typeof(UserLoginModel).Assembly; 
    Assembly controllerAssembly = typeof(HomeController).Assembly; 

    container.Install(
     new MvcInfrastructureInstaller(modelAssembly, viewAssembly, controllerAssembly, applicationTitle, resourceAssemblyLocations), 
     new MiniMembershipInstaller(), 
     new ServiceInstaller(), 
     new RepositoryInstaller(), 
     new LibraryInstaller(), 
     new AutoMapperProfileInstaller() // this installer needs to resolve dependencies such as repositories through the container itself, so it goes last. 
    ); 
} 

AutoMapperProfileInstaller nhu cầu của tôi để giải quyết một hồ sơ, trong đó có phụ thuộc để khởi tạo các mapper

public class AutoMapperProfileInstaller : IWindsorInstaller 
{ 
    public void Install(IWindsorContainer container, IConfigurationStore store) 
    { 
     Profile entityToViewModel = container.Resolve<EntityToViewModelProfile>(); 
     Profile[] profiles = new[] { entityToViewModel }; 

     Mapper.Initialize(config => 
     { 
      config.ConstructServicesUsing(container.Resolve); 

      foreach (Profile profile in profiles) 
      { 
       config.AddProfile(profile); 
      } 
     }); 
    } 
} 

này cảm thấy sai lầm trên nhiều cấp độ, điều gì sẽ cách tốt hơn để khởi tạo AutoMapper tiểu sử?

Trả lời

0

Điều duy nhất sai với cách tiếp cận đó là bạn chỉ định thủ công việc triển khai IWindowsInstaller. Sử dụng sự phản chiếu để tìm chúng và Activator.CreateInstance để nhanh chóng triển khai.

Nó cung cấp cho bạn phương pháp cấu hình linh hoạt trong đó mỗi phần/mô-đun trong hệ thống của bạn chịu trách nhiệm đăng ký riêng của nó.

Tuy nhiên, tôi sẽ tạo giao diện mới cho cấu hình AutoMapper IMapperConfiguration (Trách nhiệm duy nhất). Sử dụng sự phản chiếu để quét các hội đồng cho những người quá.