/// <summary>
        ///     Add users to user table.
        /// </summary>
        /// <param name="dbContext"></param>
        private static void AddUsers(CvManagementInMemoryDbContext dbContext)
        {
            dbContext.Users.Add(new User(1, "*****@*****.**", "5773961b8fb0e85fa14aec3681647c7d", "Linh", "Nguyen",
                                         "https://via.placeholder.com/512x512", 0, UserRoles.Admin, UserStatuses.Active));
            dbContext.Users.Add(new User(2, "*****@*****.**", "5773961b8fb0e85fa14aec3681647c7d", "Hang", "Nguyen",
                                         "https://via.placeholder.com/512x512", 0, UserRoles.Ordinary, UserStatuses.Active));
            dbContext.Users.Add(new User(3, "*****@*****.**", "5773961b8fb0e85fa14aec3681647c7d", "Lan",
                                         "Nguyen", "https://via.placeholder.com/512x512", 0, UserRoles.Ordinary, UserStatuses.Active));
            dbContext.Users.Add(new User(4, "*****@*****.**", "5773961b8fb0e85fa14aec3681647c7d", "Ha",
                                         "Nguyen", "https://via.placeholder.com/512x512", 0, UserRoles.Ordinary, UserStatuses.Active));

            dbContext.SaveChanges();
        }
        /// <summary>
        ///     Add user description to user table.
        /// </summary>
        /// <param name="dbContext"></param>
        private static void AddUserDescriptions(CvManagementInMemoryDbContext dbContext)
        {
            dbContext.UserDescriptions.Add(new UserDescription(1, 1, "DESCRIPTION_01"));
            dbContext.UserDescriptions.Add(new UserDescription(2, 1, "DESCRIPTION_02"));
            dbContext.UserDescriptions.Add(new UserDescription(3, 1, "DESCRIPTION_03"));

            dbContext.UserDescriptions.Add(new UserDescription(4, 1, "DESCRIPTION_01"));
            dbContext.UserDescriptions.Add(new UserDescription(5, 1, "DESCRIPTION_02"));
            dbContext.UserDescriptions.Add(new UserDescription(6, 1, "DESCRIPTION_03"));

            dbContext.UserDescriptions.Add(new UserDescription(7, 1, "DESCRIPTION_01"));
            dbContext.UserDescriptions.Add(new UserDescription(8, 1, "DESCRIPTION_02"));
            dbContext.UserDescriptions.Add(new UserDescription(9, 1, "DESCRIPTION_03"));

            dbContext.SaveChanges();
        }
        /// <summary>
        ///     Add project to in-memory database context.
        /// </summary>
        /// <param name="dbContext"></param>
        private static void AddProjects(CvManagementInMemoryDbContext dbContext)
        {
            dbContext.Projects.Add(new Project(1, 1, "PROJECT_01", "PROJECT_DESCRIPTION_01", 0, 0));
            dbContext.Projects.Add(new Project(2, 1, "PROJECT_02", "PROJECT_DESCRIPTION_02", 0, 0));
            dbContext.Projects.Add(new Project(3, 1, "PROJECT_03", "PROJECT_DESCRIPTION_03", 0, 0));

            dbContext.Projects.Add(new Project(4, 2, "PROJECT_01", "PROJECT_DESCRIPTION_01", 0, 0));
            dbContext.Projects.Add(new Project(5, 2, "PROJECT_02", "PROJECT_DESCRIPTION_02", 0, 0));
            dbContext.Projects.Add(new Project(6, 2, "PROJECT_03", "PROJECT_DESCRIPTION_03", 0, 0));

            dbContext.Projects.Add(new Project(7, 3, "PROJECT_01", "PROJECT_DESCRIPTION_01", 0, 0));
            dbContext.Projects.Add(new Project(8, 3, "PROJECT_02", "PROJECT_DESCRIPTION_02", 0, 0));
            dbContext.Projects.Add(new Project(9, 3, "PROJECT_03", "PROJECT_DESCRIPTION_03", 0, 0));

            dbContext.Projects.Add(new Project(10, 4, "PROJECT_01", "PROJECT_DESCRIPTION_01", 0, 0));
            dbContext.Projects.Add(new Project(11, 4, "PROJECT_02", "PROJECT_DESCRIPTION_02", 0, 0));
            dbContext.Projects.Add(new Project(12, 4, "PROJECT_03", "PROJECT_DESCRIPTION_03", 0, 0));

            dbContext.SaveChanges();
        }
 /// <summary>
 ///     Seed data to in-memory database.
 /// </summary>
 /// <param name="dbContext"></param>
 public static void Seed(this CvManagementInMemoryDbContext dbContext)
 {
     AddUsers(dbContext);
     AddUserDescriptions(dbContext);
     AddProjects(dbContext);
 }
