Question

When I install StructureMap for my project and use:

public class IndexController : Controller
    {
        private readonly IMapper<UserModel, UserDto> _mapper;

        public IndexController(IMapper<UserModel, UserDto> mapper)
        {
            _mapper = mapper;
        }

        public ActionResult Index()
        {
            List<UserDto> userDb = UserDb.GetAll();
            UserModel userModel = _mapper.Map(userDb[0]);
            return View();
        }
    }

After run with an server error message:

No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. ...

But I don't know much about the principle of dependency inversion. Please help me to resolve this.

This is ObjectFactory :

public static class IoC {
        public static IContainer Initialize() {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                    });
            //                x.For<IExample>().Use<Example>();
                        });
            return ObjectFactory.Container;
        }
    }

What do I need to do next?

Was it helpful?

Solution

You need to tell StructureMap how to wire up all types that don't follow the default convention (which you use in your scan). The default convention is to wire up all interfaces to concrete implementations with the same name as the interface (minus the "I" prefix).

In your case you need to tell StructureMap to wire up all needed closed generic types of IMapper using:

x.For(typeof(IMapper<,>)).Use(typeof(MyConcreteMapper<,>);

Put the line above where you currently have the x.For<IExample>().Use<Example().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top