Ejemplo n.º 1
0
        private static IResolver CreateResolver()
        {
            // Step 1 - Create a logger.
            var log = NLogWrapper.GetLog("Default");

            // Step 2 - Create a container.
            var container = new TactContainer(log);

            // Step 3 - Read and aggregate configuration files.
            var config = container.BuildConfiguration(cb =>
                                                      cb.AddJsonFile("AppSettings.json"));

            // Step 4 - Load assemblies from the configuration.
            var assemblies = config.LoadAssembliesFromConfig();

            // Step 5 - Create and validate configuration objects.
            container.ConfigureByAttribute(config, assemblies);

            // Step 6 - Register services by reflection using attributes.
            container.RegisterByAttribute(assemblies);

            // Step 7 - Initialize / start services in the container.
            container.InitializeByAttribute(assemblies);

            return(container);
        }
Ejemplo n.º 2
0
        public void RegisterByAttribute()
        {
            var log = new InMemoryLog();

            using (var container = new TactContainer(log))
            {
                Assert.Throws <InvalidOperationException>(() => container.Resolve <ITester>());

                container.RegisterByAttribute(typeof(Tester));
                var tester = container.Resolve <ITester>();
                Assert.IsType <Tester>(tester);
            }
        }
Ejemplo n.º 3
0
        public void ShouldRegisterFalse()
        {
            var logger = new InMemoryLog();

            using (var container = new TactContainer(logger))
            {
                var map = new Dictionary <string, string>
                {
                    { "TestConfig.ShouldRegister", "false" }
                };
                var configBuilder = new ConfigurationBuilder();
                configBuilder.AddInMemoryCollection(map);
                var config = configBuilder.Build();

                var types = new[] { typeof(TestConfig), typeof(Test) };
                container.ConfigureByAttribute(config, types);
                container.RegisterByAttribute(types);

                Assert.Throws <InvalidOperationException>(() => container.Resolve <ITest>());
            }
        }
Ejemplo n.º 4
0
        public void ShouldRegisterTrue()
        {
            var logger = new InMemoryLog();

            using (var container = new TactContainer(logger))
            {
                var map = new Dictionary <string, string>
                {
                    { "TestConfig:ShouldRegister", "true" }
                };
                var configBuilder = new ConfigurationBuilder();
                configBuilder.AddInMemoryCollection(map);
                var config = configBuilder.Build();

                var types = new[] { typeof(TestConfig), typeof(Test) };
                container.ConfigureByAttribute(config, types);
                container.RegisterByAttribute(types);

                container.Resolve <ITest>();
            }
        }
Ejemplo n.º 5
0
        public static int Main(string[] args)
        {
            // Determine what mode the application is running in.
            if (!Enum.TryParse(args.FirstOrDefault() ?? "Both", out AppMode appMode))
            {
                var modes = string.Join(", ", Enum.GetNames(typeof(AppMode)));
                Console.WriteLine($"First argument must specify a mode: {modes}");
                return(-1);
            }

            // Step 1 - Create a logger.
            var log = NLogWrapper.GetLog("Default");

            // Step 2 - Create a container.
            using (var container = new TactContainer(log))
            {
                // Step 3 - Read and aggregate configuration files.
                var config = container.BuildConfiguration(cb => cb
                                                          .AddJsonFile("AppSettings.json")
                                                          .AddJsonFile($"AppSettings.{appMode}.json"));

                // Step 4 - Load assemblies from the configuration.
                var assemblies = config.LoadAssembliesFromConfig();

                // Step 5 - Create and validate configuration objects.
                container.ConfigureByAttribute(config, assemblies);

                // Step 6 - Register services by reflection using attributes.
                container.RegisterByAttribute(assemblies);

                // Step 7 - Initialize / start services in the container.
                container.InitializeByAttribute(assemblies);

                // Wait for the user to press enter to exit the application.
                TestApplication(container, appMode).Wait();
            }

            return(0);
        }