1) Création du container MEF et déclaration IoC des interfaces :
- private CompositionContainer container;
- protected override void Configure()
- {
- // Create MEF Container
- var catalog =
- new AggregateCatalog(
- AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
- container = new CompositionContainer(catalog);
- var batch = new CompositionBatch();
- // IoC Interfaces Registering
- batch.AddExportedValue<IMyInterface>(new MyImplementation1());
- batch.AddExportedValue<IMyInterface2>(new MyImplementation2());
- // ...
- // Automapper configuration
- ConfigurationStore configurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers());
- batch.AddExportedValue<IConfiguration>(configurationStore);
- batch.AddExportedValue<IConfigurationProvider>(configurationStore);
- batch.AddExportedValue<IMappingEngine>(new MappingEngine(configurationStore));
- batch.AddExportedValue(container);
- container.Compose(batch);
- }
2) Import IoC via constructeur du controlleur / viewmodel ...etc
- [ImportingConstructor]
- MyViewModel(IService<Foo> fooService, ... , IMappingEngine mapper)
- {
- _fooService = fooService;
- // .. other services
- _mapper = mapper;
- }
3) Utilisation du mapper :
- FooDto myfooDtoInstance = _mapper.Map<Foo, FooDto>(myfooInstance);
Lien pour la configuration IoC d'Automapper avec Unity :
http://blog.aspnet.sk/michalkohut/archive/2011/09/10/konfigur-225-cia-automapper-a-s-ioc.aspx
0 commentaires:
Enregistrer un commentaire