Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dependency injection and the repository pattern with ASP.NET web services?

With regular ASP.NET MVC pages, the repository is passed in to the constructor of the control. Then the tests can instantiate the controller passing in a mock repository.

How can I do this with web services? The problem I see is that we don't have the equivalent of ControllerBuilder.SetControllerFactory.

What are the best practices to get my IoC framework (Castle) to instantiate my web service with the correct repository implementation?

I thought there might be a way to extend HttpHandler and change the way the web service is actually instantiated. I believe this is how the MVC framework does it.

like image 411
Brandon O'Rourke Avatar asked Sep 20 '09 00:09

Brandon O'Rourke


People also ask

Can we use dependency injection in asp net?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.

What is difference between dependency injection and repository pattern?

@McGarnagle Dependency injection means giving an object its instance variables. If you read IOC carefully, In a nutshell it is just asking to add an Interface to create an abstraction, that is what Repository pattern is also doing...and that is reason I asked diff.

What is repository pattern in Web API?

Repository Pattern is an abstraction of the Data Access Layer. It hides the details of how exactly the data is saved or retrieved from the underlying data source. The details of how the data is stored and retrieved is in the respective repository.


1 Answers

This is a good question. I have the same problem. I think that if you are creating web services using .asmx files, it is impossible to use constructor injection. If you were using WCF to implement the web service instead, then I think that it is possible.

In my .asmx web service I let the DI container set the dependencies by setting properties. As my application is also a web form asp.net application, than is how I have to do it, because I cannot use constructor injection on web forms either. But I'm using StructureMap, and it has a BuildUp function that can set properties of an already created object. Not as clean as constructor injection, but a good compromise.

But the web services differentiate themselves from the web forms, becuase I can place the buildup outside of the web form, in the Application_PostMapRequestHandler event. But I have not found out an event that is triggered after the web service class is created. Thus in the constructor of my web service, I have the following code

ObjectFactory.BuildUp(this);

And that is an anti pattern. A class that is initialized by a DI container should not know of the DI container itself. But I have not yet found a better solution.

like image 191
Pete Avatar answered Oct 13 '22 00:10

Pete