Exemplo n.º 1
0
        protected BaseTest()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .ConfigureWarnings(opt => opt.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                          .Options;


            DbContext = new ApplicationDbContext(options);

            IServiceCollection services = new ServiceCollection();

            services.AddScoped <ApplicationDbContext>(provider => DbContext);

            // Add automapper.
            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfiles(MappingConfiguraition.GetMappingTypes());
            });

            services.AddSingleton <IMapper>(mapperConfig.CreateMapper());

            Provider = services.BuildServiceProvider();

            DefaultAccount = new Account {
                UserName = "******", Email = "[email protected]"
            };
            DbContext.Users.Add(DefaultAccount);
            DbContext.SaveChanges();
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            // Add framework services.
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                                                                              b => b.MigrationsAssembly("dtu.blognet.Infrastructure.DataAccess")));

            services.AddIdentity <Account, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            // Add JWT configuration object.
            services.Configure <JwtConfiguration>(Configuration.GetSection("JWTSettings"));

            // Add automapper.
            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfiles(MappingConfiguraition.GetMappingTypes());
                cfg.AddProfile(new ViewModelProfile());
            });

            services.AddSingleton <IMapper>(mapperConfig.CreateMapper());

            // Queries.
            services.AddTransient <QueryDb, QueryDb>();
            services.AddTransient <BlogQueryFactory, BlogQueryFactory>();
            services.AddTransient <AccountQueryFactory, AccountQueryFactory>();
            services.AddTransient <PostQueryFactory, PostQueryFactory>();
            services.AddTransient <CommentQueryFactory, CommentQueryFactory>();
            services.AddTransient <SubscriptionQueryFactory, SubscriptionQueryFactory>();

            // Commands.
            services.AddTransient <BlogCommandHandlerFactory, BlogCommandHandlerFactory>();
            services.AddTransient <PostCommandHandlerFactory, PostCommandHandlerFactory>();
            services.AddTransient <CommentCommandHandlerFactory, CommentCommandHandlerFactory>();
            services.AddTransient <SubscriptionCommandHandlerFactory, SubscriptionCommandHandlerFactory>();
        }