Skip to content

How to Create a Setup Component

Iuga Alexandru edited this page May 19, 2023 · 2 revisions

As an example you may look into the existing startup components:

  • ConsoleTools.Commando.Setup.Autofac
  • ConsoleTools.Commando.Setup.Ninject
  • ConsoleTools.Commando.Setup.Microsoft

Builder Approach

This is the suggested way of setup starting with v2.0.0. TBD

Dependency Injection approach

Create an extension method

The setup process is composed of a number of dependencies that need to be registered into the dependency container of choice:

  • Command Parser
  • Command Router
  • Command Factory
  • Command Metadata Collection
  • Each Command type
  • Each View type
  • Application

If we use Autofac as dependency container, the code looks like this:

public static void RegisterCommando(this ContainerBuilder containerBuilder, Type commandParserType, params Assembly[] assemblies)
{
    containerBuilder.RegisterType<CommandRouter>().AsSelf();
    containerBuilder.RegisterType<CommandFactory>().As<ICommandFactory>();
    containerBuilder.RegisterType(commandParserType).As<ICommandParser>();

    CommandMetadataCollection commandMetadataCollection = new();
    Assembly commandoAssembly = typeof(CommandMetadataCollection).Assembly;
    commandMetadataCollection.LoadFrom(commandoAssembly);
    commandMetadataCollection.LoadFrom(assemblies);

    containerBuilder.RegisterInstance(commandMetadataCollection).AsSelf().SingleInstance();

    foreach (Type type in commandMetadataCollection.GetCommandTypes())
        containerBuilder.RegisterType(type).AsSelf();

    foreach (Type type in commandMetadataCollection.GetViewTypes())
        containerBuilder.RegisterType(type).AsSelf();

    containerBuilder.RegisterType<Application>().AsSelf().SingleInstance();
}
Clone this wiki locally