0

We have the following solution layout:

  • DataAccess
  • Business Logic
  • WCF Services
  • MVC

We chose to have a WCF service layer, because in the future there will be other applications using the same logic. That's considered to be SOA.

Between the DA, BL and WCF layer I tend to use dependency injection, because I'd like to unit test the Business Logic etc..

But now my question is: should I use dependency injection for the WCF as well? What I mean is, should I pass dependencies through the WCF services from the application (consumer)? Personally I find that weird, because it then no longer seems SOA to me?

Can anyone help me out?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bjornruysen
  • 850
  • 1
  • 6
  • 15
  • Take a look at [this answer](http://stackoverflow.com/a/10699539/264697). It will give you some pointers about how to write a DI friendly WCF service that needs no maintainance at all. – Steven Jun 05 '12 at 12:09

3 Answers3

1

Might be nitpicking, but using services doesn't mean necessarily SOA.

With that out of our way, why would you need to pass dependencies from the application? You can compose your services on the host as shown here.

Community
  • 1
  • 1
Filippo Pensalfini
  • 1,714
  • 12
  • 16
  • Thank you for your answer. I still have some questions though.. If I'd use the solution from the link you gave me, can I still use the "regular Service Reference" approach for implementing the services? – bjornruysen Jun 05 '12 at 11:57
  • Yes, Add Service Reference only creates a proxy client-side to talk to the service. The proxy is unaffected by the injection on the service. – Filippo Pensalfini Jun 05 '12 at 12:17
1

If you want to unit test business logic you can even declare different end points for them and consume then in your unit testing, it's an alternate of dependency injection. And if you really want to use dependency injection then it is always advisable to do it on service level rather then on client but still there is no harm if you use it from client. If i would given choice then i would have done it in Service class and from client i could get a parameter which will suggest whether your are using it for unit testing or application has made an call to your WCF service.

Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • Thank you! So with doing it on a Service level, you mean I shouldn't pass dependencies through WCF? – bjornruysen Jun 05 '12 at 11:58
  • Yes, my reason for not to do it, is because if in future you want to change your testing tool then you have to code for dependency injection again where as if it in server side then you can save time for that part of code. – Deepesh Jun 05 '12 at 12:01
  • 1
    I don't get it, even if you wanted, how would you do Dependency Injection on the client? What would you inject? Besides, declaring different endpoints is not really an alternative to dependency injection, it sounds more like integration testing instead of unit testing. – Filippo Pensalfini Jun 05 '12 at 12:26
1

should I pass dependencies through the WCF services from the application?

I guess the application you mean the consumer of the WCF service right? I believe from the application(MVC) you are talking to the service layer through proxies. You should not inject dependencies to a WCF service from the consumer application and you can't do that.

But you can use dependency injection in WCF to inject data/logging components for better unit testing in the service side itself by creating custom service host factory.

http://prideparrot.com/blog/archive/2012/2/dependency_injection_in_wcf_using_castle_windsor

VJAI
  • 32,167
  • 23
  • 102
  • 164