Unity not resolving repository?

First of all, when using dependency injection, it's better to have dependencies on interfaces, rather than having dependencies on concrete classes. I would change your controllers to.

Up vote 1 down vote favorite share g+ share fb share tw.

I have the classes below: SuppliersRepository. Cs (with the interface defining the method): public class SuppliersRepository : BaseRepository,ISuppliersRepository { public IEnumerable GetSuppliersByCoordinates(double latitude, double longitude) { using (IDbConnection connection = OpenConnection()) { const string query = "SELECT ID=SupplierID,Name=Suppliername FROM suppliers WHERE dbo. Distance(@latitude,@longitude,latitude,longitude) (query, new { latitude = latitude,longitude=longitude }); } } } BaseRepository.

Cs (with the interface defining the method) public abstract class BaseRepository: IBaseRepository { public IDbConnection OpenConnection() { IDbConnection connection = new SqlConnection(WebConfigurationManager. ConnectionStrings"myconnection". ConnectionString); connection.Open(); return connection; } } A Bootstraper.

Cs called from global.asax. Cs with: public static class Bootstrapper { public static void Initialise() { var container = BuildUnityContainer(); DependencyResolver. SetResolver(new UnityDependencyResolver(container)); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType(); container.RegisterType(); container.

RegisterInstance(new HttpControllerActivator(container)); container. RegisterControllers(); return container; } } And two controllers, first one resolves the repository fine: public class VenueController : MasterController { Dependency public SuppliersRepository _SuppliersRepository { get; set; } } But this one fails: public class AjaxController : Controller { Dependency public BaseRepository _BaseRepository { get; set; } } It throws a System. InvalidOperationException: The type BaseRepository cannot be constructed.

You must configure the container to supply this value. Any idea what am I doing wrong? C# asp.

Net-mvc mvc dependency-injection unity link|improve this question asked Apr 5 at 12:04David Aleu1,115315 90% accept rate.

It's better to use constructor injection instead of property injection. This way you don't have to decorate your properties with the Dependency attribute, and prevents your application code to take a dependency on Unity. – Steven Apr 5 at 13:48.

First of all, when using dependency injection, it's better to have dependencies on interfaces, rather than having dependencies on concrete classes. I would change your controllers to public class VenueController : MasterController { Dependency public ISuppliersRepository _SuppliersRepository { get; set; } } public class AjaxController : Controller { Dependency public IBaseRepository _BaseRepository { get; set; } } Second, BaseRepository is abstract class. Instances of abstract class can not be constructed, and I think that's the reason why it fails when unity comes to actually creating BaseRepository.

You should inject concrete classes. Container.RegisterType().

1 BaseRepository is indeed abstract. – nonnb Apr 5 at 12:19 Thanks for this, I was being lazy and I added the method in my BaseRepository and if cannot be constructed being abstract I then move the method to another class based on BaseRepository and it works fine now. – David Aleu Apr 5 at 12:39.

You should define your classes to take a property of IRepository, e. G: public class AjaxController : Controller { Dependency public IRepository _BaseRepository { get; set; } } Your properties should always be defined in terms of the interface, not the concrete implementation, otherwise you have tied yourself to the implementation, thus making the use of dependency injection rather pointless!

Fair enough, I originally injected the interface but I changed this because it wasn't working and was running out of ideas! – David Aleu Apr 5 at 12:26 @RB - Although you are right in that DI is usually used with interface segregation, OP could use Unity to inject a derived SuppliersRepository into any BaseRepository property, i.e. Container.RegisterType(); – nonnb Apr 5 at 12:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions