Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Sep 19, 2024
1 parent 143d68c commit 2857014
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/client/LibplanetConsole.Clients/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected ApplicationBase(ApplicationOptions options)
_container.ComposeExportedValue(_client);
_container.ComposeExportedValue<IClient>(_client);
_container.ComposeExportedValue<IBlockChain>(_client);
_container.ComposeExportedValues(options.Components);
_clientServiceContext = _container.GetValue<ClientServiceContext>();
_clientServiceContext.EndPoint = options.EndPoint;
_container.GetValue<IApplicationConfigurations>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;

namespace LibplanetConsole.Frameworks.Extensions;

Expand All @@ -7,4 +9,22 @@ public static class CompositionContainerExtensions
public static T GetValue<T>(this CompositionContainer @this)
=> @this.GetExportedValue<T>() ??
throw new InvalidOperationException($"'{typeof(T)}' is not found.");

public static void ComposeExportedValues(this CompositionContainer @this, object[] components)
{
var batch = new CompositionBatch();
foreach (var item in components)
{
var contractName = AttributedModelServices.GetContractName(item.GetType());
var typeIdentity = AttributedModelServices.GetTypeIdentity(item.GetType());
var metadata = new Dictionary<string, object?>
{
{ "ExportTypeIdentity", typeIdentity },
};
var export = new Export(contractName, metadata, () => item);
batch.AddExport(export);
}

@this.Compose(batch);
}
}
1 change: 1 addition & 0 deletions src/console/LibplanetConsole.Consoles/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected ApplicationBase(ApplicationOptions options)
_container.ComposeExportedValue(_clients);
_container.ComposeExportedValue<IClientCollection>(_clients);
_container.ComposeExportedValue<IApplicationService>(_clients);
_container.ComposeExportedValues(options.Components);
_consoleContext = _container.GetValue<ConsoleServiceContext>();
_consoleContext.EndPoint = options.EndPoint;
_container.GetValue<IApplicationConfigurations>();
Expand Down
6 changes: 5 additions & 1 deletion src/node/LibplanetConsole.Nodes/ApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Diagnostics;
using LibplanetConsole.Common;
using LibplanetConsole.Frameworks;
Expand Down Expand Up @@ -34,7 +37,8 @@ protected ApplicationBase(ApplicationOptions options)
_container.ComposeExportedValue(_node);
_container.ComposeExportedValue<INode>(_node);
_container.ComposeExportedValue<IBlockChain>(_node);
Array.ForEach(options.Components, _container.ComposeExportedValue);
_container.ComposeExportedValues(options.Components);
_container.ComposeParts(options.Components);
_nodeContext = _container.GetValue<NodeContext>();
_nodeContext.EndPoint = options.EndPoint;
_logger.Debug(options.EndPoint.ToString());
Expand Down

0 comments on commit 2857014

Please sign in to comment.