When do I pass RegistrationBuilder to a directory?

With the new smooth MEF programming model, if I have several directories:

  • To which directory do I pass RegistrationBuilder
  • Do I need to transfer a RegistrationBuilder call to SatisfyImportsOnce ?
  • Which SatisfyImportsOnce or ComposeParts use? (did something change with that in fluent mef?)

eg. Here is an example to illustrate my confusion (see Comments on rhs):

 // Get pre-wired registration builder RegistrationBuilder rb = new MefCompositionRoot().CommonRegistrationBuilder(); // Register this WCF service class rb.ForType<LogService>().Export<LogService>(); var assembly = typeof (LogService).Assembly; var assemblyCatalog = new AssemblyCatalog(assembly, rb); // <-- HERE? var dirCatalog = new DirectoryCatalog("bin", rb); // <-- and HERE? // Combine catalogs var catalog = new AggregateCatalog(); catalog.Catalogs.Add(dirCatalog); catalog.Catalogs.Add(assemblyCatalog); var container = new CompositionContainer(catalog); container.SatisfyImportsOnce(this, rb);// THIS? container.ComposeParsts(this); ///or THIS? 
+4
source share
1 answer
  • You can put RegistrationBuilder in any of the directories with which you want to use the MEF conventions. If you want to use only the legend model, use it in all directories. If DirectoryCatalog in your sample code loads assemblies containing only export / import attributes, then you will not need RegistrationBuilder . Note that both models (Attributed and Contentions) can coexist . Thus, you can add RegistrationBuilder to all directories if there cannot be a type that satisfies a rule (configured with RegistrationBuilder) that you do not want to use in the CompositionContainer .
  • This overload of SatisfyImportsOnce is a mystery. From the documentation (and from a quick look at the MEF source), it looks like you can use a specific adboc for RegistrationBuilder. In fact, I managed to use it only with sample code.
  • SatisfyImportsOnce disables rebuilding . Check out this great answer on this topic.

SatisfyImportsOnce example (Object, ReflectionContext)

 private static void TestLateRegistration_SameBuilder_Ok() { var rb = new RegistrationBuilder(); var assemblyCatalog = new AssemblyCatalog(typeof(LogService).Assembly, rb); using (var container = new CompositionContainer(assemblyCatalog)) { rb.ForType<LogService>().Export(); var server = new TypeImportingLogService(); //Use the same RegistrationBuilder. container.SatisfyImportsOnce(server, rb); } } 
+1
source

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


All Articles