in a WPF application I'm using Caliburn Micro for MVVM pattern... I want to try another IoC and want to reuse the most of the existing code...
In my application I've defined all the exportable class via attribute as
[Export(typeof(ITaggable))]
[Export(typeof(CorporateActionViewModel))]
[Export(typeof(IScreen))]
public class CorporateActionViewModel :...
How can I register them without doing manually
ContainerInstance.Register<ITaggable, CorporateActionViewModel>();
ContainerInstance.Register<IScreen, CorporateActionViewModel>();
ContainerInstance.Register<CorporateActionViewModel, CorporateActionViewModel>();
Another question is regarding the Lazy initialization... I've read here how to register lazy... but do I have to call the Container.Verify() or not?
Thanks
The use of the ExportAttribute
thoughout your complete source just to register all your types sounds like a violation of the Dependency Inversion Principle. Which is on its own questionable but it has several disadvantages for sure.
Simple Injector has no need for using attributes to find the classes you want to register. It is actually one of the design principles of the Simple Injector crew.
You could easily (well easy... depending on your current design offcourse...) remove the attribute if you follow the SOLID principles for your viewmodels (and corresponding views).
If we take a typical LoB application where we have a bunch of entities in a database we could split our viewmodel/view design in these generic interfaces which your viewmodels will implement (one at a time offcourse):
//for a typical datagrid view of your entities with e.g. Add, Edit and Delete button
IWorkspace<TEntity>;
//for a typical edit view for one entity (including possible child entities)
IEditEntity<TEntity>;
//for choosing a specific foreign entity type from your edit view
//e.g. your editing an order and need to specify the customer
IChooseEntity<TEntity>
Using these we will get very specific viewmodels which are SOLID and which still could be composed to a very big complicated view for the user if you wish.
You could register these types very easily with Simple Injector using a batch registration like this:
container.RegisterManyForOpenGeneric(
typeof(IChooseEntityViewModel<>), Assembly.GetExecutingAssembly());
As a bonus of this design you could wrap your viewmodels with one or more decorators which could be used to some real MVVM stuff like find your view, bind it to the viewmodel and show the view in window/page etc. If you want to read more about decorators, in combination with simple injector you can find some nice articles here (don't forget the various links).