0

I am implementing AutoMapper and structureMap in my WebAPI solution. I am unable to inject automapper using structuremap .I also want to inject my DAL,BAL and API layers. How do I do that with structure Map. I googled and thought I found a solution but that seems incompatible with the latest version of structure map. DefaultRegistry class doesnt compile. It doesnt find keywords MapperConfiguration, IMapperConfiguration, IMapper Please see my code below

AutoMapper

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

DefaultRegistry class in Webapi project

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>();
    }

DataAccess Layer

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;
    }
}

Business Access Layer

public class MovieBusiness: IMovieBusiness
{
    IMovieService _movieService;

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

WebAPI

public class MoviesController : ApiController
{
    IMovieBusiness _movieBusiness;
    public MoviesController(IMovieBusiness movieBusiness)
    {
        _movieBusiness = movieBusiness;
    }
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
Tom
  • 8,175
  • 41
  • 136
  • 267
  • What do you mean by "I am unable to inject"? What behavior are you seeing? Is there an error? If so, what is it? – NightOwl888 Jun 19 '17 at 23:04
  • As I have mentioned in my post the DefaultRefgistry class isnt compiling. I tried to follow this example https://www.exceptionnotfound.net/using-automapper-with-structuremap-in-asp-net-web-api/ but my current version of structuremap doesnt seem to recognise a lot of keywords.This is mentioned in my post as well – Tom Jun 20 '17 at 05:35

0 Answers0