I have the structure like so:
- MyWork.BLL
- MyWork.DAL
- MyWork.Interfaces
- MyWork.Models
- MyWork.Web
I have unity by convention setup in MyWork.Web project. It works fine when I leave Service.cs and IService.cs under MyWork.BLL. I'm trying to move IService.cs to MyWork.Interfaces for organization and decoupling. This way when working on the web project, all I have to worry about is coding against interface and add the using statment for MyWork.Interfaces. However, if I attempt to move IService.cs to the interface project, Unity by convention no longer works. Anyone know if this is possible? Do I have to always keep the interface and its implementation under the same project in order for Unity by convention to work properly? Here is my code for Unity by convention:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default);
}
Here is the error:
The current type, MyWork.Interfaces.IService, is an interface and cannot be constructed. Are you missing a type mapping?
Update:
UnityConfig.cs under App_Start of MyWork.Web
namespace MyWork.Web.App_Start
{
public class UnityConfig
{
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ICalculatorService, Interfaces.ICalculatorService>(); // added this per suggested answer
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default);
}
}
}