Esempio n. 5
0
        public static void Register(HttpConfiguration httpConfiguration)
        {
            var builder = new ContainerBuilder();

            builder.RegisterApiControllers()
            .WithAttributeFiltering();

            #region Automapper

            var oMapperConfigurationExpression = new MapperConfigurationExpression();
            oMapperConfigurationExpression.CreateMap <User, ProfileModel>();
            oMapperConfigurationExpression.CreateMap <ProfileModel, User>();
            oMapperConfigurationExpression.CreateMap <SkillCategory, SkillCategoryViewModel>();
            oMapperConfigurationExpression.CreateMap <IQueryable <SkillCategory>, IQueryable <SkillCategoryViewModel> >();
            //var autoMapperOptions = new MapperConfiguration(options =>
            //{
            //    options.CreateMap<User, ProfileModel>();
            //    options.CreateMap<ProfileModel, User>();
            //    options.CreateMap<SkillCategory, SkillCategoryViewModel>();
            //    options.CreateMap<IQueryable<SkillCategory>, IQueryable<SkillCategoryViewModel>>();
            //});

            var autoMapperOptions = new MapperConfiguration(oMapperConfigurationExpression);
            var mapper            = new Mapper(autoMapperOptions);
            Mapper.Initialize(oMapperConfigurationExpression);
            builder.RegisterInstance(mapper)
            .As <IMapper>()
            .SingleInstance();

            #endregion

            #region Controllers & hubs

            // Controllers & hubs
            builder.RegisterApiControllers(typeof(Startup).Assembly);
            builder.RegisterWebApiFilterProvider(httpConfiguration);

            #endregion

            #region Database context

#if !USE_IN_MEMORY
            builder.Register(c =>
            {
                var dbContextOptionsBuilder = new DbContextOptionsBuilder <CvManagementDbContext>();
                var connectionString        = ConfigurationManager.ConnectionStrings["CvManagement"].ConnectionString;
                dbContextOptionsBuilder.UseSqlServer(connectionString)
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(new LoggerFactory().AddConsole(
                                      (category, level) => level == LogLevel.Information &&
                                      category == DbLoggerCategory.Database.Command.Name, true));

                var dbContext = new CvManagementDbContext(dbContextOptionsBuilder.Options);
                return(dbContext);
            })
            .As <DbContext>()
            .InstancePerLifetimeScope();
#else
            builder.Register(c =>
            {
                var dbContextOptionsBuilder = new DbContextOptionsBuilder <CvManagementInMemoryDbContext>();
                dbContextOptionsBuilder.UseInMemoryDatabase(nameof(CvManagementInMemoryDbContext))
                .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));

                var dbContext = new CvManagementInMemoryDbContext(dbContextOptionsBuilder.Options);
                dbContext.Seed();
                return(dbContext);
            })
            .As <DbContext>()
            .SingleInstance();
#endif
            #endregion

            #region Model

            var appSettings = FindAppSettings();
            builder.RegisterInstance(appSettings);

            var appPaths = FindAppPathSettings();
            builder.RegisterInstance(appPaths);

            #endregion

            #region Services

            builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope();
            builder.RegisterType <UnitOfWork>().As <IUnitOfWork>().InstancePerLifetimeScope();
            builder.RegisterType <DbService>().As <IDbService>().InstancePerLifetimeScope();
            builder.RegisterType <ProfileService>().As <IProfileService>().InstancePerLifetimeScope();
            builder.RegisterType <GoogleCaptchaService>().As <ICaptchaService>().InstancePerLifetimeScope();
            builder.RegisterType <TokenService>().As <ITokenService>().InstancePerLifetimeScope();
            builder.RegisterType <FileService>().As <IFileService>().InstancePerLifetimeScope();
            builder.Register(c => new HttpClient()).As <HttpClient>().SingleInstance();
            builder.RegisterHttpRequestMessage(httpConfiguration);
            builder.Register(x => new UrlHelper(x.Resolve <HttpRequestMessage>()));
            builder.RegisterType <ProfileCacheService>().As <IValueCacheService <string, ProfileModel> >().SingleInstance()
            .WithAttributeFiltering();

            RegisterRedisCachingServices(ref builder);

            // Api services.
            builder.RegisterType <UserService>().As <IUserService>().InstancePerLifetimeScope();
            builder.RegisterType <UserDescriptionService>().As <IUserDescriptionService>().InstancePerLifetimeScope();
            builder.RegisterType <ProjectService>().As <IProjectService>().InstancePerLifetimeScope();
            builder.RegisterType <HobbyService>().As <IHobbyService>().InstancePerLifetimeScope();
            builder.RegisterType <SkillService>().As <ISkillService>().InstancePerLifetimeScope();
            builder.RegisterType <ProjectSkill>().As <IProjectSkillService>().InstancePerLifetimeScope();
            builder.RegisterType <ProjectResponsibilityService>().As <IProjectResponsibilityService>().InstancePerLifetimeScope();
            builder.RegisterType <ResponsibilityService>().As <IResponsibilityService>().InstancePerLifetimeScope();
            builder.RegisterType <SkillCategoryService>().As <ISkillCategoryService>().InstancePerLifetimeScope();

            #endregion

            var containerBuilder = builder.Build();
            httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(containerBuilder);
        }