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