Reputation: 562
I am new to Rest services. I did fair bit of research on this topic but still not clearly understood. Hope someone clear this to me. Thanks in advance.
My client is going to execute a method on my service class through an url. All I understood is client going to call
localhost/service/get/customers
In my rest service I have dependencies like a IRepository
so that I can go to database and fetch the records. If I go with constructor injection when would client call this constructor?
It looks like I need to use a service locator to fetch IRepository
inside the method which I need it.
Doesn't this violate OOP principle? Is testing easy without DI?
Can anyone please clarify this.
For any service how do we expect client to know what is IRepository
is what methods it does have?
Is isn't it for internal implementation purpose Do we need t expose to the client? Why should he bother about it? Can't I provide just Uri Which method to call (localhost/service/Products) and job done.
I will appreciate if you provide any live example.
Thanks a lot.
Upvotes: 0
Views: 743
Reputation: 16878
No, it will not violate OOP priciple, if you will resolve for dependency inside a method. But there is no need for that, you can use constructor Dependency Injection within WCF using Instance Provider and ServiceHostFactory, there is also good example in this question.
If you are self-hosting the service, it will be even more simpler because it requires implementing only IInstanceProvider
and ServiceHost
. Assuming that you have Service
service with constructor taking an IDependency
parameter:
public class CustomInstanceProvider : IInstanceProvider, IContractBehavior
{
private readonly IDependency dependency;
public CustomInstanceProvider(IDependency dependency)
{
this.dependency = dependency;
}
public object GetInstance(InstanceContext instanceContext)
{
// Here you are injecting dependency by constructor
return new Service(dependency);
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return this.GetInstance(instanceContext);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.InstanceProvider = this;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
}
public class CustomServiceHost : ServiceHost
{
public CustomServiceHost(IDependency dependency, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
foreach (var cd in this.ImplementedContracts.Values)
{
cd.Behaviors.Add(new CustomInstanceProvider(dependency));
}
}
}
Then when creating service, use CustomServiceHost
:
var serviceHost = new CustomServiceHost(dependency, typeof(Service), uris);
Upvotes: 2