How to give a registrar instance to each class that has a property of type ILogger using MEF

Is it possible to inject a registrar instance for each class that has a property of type ILogger using MEF. Is there any other solution that makes up each type of class separately. I tried to find the batch composition, but it looked very tiring for me, since every class that needs a journal instance must be compiled in a package. Is there any other good solution? Thanks in advance.

+4
source share
1 answer

Well, as a rule, in MEF you allow MEF to build the object, which will allow it to automatically compose the objects correctly and automatically set the ILogger properties using the [Import] tags.

Saying this, this does not always work in every scenario. If you cannot create MEF objects for your objects because they are generated from a separate source, there are other options.

For example, this common problem occurs in WPF and Silverlight. When you use these technologies, you often want the XAML parser to be able to create your objects, but in this case they will never be linked.

Silverlight 4 adds the CompositionInitializer class to handle this situation. This allows you to simply add this to your constructor:

public MyClass() // MyClass Constructor { CompositionInitializer.SatisfyImports(this); } 

And the CompositionInitializer will use the directories defined in the static class called CompositionHost to create the constructed object. It seems like this could be a good alternative in your situation, as well ...

There is currently no official version of this desktop. Glenn Block has published an older port for use on a desktop computer (called PartInitializer, which was the old name) for its SkyDrive, which works quite well. It’s easy enough to migrate the current version of SL for use in desktop applications, although this will require manual porting.

It is said that it was said (on Twitter and elsewhere) that the CompositionInitializer for use on the desktop will soon be added to the MEF codeplex site ...

+4
source

Source: https://habr.com/ru/post/1303723/


All Articles