WPF + PRISM - Should I inject a WCF client in the View-Model?

I have a WPF application in PRISM architecture.

From what I read on the net, I saw that it is best if View-Models invokes the WCF service using ASYNC , and also it is best to create a new connection for each operation . (I was told that it’s not good to keep in touch alive for too long).

My question is: how do I embed WCF clients in my view model?

If I just create an interface for the "automatically generated" client - and implement the interface - it will just create an instance for the client in my View-Model constructor, but it will not help me if I want to create a new client for every operation that I perform in the window .

For this, I need something like a "client factory" for input.

Can anyone give their opinion on this?

+4
source share
1 answer

First, the IMVHO WCF call is best used in the Model, not in the ViewModel.

You can create a factory helper class, you pass it an interface and pass a specific instance that implements this interface. The factory assistant can still use PRISM to resolve the interface to a specific type, the types will solve everything you registered them for, and each time you do not register them with a life manager, you will get a new instance.

Using PRISM inside the factory helper helps keep everything you need to check for one β€” you can make fun of WCF-related proxy classes during testing, and the target model will never know the difference. If you really want to follow the dependent injection pattern, you can enter the factory helper in the constructor.

0
source

All Articles