For the complete tutorial directory, see :《CustomData service providers-Introduction"
In the previous tutorial, we have implementedIserviceproviderAndIdataservicemetadataprovider, Now let's continue.
Mount Idataservicequeryprovider Implementation
Now we plan to implementIdataservicequeryproviderInterface, We Need To refactor the previously implementedIserviceproviderInterface.
Public abstract class dspdataservice <t>:
Dataservice <t>, iserviceprovider
{
Public idataservicemetadataprovider _ metadata;
Public idataservicequeryprovider _ query;
Public dspdataservice ()
{
_ Metadata = getmetadataprovider (typeof (t ));
_ Query = getqueryprovider (_ metadata );
}
Public object getservice (type servicetype)
{
If (servicetype = typeof (idataservicemetadataprovider ))
Return _ metadata;
Else if (servicetype = typeof (idataservicequeryprovider ))
Return _ query;
Else
Return NULL;
}
Public abstract idataservicemetadataprovider
Getmetadataprovider (type datasourcetype );
Public abstract idataservicequeryprovider
Getqueryprovider (idataservicemetadataprovider metadata );
}
Note: You need to implement it in your own implementation class.GetqueryproviderMethod.
IdataservicequeryproverImplementation may requireIdataservicemetadataproviderSo this parameter is passed in.
Public class sample: dspdataservice <Object>
{
...SeeCode...
Public override idataservicequeryprovider
Getqueryprovider (idataservicemetadataprovider metadata)
{
Return new dspqueryprovider <Object> ();
}
}
Implementation Idataservicequeryprovider
If you only want to see the service contract and$ MetadataInformation, then implementCurrentdatasourceAttribute. Other methods of this interface are returned after the query is called.Resourcesets.
Public class dspqueryprovider <t>: idataservicequeryprovider
{
T_currentdatasource;
Public object currentdatasource
{
Get {
Return _ currentdatasource;
}
Set {
_ Currentdatasource = value as t;
}
}
Public object getopenpropertyvalue (
Object target,
String propertyname)
{
Throw new notimplementedexception ();
}
Public ienumerable <keyvaluepair <string, Object>
Getopenpropertyvalues (object target)
{
Throw new notimplementedexception ();
}
Public object getpropertyvalue (
Object target,
Resourceproperty)
{
Throw new notimplementedexception ();
}
Public iqueryable getqueryrootforresourceset (
Resourceset)
{
Throw new notimplementedexception ();
}
Public resourcetype getresourcetype (object target)
{
Throw new notimplementedexception ();
}
Public object invokeserviceoperation (
Serviceoperation,
Object [] parameters)
{
Throw new notimplementedexception ();
}
Public bool isnullpropagationrequired
{
Get {Throw new notimplementedexception ();}
}
}
Well, at least our custom data service provider can now run, although it can only provide metadata information.
In the browser, type:Http: // localhost/sample. SVCNow you can see the following picture:
Note: InVisual StudioIn the debugging environment,LocalhostA dynamic port is required later)
If you typeHttp: // localhost/sample. svc/$ metadataYou should see the following picture:
Of course, we have not implemented the query. If you want to navigateProductsIf the resource set is used, an error is returned (the address is:Http: // localhost/sample. svc/products