Ejemplo n.º 1
0
        public VendingMachineViewModel Ask()
        {
            using (var dbContext = new VendingMachineDbContext())
            {
                var vMachine       = dbContext.Set <VendingMachineEntity>().Single();
                var buyer          = dbContext.Set <UserEntity>().Single();
                var vMachineWallet = vMachine.Coins
                                     .Select(c => new CoinViewModel
                {
                    ParValue = c.FaceValue,
                    Count    = c.Count
                }).ToList();
                var buyerWallet = buyer.Coins
                                  .Select(c => new CoinViewModel
                {
                    ParValue = c.FaceValue,
                    Count    = c.Count
                }).ToList();
                var availableGoods = vMachine.Goods
                                     .Select(g => new GoodsViewModel
                {
                    Identity = g.Id,
                    Name     = g.Name,
                    Price    = g.Price,
                    Count    = g.Count
                }).ToList();

                return(new VendingMachineViewModel
                {
                    AvailableGoods = availableGoods,
                    BuyerWallet = buyerWallet,
                    MachineWallet = vMachineWallet
                });
            }
        }
        private static IContainer GetContainer()
        {
            var services = new ServiceCollection();

            services.AddSingleton(new LoggerFactory()
                                  .AddConsole(LogLevel.Trace)
                                  .AddDebug());

            services.AddLogging();

            var builder = new ContainerBuilder();

            builder.Register(
                c =>
            {
                var optionsBuilder = new DbContextOptionsBuilder <VendingMachineDbContext>();
                optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Database=VendingMachine;trusted_connection=true;Integrated Security=True");
                var dbContext = new VendingMachineDbContext(optionsBuilder.Options);
                dbContext.Database.EnsureCreated();
                return(dbContext);
            })
            .As <VendingMachineDbContext>()
            .As <DbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterAssemblyTypes(typeof(ClientCardRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(typeof(SaleService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(typeof(RemoteMachineService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(typeof(ActionHandlerProvider).Assembly)
            .Where(t => t.Name.EndsWith("Provider"))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(typeof(ActionHandlerProvider).Assembly)
            .Where(t => t.Name.EndsWith("ActionHandler"))
            .AsSelf();

            builder.RegisterAssemblyTypes(typeof(ActionHandlerProvider).Assembly)
            .Where(t => t.Name.EndsWith("ActionHandler"))
            .AsSelf();

            builder.RegisterType <TcpRequestListener>();
            builder.RegisterType <JobRunner>();

            builder.Populate(services);

            return(builder.Build());
        }
Ejemplo n.º 3
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterModule <CompositionRoot>();
            var container = containerBuilder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            var dbContext = new VendingMachineDbContext();

            dbContext.Database.Initialize(false);
        }
Ejemplo n.º 4
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services
            .AddMvc()
            .AddControllersAsServices();

            services
            .AddSingleton(
                new LoggerFactory()
                .AddConsole(LogLevel.Trace)
                .AddDebug()
                )
            .AddLogging();

            var builder = new ContainerBuilder();

            builder.Register(
                c =>
            {
                var optionsBuilder = new DbContextOptionsBuilder <VendingMachineDbContext>();
                optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Database=VendingMachine;trusted_connection=true;Integrated Security=True");
                var dbContext = new VendingMachineDbContext(optionsBuilder.Options);
                dbContext.Database.EnsureCreated();
                return(dbContext);
            })
            .As <VendingMachineDbContext>()
            .As <DbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterAssemblyTypes(typeof(ClientCardRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(typeof(SaleService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();

            builder.Populate(services);

            return(new AutofacServiceProvider(builder.Build()));
        }