I'm trying to use the Unity Registration by Convention feature. I can't figure out how to register from specific assembly/project files with the file ending "Repository".
container.RegisterTypes(
AllClasses.FromAssembliesInBasePath(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.ContainerControlled);
I've found only this example from a MSDN blog ( https://blogs.msdn.microsoft.com/agile/2013/03/12/unity-configuration-registration-by-convention/ ) post and as I understand it, this will search through all projects/assemblies and will look for default naming convention files Class and IClass.
I have a project in my solution named CManager.Repository with repository files ending with *Repository. They are auto-registered.
Any hint or help?
This works for me on a enterprise web-api that I have been working on for months now. It has an n-tier architecture with a data layer that has .cs files that end with 'Repository'. While I agree with the other post the Interfaces should be seperated, in my web-api they are not implemented in this way.
This setup has usually works for my team. Sometimes we still get classes that throw unity errors for no apparent reason. For those we do something like this;
I hope this helps
Regards.
For auto-registration of unity it's better to separate the
Interfaces
andRepositories
as folder in the repository project with proper naming convention.If you have all repositories under single project then it's not needed to scan for all assemblies. This should register all repositories under project
CManager.Repository
we have to register unity components on application startup, traditionally inside
Global.asax.cs
file. So putUnityConfig.cs
file on startup project underApp_Start
folder.and make sure other projects are linked up with startup project.