patterncsharpMinor
WCF client class
Viewed 0 times
clientclasswcf
Problem
The class is responsible for wrapping requests to a WCF Data Service, which tidies up calling code. Transactions are implicitly supported. It should efficiently send just updates to the services (only changed values). The next step is to modify it and allow named properties to be queried, rather than all fields.
Please review this.
Please review this.
using System;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Services.Client;
using System.Data.Services.Common;
using System.Threading.Tasks;
internal class ServiceWrapper
{
private DataServiceContext context;
public ServiceWrapper(Uri uri, string collection)
{
context = new DataServiceContext(uri, DataServiceProtocolVersion.V3);
CollectionName = collection;
}
public string CollectionName { get; private set; }
public DataServiceContext Context
{
get { return context; }
}
public DataServiceQuery QueryAll
{
get { return Context.CreateQuery(CollectionName); }
}
public DataServiceQuery InvokeMethod(string opName, params Tuple[] args)
{
DataServiceQuery query = Context.CreateQuery(opName);
foreach (Tuple pair in args)
{
query = query.AddQueryOption(pair.Item1, pair.Item2);
}
return query;
}
public void Add(T entity)
{
context.AddObject(CollectionName, entity);
}
public void Update(T entity)
{
context.AttachTo(CollectionName, entity);
context.UpdateObject(entity);
}
public void Delete(T entity)
{
context.AttachTo(CollectionName, entity);
context.DeleteObject(entity);
}
public async Task CommitAsync()
{
await Task.Run(() => context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate | SaveChangesOptions.Batch));
}
}Solution
Some initial thoughts off the top of my head. I personally like immutability in my class data where possible. You have such available:
Also, if you have LINQ available to you, you can simplify
private readonly DataServiceContext context;
private readonly string collectionName;
public ServiceWrapper(Uri uri, string collection)
{
context = new DataServiceContext(uri, DataServiceProtocolVersion.V3);
collectionName = collection;
}
public string CollectionName { get { return collectionName; } }Also, if you have LINQ available to you, you can simplify
InvokeMethod():public DataServiceQuery InvokeMethod(string opName, params Tuple[] args)
{
return args.Aggregate(
Context.CreateQuery(opName),
(current, pair) => current.AddQueryOption(pair.Item1, pair.Item2));
}Code Snippets
private readonly DataServiceContext context;
private readonly string collectionName;
public ServiceWrapper(Uri uri, string collection)
{
context = new DataServiceContext(uri, DataServiceProtocolVersion.V3);
collectionName = collection;
}
public string CollectionName { get { return collectionName; } }public DataServiceQuery<T> InvokeMethod(string opName, params Tuple<string,object>[] args)
{
return args.Aggregate(
Context.CreateQuery<T>(opName),
(current, pair) => current.AddQueryOption(pair.Item1, pair.Item2));
}Context
StackExchange Code Review Q#31241, answer score: 3
Revisions (0)
No revisions yet.