Unity and web service
I had this awesome idea... but I am afraid maybe it is actually a bad idea....
we use unity for dependency injection.
I make interfaces from my web services using partial classes for the purpose of mocking and web services....
What I want to do 开发者_开发知识库is put my web services into unity and get them via dependency injection...
What do you think? Is there too much overhead somewhere? Memory leaks? Is this a bad idea?
Remember: don't use a DI Container to look up services - it's the Service Locator anti-pattern. Instead, use Constructor Injection to get your dependencies into your consumers.
You can also do this with web services - at least with WCF it's possible to use Constructor Injection for the service implementation.
This means that you can inject all necessary dependencies into your web service and let Unity resolve them all for you. That would be the correct approach. That's not a bad idea at all.
To prevent memory leaks you must remember to Release each resolved instance after use. That is, however, trivial (one of the included links here shows how to do that).
I think it's worth a shot to get your web proxy classes via unity. I can envision your mock tests becoming a lot easier to configure and execute. Actually, the more I think about it, the idea seems pretty solid and worth the effort.
精彩评论