Example #1
0
        private static BasicInjectionContainer ConfigureInjectionContainer(AppSettings appsettings)
        {
            var container = new BasicInjectionContainer();

            container.RegisterInstance(appsettings);
            container.RegisterType <IOutputWriter, ConsoleOutputWriter>();
            container.RegisterType <IBaseDirectoryProvider, BaseDirectoryProvider>();
            container.RegisterType <RepositoryFactory>();
            container.RegisterType <AccountService>();
            container.RegisterType <CsvService>();
            container.RegisterType <MapService>();
            container.RegisterType <SummaryService>();
            container.RegisterType <TransactionService>();

            var commands     = new List <ICommand>();
            var commandTypes = typeof(Program).GetTypeInfo().Assembly.DefinedTypes
                               .Where(t => (typeof(ICommand)).GetTypeInfo().IsAssignableFrom(t))
                               .Where(t => t.IsClass && !t.IsAbstract)
                               .Select(t => t.AsType())
                               .ToList();

            foreach (var commandType in commandTypes)
            {
                container.RegisterType(commandType, commandType);
                var command = container.Resolve(commandType) as ICommand;
                commands.Add(command);
            }

            container.RegisterInstance <IReadOnlyCollection <ICommand> >(commands.AsReadOnly());

            return(container);
        }
        /// <summary>
        /// Simple Demonstration of how to use the Basic Injection Container
        /// </summary>
        /// <param name="args">The command line arguments</param>
        public static void Main(string[] args)
        {
            var logger    = ConfigureLogger();
            var container = new BasicInjectionContainer();

            try
            {
                // Add an instance to the container that has already been created
                container.RegisterInstance(logger);

                // Add a type to the container
                container.RegisterType <MathsService>();

                // Add a type to the container that should be resolved to an implementing
                // or inherited type. Useful if the consumers require interfaces in their
                // constructors
                container.RegisterType <IAddService, AddService>();

                // Get an instance from the container
                var mathsService = container.Resolve <MathsService>();

                var sum = mathsService.Add(1, 2);
                logger.Information("The sum of 1 and 2 is {sum}", sum);

                // Add an instance to the container that implements an interface, or is
                // the sub type of some super type
                container.RegisterInstance <IMessageService>(new MessageService(logger));

                var messageService = container.Resolve <IMessageService>();
                messageService.SayHello("David");
            }
            catch (Exception exception)
            {
                logger.Error(exception, "An error occured in the usage demonstration");
            }

            logger.Information("Done");
            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }
        }
Example #3
0
        private static BasicInjectionContainer ConfigureContainer()
        {
            var container = new BasicInjectionContainer();

            // Infrastructure Configuration
            container.RegisterType <IOutputWriter, ConsoleOutputWriter>();

            // WS.Games Configuration
            var baseDirectory = "/Users/davidbrunger/Documents/Visual Studio Code Projects/WS.Games.Elo/Data";

            container.RegisterType <PlayerService>();
            container.RegisterInstance <IRepositoryFactory>(new JsonRepositoryFactory(baseDirectory));
            container.RegisterType <IPlayerServiceConfiguration, Configuration>();
            container.RegisterType <GameService>();
            container.RegisterInstance(new EloCalculator(32));
            container.RegisterType <IGameServiceConfiguration, Configuration>();
            container.RegisterType <ImageService>();

            // Command Line Commands
            var commands     = new List <ICommand>();
            var commandTypes = typeof(Program).GetTypeInfo().Assembly.DefinedTypes
                               .Where(t => (typeof(ICommand)).GetTypeInfo().IsAssignableFrom(t))
                               .Where(t => t.IsClass && !t.IsAbstract)
                               .Select(t => t.AsType())
                               .ToList();

            foreach (var commandType in commandTypes)
            {
                container.RegisterType(commandType, commandType);
                var command = container.Resolve(commandType) as ICommand;
                commands.Add(command);
            }

            container.RegisterInstance <IReadOnlyCollection <ICommand> >(commands.AsReadOnly());

            return(container);
        }