Я пишу RESTful-сервис на C # / wcf и мне нужно установить фильтры на GET.Например, сколько записей нужно вернуть, может быть, если я захочу что-то отфильтровать и т. Д. Рассмотрим этот код:
[WebGet(UriTemplate = "/devices/{DeviceId}/positions")]
public List<GPSPosition> GetDevicePositions(string deviceId)
{
//Lookup device:
using (var context = new MobileModelContext(ContextManager.AccountGKey.Value))
{
var d = context.Devices.Where(aa => aa.DeviceId == deviceId).FirstOrDefault();
if (d == null)
{
outgoingResponse.StatusCode = HttpStatusCode.NotFound;
outgoingResponse.StatusDescription = "Device not found";
return null;
}
var query = from p in context.Positions
where p.DeviceKey.Equals(d.DeviceKey)
select new GPSPosition
{
PositionGKey = p.PositionGKey,
Latitude = p.Latitude,
Longitude = p.Longitude,
Speed = p.Speed,
Accuracy = p.Accuracy,
Altitude = p.Altitude,
GPSTime = p.GPSTime,
DeviceTime = p.DeviceTime
};
return query.ToList();
}
}
[WebGet(UriTemplate = "/devices/{DeviceId}/positions?RecordCount={RecordCount}")]
public List<GPSPosition> GetDevicePositions2(string deviceId, int recordCount)
{
//Lookup device:
using (var context = new MobileModelContext(ContextManager.AccountGKey.Value))
{
var d = context.Devices.Where(aa => aa.DeviceId == deviceId).FirstOrDefault();
if (d == null)
{
outgoingResponse.StatusCode = HttpStatusCode.NotFound;
outgoingResponse.StatusDescription = "Device not found";
return null;
}
var query = from p in context.Positions
where p.DeviceKey.Equals(d.DeviceKey)
select new GPSPosition
{
PositionGKey = p.PositionGKey,
Latitude = p.Latitude,
Longitude = p.Longitude,
Speed = p.Speed,
Accuracy = p.Accuracy,
Altitude = p.Altitude,
GPSTime = p.GPSTime,
DeviceTime = p.DeviceTime
};
return query.Take(recordCount).ToList();
}
}
Много повторений.Я могу переместить код в другую функцию, но, тем не менее, у меня есть 2 шаблона, у меня есть 2 функции.Есть ли способ сделать 1 шаблон для / position /, который охватит все возможные "?"сценарии?