public void UpdatePlugins() { composablePart = AttributedModelServices.CreatePart(this); CompositionBatch batch = new CompositionBatch(new[] { composablePart }, Enumerable.Empty <ComposablePart>()); PluginsContainer.Compose(batch); }
private void AddResourceImpl(IEnumerable <IResource> formats, CompositionContainer container) { foreach (var impl in formats) { try { var part = AttributedModelServices.CreatePart(impl); if (part.ImportDefinitions.Any()) { container.SatisfyImportsOnce(part); } } catch (Exception X) { System.Diagnostics.Trace.WriteLine(X.Message, impl.Tag); } foreach (var ext in impl.Extensions) { m_extension_map.Add(ext.ToUpperInvariant(), impl); } foreach (var signature in impl.Signatures) { m_signature_map.Add(signature, impl); } } }
public void Pipeline_ShouldInitializeTheCorrectModule() { var container = new CompositionContainer(); var clockService = new ClockService(); var factory = new Mef.ClassFactory(); var module = new ValidateAccount4Module(clockService, factory); var accountingPipeline = Substitute.For <AccountingPipeline>(); var clockServicePart = AttributedModelServices.CreatePart(clockService); var factoryPart = AttributedModelServices.CreatePart(factory); var modulePart = AttributedModelServices.CreatePart(module); var accountingPipelinePart = AttributedModelServices.CreatePart(accountingPipeline); var accountingPipelineContext = new AccountingPipelineContext(); //Manual composition var batch = new CompositionBatch(new List <ComposablePart> { clockServicePart, factoryPart, modulePart, accountingPipelinePart }, null); container.Compose(batch); Mef.ClassFactory.Set(container); var pipeline = accountingPipelineContext.CreatePipelineHost(factory); pipeline.Execute(); accountingPipeline.Received(1).ValidateAccount += Arg.Any <ModuleDelegate <IAccountingPipelineContext> >(); //Manual cleanup to prevent memory leaks that will affect other tests batch.RemovePart(clockServicePart); batch.RemovePart(factoryPart); batch.RemovePart(modulePart); batch.RemovePart(accountingPipelinePart); Mef.ClassFactory.Dispose(); }
public static CompositionBatch AddValue <T>(this CompositionBatch batch, string contractName, T value) { var contractExport = CreateExportDefinition(contractName, typeof(T)); var partDefinition = CreatePartDefinition(Enumerable.Empty <ImportDefinition>(), contractExport, typeof(T)); var part = AttributedModelServices.CreatePart(partDefinition, value); batch.AddPart(part); return(batch); }
public void CreatePart_From_InvalidPartDefiniton_ShouldThrowArgumentException() { Assert.Throws <ArgumentException>("partDefinition", () => { var partDefinition = new ConcreteCPD(); var instance = new CPDTest(); AttributedModelServices.CreatePart(partDefinition, instance); }); }
public void WhenSatisfyingImportsOnce_ThenDelegates() { var delegating = new ContainerCompositionServiceAdapter(this.container); var consumer = new FooConsumer(); delegating.SatisfyImportsOnce(AttributedModelServices.CreatePart(consumer)); Assert.NotNull(consumer.Foo); }
public void WhenSatisfyingImportsOnce_ThenDelegates() { var composition = new Mock <INuPatternCompositionService>(); var delegating = new DelegatingCompositionService(composition.Object); delegating.SatisfyImportsOnce(AttributedModelServices.CreatePart(new object())); composition.Verify(c => c.SatisfyImportsOnce(It.IsAny <ComposablePart>())); }
/// <summary> /// 對指定的對象實例進行合成處理 /// </summary> /// <param name="target">對象實例</param> public void Compose(object target) { // 為該對象創建一個可組合的部件 var part = AttributedModelServices.CreatePart(target); // 該組合部件中存在所需導入的對象 if (part.ImportDefinitions.Any()) { // 對該部件進行導入 _container.SatisfyImportsOnce(part); } }
protected InterceptingComposablePartContext() { MockPart = new Mock <ComposablePart>(); InterceptedPart = AttributedModelServices.CreatePart(new OrderProcessor()); LoggerImportDefinition = InterceptedPart.ImportDefinitions.First(); OrderProcessorExportDefinition = InterceptedPart.ExportDefinitions.First(); MockInterceptor = new Mock <IExportedValueInterceptor>(); Exports = new List <Export> { new Export(OrderProcessorExportDefinition, () => new Logger()) }; InterceptingPart = new InterceptingComposablePart(InterceptedPart, MockInterceptor.Object); }
/// <summary> /// Creates a ComposablePart from an attributed object and adds it to the composition</summary> /// <param name="composer">IComposer</param> /// <param name="attributedPart">Attributed object to add to the composition</param> /// <returns>ComposablePart from the attributed object</returns> /// <remarks>This method creates a ComposablePart from the attributed object, adds it to a CompositionBatch /// and then executes composition on this CompositionBatch.</remarks> public static ComposablePart AddPart(this IComposer composer, object attributedPart) { Requires.NotNull(composer, "composer"); Requires.NotNull(attributedPart, "attributedPart"); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); var batch = new CompositionBatch(new ComposablePart[] { part }, Enumerable.Empty <ComposablePart>()); composer.Container.Compose(batch); return(part); }
private ComposablePart CreatePart(Delegate valueFactory) { var args = GetArguments(valueFactory); var value = valueFactory.DynamicInvoke(args); var part = AttributedModelServices.CreatePart(_composablePartDefinition, value); foreach (var import in _imports) { part.SetImport(import.Key, import.Value); } _imports.Clear(); return(part); }
static void Main(string[] args) { CompositionContainer container = new CompositionContainer(); CompositionBatch batch = new CompositionBatch(); batch.AddPart(AttributedModelServices.CreatePart(new Part1())); batch.AddPart(AttributedModelServices.CreatePart(new Part2())); batch.AddPart(AttributedModelServices.CreatePart(new Part3())); container.Compose(batch); Part3 _part = container.GetExportedValue <Part3>(); Console.WriteLine(_part.data.data.data); Console.ReadLine(); }
public void CreateGraph() { // Make sure VS has loaded before using MEF (prevents native access violations!) VSHost.EnsureSolution(@"LessDependencies\LessDependencies.sln"); // Don't create the instance using MEF, to ensure that I get a fresh graph for each test. graph = new LessDependencyGraph(WebEditor.ExportProvider.GetExport <IFileExtensionRegistryService>().Value); graph.IsEnabled = true; // Add the instance to the MEF catalog so that its IFileSaveListener is picked up graphPart = AttributedModelServices.CreatePart(graph); var cc = (CompositionContainer)WebEditor.CompositionService; cc.Compose(new CompositionBatch(new[] { graphPart }, null)); }
/// <summary> /// Creates composable parts from an array of attributed objects and composes them in the specified composition container. /// </summary> /// <param name="container">The composition container to perform composition in.</param> /// <param name="attributedParts">An array of attributed objects to compose.</param> public static void ComposeParts(this ICompositionContainer container, params object[] attributedParts) { Requires.NotNull(container, nameof(container)); Requires.NotNull(attributedParts, nameof(attributedParts)); ComposablePart[] parts = new ComposablePart[attributedParts.Length]; for (int i = 0; i < parts.Length; i++) { parts[i] = AttributedModelServices.CreatePart(attributedParts[i]); } CompositionBatch batch = new CompositionBatch(parts, Enumerable.Empty <ComposablePart>()); container.Compose(batch); }
[ActiveIssue(25498, TestPlatforms.AnyUnix)] // System.ArgumentException: ComposablePartDefinition of type 'System.ComponentModel.Composition.AttributedModel.ConcreteCPD' cannot be used in this context. Only part definitions produced by the ReflectionModelServices.CreatePartDefinition are supported. public void CreatePart_From_InvalidPartDefiniton_ShouldThrowArgumentException() { Assert.Throws <ArgumentException>("partDefinition", () => { try { var partDefinition = new ConcreteCPD(); var instance = new CPDTest(); var part = AttributedModelServices.CreatePart(partDefinition, instance); } catch (Exception e) { Console.WriteLine(e); throw; } }); }
private void RefreshExensions() { catalog.Refresh(); calcExtensionImport = new CalculatorExtensionImport(); calcExtensionImport.ImportsSatisfied += (sender, e) => { this.textStatus.Text += String.Format("{0}\n", e.StatusMessage); }; container.ComposeParts(calcExtensionImport); menuAddins.Items.Clear(); foreach (var extension in calcExtensionImport.CalculatorExtensions) { var menuItemHeader = new StackPanel { Orientation = Orientation.Horizontal }; menuItemHeader.Children.Add(new Label { Content = extension.Title }); var menuCheck = new CheckBox { IsChecked = true }; menuCheck.Unchecked += (sender1, e1) => { MenuItem mi = (sender1 as CheckBox).Tag as MenuItem; ICalculatorExtension ext = mi.Tag as ICalculatorExtension; ComposablePart part = AttributedModelServices.CreatePart(ext); var batch = new CompositionBatch(); batch.RemovePart(part); container.Compose(batch); MenuItem parentMenu = mi.Parent as MenuItem; parentMenu.Items.Remove(mi); }; menuItemHeader.Children.Add(menuCheck); var menuItem = new MenuItem { Header = menuItemHeader, ToolTip = extension.Description, Tag = extension }; menuCheck.Tag = menuItem; menuItem.Click += ShowAddIn; menuAddins.Items.Add(menuItem); } }
/// <summary> /// Add export instances /// </summary> /// <param name="attributedParts">instances that has export attributes</param> #endregion // Documentation public static void ComposeParts(params object[] attributedParts) { #region Validation if (_container == null) { throw new NullReferenceException("container does not initialized"); } if (attributedParts == null || attributedParts.Length == 0) { return; } #endregion // Validation CompositionBatch batch = new CompositionBatch( attributedParts.Select(attributedPart => AttributedModelServices.CreatePart(attributedPart)).ToArray(), Enumerable.Empty <ComposablePart>()); _container.Compose(batch); }
public void PartAddedTwice_AppearsTwice() { // You probably shouldn't be adding a part to the container twice, but it's not something we're going to check for and throw an exception on var container = new CompositionContainer(); var disposable = new AnyPartDisposable(); var part = AttributedModelServices.CreatePart(disposable); var batch = new CompositionBatch(); batch.AddPart(part); container.Compose(batch); batch = new CompositionBatch(); batch.AddPart(part); container.Compose(batch); var exports = container.GetExports <AnyPartDisposable>(); Assert.AreEqual(2, exports.Count()); container.Dispose(); }
public void ImportCompletedUsingSatisfyImportsOnce() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); var entrypoint = new UpperCaseStringComponent(); var entrypointPart = AttributedModelServices.CreatePart(entrypoint); batch.AddParts(new LowerCaseString("abc")); container.Compose(batch); container.SatisfyImportsOnce(entrypointPart); Assert.Equal(1, entrypoint.LowerCaseStrings.Count); Assert.Equal(1, entrypoint.ImportCompletedCallCount); Assert.Equal(1, entrypoint.UpperCaseStrings.Count); Assert.Equal("abc", entrypoint.LowerCaseStrings[0].Value.String); Assert.Equal("ABC", entrypoint.UpperCaseStrings[0]); batch = new CompositionBatch(); batch.AddParts(new object()); container.Compose(batch); container.SatisfyImportsOnce(entrypointPart); Assert.Equal(1, entrypoint.LowerCaseStrings.Count); Assert.Equal(1, entrypoint.ImportCompletedCallCount); Assert.Equal(1, entrypoint.UpperCaseStrings.Count); Assert.Equal("abc", entrypoint.LowerCaseStrings[0].Value.String); Assert.Equal("ABC", entrypoint.UpperCaseStrings[0]); batch.AddParts(new LowerCaseString("def")); container.Compose(batch); container.SatisfyImportsOnce(entrypointPart); Assert.Equal(2, entrypoint.LowerCaseStrings.Count); Assert.Equal(2, entrypoint.ImportCompletedCallCount); Assert.Equal(2, entrypoint.UpperCaseStrings.Count); Assert.Equal("abc", entrypoint.LowerCaseStrings[0].Value.String); Assert.Equal("ABC", entrypoint.UpperCaseStrings[0]); Assert.Equal("def", entrypoint.LowerCaseStrings[1].Value.String); Assert.Equal("DEF", entrypoint.UpperCaseStrings[1]); }
/// <summary> /// 卸载插件 /// </summary> /// <param name="plugin"></param> /// <returns></returns> private bool UnLoadPlugin(IPlugin plugin) { bool isSuccess = false; try { var batch = new CompositionBatch(); var part = AttributedModelServices.CreatePart(plugin); //var part = batch.AddExportedValue<IPlugin>(plugin); //var part2 = _container.GetExportedValues<IPlugin>().First(); //Lazy<IPlugin> part3 = _container.GetExport<IPlugin>(); //IPlugin tmp = part3.Value; batch.RemovePart(part); _container.Compose(batch); //_container.ReleaseExport(part3); isSuccess = true; } catch { isSuccess = false; } return(isSuccess); }
public static ComposablePart CreateAttributed(object instance) { return(AttributedModelServices.CreatePart(instance)); }
public void CreatePart_ObjectInstance_ShouldProduceSharedPart() { var part = AttributedModelServices.CreatePart(typeof(MyExport)); Assert.Equal(CreationPolicy.Shared, part.Metadata.GetValue <CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName)); }