private Func <IQueryable <TEntity>, IEnumerable <TViewModel> > GetAutoMapperConversion <TEntity, TViewModel>(bool canUseAutoMapperProjection = false) { Func <IQueryable <TEntity>, IEnumerable <TViewModel> > conversion; if (AutoMapperUtils.SameTypes <TEntity, TViewModel>()) { conversion = q => q.Cast <TViewModel>().ToList(); } else { // https://github.com/AutoMapper/AutoMapper/issues/362 // The idea behind Project().To is to be passed to a query provider like EF or NHibernate that will then do the appropriate SQL creation, // not necessarily that the in-memory-execution will work. // Project.To has a TON of limitations as it's built explicitly for real query providers, and only does things like MapFrom etc. // To put it another way - don't use Project.To unless you're passing that to EF or NH or another DB query provider that knows what to do with the expression tree. if (canUseAutoMapperProjection) { conversion = q => q.ProjectTo <TViewModel>(_mapperConfiguration).AsEnumerable(); } else { conversion = _mapper.Map <IEnumerable <TViewModel> >; } } return(conversion); }
public static void Initialize(IServiceProvider serviceProvider) { log4net.Config.XmlConfigurator.Configure(); var context = new MvcMovieContext( serviceProvider.GetRequiredService < DbContextOptions <MvcMovieContext> >()); { // don't seed the original movie models if exist if (!context.Movie.Any()) { context.Movie.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-2-12"), Genre = "Romantic Comedy", Price = 7.99M }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M }); } // don't seed the Json movie models if exist if (!context.Movies.Any()) { var controller = new MvcMoviesController(context); var JsonMovieList = controller.GetMvcMoviesFromJson(); var seededMoviesList = new List <Movies>(); // use automapper to map Json response to Movie objects foreach (dynamic m in JsonMovieList) { var mapper = new AutoMapperUtils(); var _movie = mapper.MapJsonMovieToEntityMoviesModel(JObject.FromObject(m)); seededMoviesList.Add(_movie); } context.Movies.AddRange(seededMoviesList); context.SaveChanges(); // create a new async thread to fill the Foreign Key Ids to run in the background // this will take some time to complete and not needed for the movie search Task.Factory.StartNew(() => SetContextIdsAsync(context)); } } }
public async Task<TrainingFormViewModel> GetById(Guid id) { var model = await _dataContext.TrainingForms.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id); var result = AutoMapperUtils.AutoMap<TrainingForm, TrainingFormViewModel>(model); result.TrainingSubjects = await _dataContext.TrainingSubjects.AsNoTracking().Where(x => x.TrainingFormId == id).OrderBy(x => x.Order).ToListAsync(); return result; }
public async Task <Pagination <DepartmentViewModel> > GetAllAsync(DepartmentQueryModel queryModel) { var query = from dept in _dataContext.Departments.Include(x => x.Organization).AsNoTracking() select new DepartmentViewModel { Id = dept.Id, Name = dept.Name, Code = dept.Code, OrganizationId = dept.OrganizationId, Organization = AutoMapperUtils.AutoMap <Organization, OrganizationViewModel>(dept.Organization), LastModifiedOnDate = dept.LastModifiedOnDate }; if (queryModel.ListTextSearch != null && queryModel.ListTextSearch.Count > 0) { foreach (var ts in queryModel.ListTextSearch) { query = query.Where(q => q.Name.Contains(ts) || q.Code.Contains(ts) ); } } if (queryModel.OrganizationId != null && queryModel.OrganizationId != Guid.Empty) { query = query.Where(x => x.OrganizationId == queryModel.OrganizationId); } return(await query.GetPagedAsync(queryModel.CurrentPage.Value, queryModel.PageSize.Value, queryModel.Sort)); }
public async Task <IActionResult> Get(Guid id) { return(await ExecuteFunction(async (user) => { var result = await _organizationService.GetById(id); return AutoMapperUtils.AutoMap <Organization, OrganizationViewModel>(result);; })); }
public async Task <IActionResult> Get(Guid id) { return(await ExecuteFunction(async (user) => { var result = await _departmentService.GetById(id); return AutoMapperUtils.AutoMap <Department, DepartmentViewModel>(result); })); }
public async Task <IActionResult> Create(OrganizationRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = AutoMapperUtils.AutoMap <OrganizationRequestModel, Organization>(requestModel); return await _organizationService.SaveAsync(model); })); }
public async Task <IActionResult> Create(TitleRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = AutoMapperUtils.AutoMap <TitleRequestModel, Title>(requestModel); return await _titleService.SaveAsync(model); })); }
public async Task<IActionResult> Get(Guid id) { return await ExecuteFunction(async (user) => { var result = await _trainingProgramService.GetById(id); return AutoMapperUtils.AutoMap<TrainingProgram, TrainingProgramViewModel>(result); ; }); }
public CatapultEngineControllerTests() { _catapultEngineService = new Mock <ICatapultEngineService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <CatapultEngineController>(); }
public async Task<IActionResult> Create(TrainingProgramRequestModel requestModel) { return await ExecuteFunction(async (user) => { var model = AutoMapperUtils.AutoMap<TrainingProgramRequestModel, TrainingProgram, TrainingProgram_UserRequestModel, TrainingProgram_User>(requestModel); return await _trainingProgramService.SaveAsync(model, requestModel.TrainingProgram_Users); }); }
public ProjectMemberControllerTests() { _projectMemberService = new Mock <IProjectMemberService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <ProjectMemberController>(); }
public ExternalServiceTypeControllerTests() { _externalServiceTypeService = new Mock <IExternalServiceTypeService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <ExternalServiceTypeController>(); }
public async Task <IActionResult> Create(DepartmentRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = AutoMapperUtils.AutoMap <DepartmentRequestModel, Department>(requestModel); return await _departmentService.SaveAsync(model); })); }
public JobDefinitionControllerTests() { _jobDefinitionService = new Mock <IJobDefinitionService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <JobDefinitionController>(); }
public VersionControllerTests() { _catapultEngineService = new Mock <ICatapultEngineService>(); _providerService = new Mock <ITaskProviderService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <VersionController>(); }
public async Task<IActionResult> Update(Guid id, [FromBody] TrainingProgramRequestModel requestModel) { return await ExecuteFunction(async (user) => { var model = await _trainingProgramService.GetById(id); model = AutoMapperUtils.AutoMap<TrainingProgramRequestModel, TrainingProgram, TrainingProgram_UserRequestModel, TrainingProgram_User>(requestModel, model); return await _trainingProgramService.SaveAsync(model, requestModel.TrainingProgram_Users); }); }
public ProviderControllerTests() { _pluginService = new Mock <IPluginService>(); _pluginAdditionalConfigService = new Mock <IPluginAdditionalConfigService>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger <ProviderController>(); }
public AccountControllerTests() { _userService = new Mock <IUserService>(); _mapper = AutoMapperUtils.GetMapper(); _notificationProvider = new Mock <INotificationProvider>(); _logger = LoggerMock.GetLogger <AccountController>(); }
public ProjectMemberControllerTests() { _projectMemberService = new Mock <IProjectMemberService>(); _userService = new Mock <IUserService>(); _notificationProvider = new Mock <INotificationProvider>(); _mapper = AutoMapperUtils.GetMapper(); _configuration = new Mock <IConfiguration>(); _logger = LoggerMock.GetLogger <ProjectMemberController>(); }
public JobQueueControllerTests() { _jobQueueService = new Mock<IJobQueueService>(); _catapultEngineService = new Mock<ICatapultEngineService>(); _configuration = new Mock<IConfiguration>(); _mapper = AutoMapperUtils.GetMapper(); _logger = LoggerMock.GetLogger<JobQueueController>(); }
public async Task <AppRoleDetailModel> GetById(Guid Id) { var entity = _dataContext.AppRoles.AsNoTracking().FirstOrDefault(x => x.Id == Id); if (entity == null) { throw new Exception(IRoleService.Message_RoleNotFound); } var result = AutoMapperUtils.AutoMap <Role, AppRoleDetailModel>(entity); result.Permissons = await GetPermisson(Id); return(result); }
public AccountControllerTests() { _userService = new Mock <IUserService>(); _externalAccountTypeService = new Mock <IExternalAccountTypeService>(); _mapper = AutoMapperUtils.GetMapper(); _notificationProvider = new Mock <INotificationProvider>(); _configuration = new Mock <IConfiguration>(); _logger = LoggerMock.GetLogger <AccountController>(); }
public async Task <IActionResult> Create([FromForm] UserRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = AutoMapperUtils.AutoMap <UserRequestModel, User>(requestModel); //TODO: FAKE PASSWORD var passwordHasher = new PasswordHasher <User>(); model.Password = passwordHasher.HashPassword(model, Default.Password); var result = await _userService.SaveAsync(model, requestModel.AvatarFile); return AutoMapperUtils.AutoMap <User, UserViewModel>(result); })); }
public async Task <IActionResult> Update(Guid id, [FromBody] OrganizationRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = await _organizationService.GetById(id); if (model == null) { throw new ArgumentException($"Id {id} không tồn tại"); } model = AutoMapperUtils.AutoMap <OrganizationRequestModel, Organization>(requestModel, model); return await _organizationService.SaveAsync(model); })); }
public async Task <bool> ChangeStatus(Guid id, string status) { var model = await GetById(id); model.Status = status; var trainingProgram_UserRequestModels = new List <TrainingProgram_UserRequestModel>(); foreach (var trp_u in model.TrainingProgram_Users) { var trp_ur = AutoMapperUtils.AutoMap <TrainingProgram_User, TrainingProgram_UserRequestModel>(trp_u); trainingProgram_UserRequestModels.Add(trp_ur); } await SaveAsync(model, trainingProgram_UserRequestModels); return(true); }
public async Task <IActionResult> Update(Guid id, [FromBody] TrainingFormRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var viewModel = await _trainingFormService.GetById(id); var model = AutoMapperUtils.AutoMap <TrainingFormViewModel, TrainingForm>(viewModel); if (model == null) { throw new ArgumentException($"Id {id} không tồn tại"); } model = AutoMapperUtils.AutoMap <TrainingFormRequestModel, TrainingForm>(requestModel, model); return await _trainingFormService.SaveAsync(model, requestModel.TrainingSubjects); })); }
public async Task <IActionResult> Update(Guid id, [FromForm] UserRequestModel requestModel) { return(await ExecuteFunction(async (user) => { var model = await _userService.GetById(id); if (model == null) { throw new ArgumentException($"Id {id} không tồn tại"); } var newModel = AutoMapperUtils.AutoMap <UserRequestModel, User>(requestModel, model); var result = await _userService.SaveAsync(newModel, requestModel.AvatarFile); return AutoMapperUtils.AutoMap <User, UserViewModel>(result); })); }
public void GetTestMovies(List <dynamic> JsonMovieList) { if (TestMovies == null) { TestMovies = new List <Movies>(); } List <Movies> seededMovielist = new List <Movies>(); foreach (dynamic m in JsonMovieList) { var mapper = new AutoMapperUtils(); var _movie = mapper.MapJsonMovieToEntityMoviesModel(JObject.FromObject(m)); seededMovielist.Add(_movie); TestMovies.Add(_movie); } }
public static void InitAutoMapper(Func <Type, object> resolver) { _mapperConfiguration = new MapperConfiguration(cfg => { cfg.ConstructServicesUsing(resolver); cfg.AddProfile <EmployeeProfile>(); cfg.AddProfile <FunctionProfile>(); cfg.AddProfile <OUProfile>(); cfg.AddProfile <UserProfile>(); cfg.AddProfile <SubFunctionProfile>(); cfg.AddProfile <ProductProfile>(); }); _mapperConfiguration.AssertConfigurationIsValid(); _mapper = _mapperConfiguration.CreateMapper(); _autoMapperUtils = new AutoMapperUtils(_mapperConfiguration); }