0

我正在实施AutoMapperstructureMap在我的WebAPI解决方案中。我无法使用结构映射注入自动映射器。我还想注入我的 DAL、BAL 和 API 层。我如何使用结构映射来做到这一点。我用谷歌搜索并认为我找到了解决方案,但这似乎与最新版本的结构图不兼容。DefaultRegistry 类无法编译。它没有找到关键字 MapperConfiguration、IMapperConfiguration、IMapper 请在下面查看我的代码

自动映射器

public class DomainToDtoMapping : Profile
{
    public DomainToDtoMapping()
    {
        CreateMap<BaseEntity, BaseDto>().ReverseMap();
        CreateMap<Movie, MoviesDto>().ReverseMap();
    }
}

Webapi 项目中的 DefaultRegistry 类

public class DefaultRegistry : Registry {
    #region Constructors and Destructors
    public DefaultRegistry() {
        var profiles = from t in typeof(DefaultRegistry).Assembly.GetTypes()
                       where typeof(Profiles).IsAssignableFrom(t)
                       select (Profiles) Activator.CreateInstance(t);

        //For each Profile, include that profile in the MapperConfiguration
        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });

        //Create a mapper that will be used by the DI container
        var mapper = config.CreateMapper();

        //Register the DI interfaces with their implementation
        For<IMapperConfiguration>().Use(config);
        For<IMapper>().Use(mapper);

        //Register the UserRepository and pass instance of Mapper to its constructor
        For<IMovieBusiness>().Use<MovieBusiness>()
            .Ctor<IMapper>().Is(mapper);

        For<IConnectionFactory>().Use<ConnectionFactory>();
        For<IMovieRepository>().Use<MovieRepository>();
        For<IUnitOfWork>().Use<UnitOfWork>();
        For<IMovieService>().Use<MovieService>();
        For<IMovieBusiness>().Use<MovieBusiness>();
    }

数据访问层

public class MovieRepository : GenericRepository<Movie>, IMovieRepository
{
    IConnectionFactory _connectionFactory;

    public MovieRepository(IConnectionFactory connectionFactory)
    {
        _connectionFactory = connectionFactory;
    }
}

public class MovieService : IMovieService
{
    IUnitOfWork _unitOfWork;
    public MovieService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
}

业务访问层

public class MovieBusiness: IMovieBusiness
{
    IMovieService _movieService;

    public MovieBusiness(IMovieService movieService)
    {
        _movieService = movieService;
    }
}

网络API

public class MoviesController : ApiController
{
    IMovieBusiness _movieBusiness;
    public MoviesController(IMovieBusiness movieBusiness)
    {
        _movieBusiness = movieBusiness;
    }
}
4

0 回答 0