I have a three-layer Web Api architecture with three projects inside: data, business and presentation layers. I need to initialize two different cartographers in two business and presentation layers.
I created a static class and method to initialize a single handler in business logic:
using AutoMapper;
using Shop.BLL.DTOModels;
using Shop.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shop.BLL.InitMapper
{
public static class InitializeMapperBLL
{
public static void RegisterMappings()
{
Mapper.Initialize(cfg => cfg.CreateMap<Category, DTOCategoryModel>());
}
}
}
And call it like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shop.DAL.Repositories;
using AutoMapper;
using Shop.BLL.DTOModels;
using Shop.DAL.Models;
using Shop.BLL.Interfaces;
using Shop.DAL.Interfaces;
using Shop.BLL.InitMapper;
namespace Shop.BLL.Services
{
public class CategoryService : ICategoryService
{
IUnitOfWork Database { get; set; }
public CategoryService(IUnitOfWork uow)
{
Database = uow;
}
public IEnumerable<DTOCategoryModel> GetCategories()
{
InitializeMapperBLL.RegisterMappings();
return Mapper.Map<IEnumerable<Category>, List<DTOCategoryModel>>(Database.Categories.GetAll());
}
public void Dispose()
{
Database.Dispose();
}
}
}
And in the presentation layer, I do the same:
using AutoMapper;
using Shop.API.ViewModels;
using Shop.BLL.DTOModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shop.API.MapperInit
{
public static class InitializeMapperAPI
{
public static void RegisterMappings()
{
Mapper.Initialize(cfg => cfg.CreateMap<DTOCategoryModel, CategoryViewModel>());
}
}
}
And call Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
InitializeMapperAPI.RegisterMappings();
CreateKernel();
}
And I already have a Mapper error. You must call Initialize once per application domain / process.
How to solve this problem?
source
share