How to make this code DRY?

I am writing a RESTful service with C # / wcf and you need to put filters in GET. How and how many records to return, maybe if I want to filter something, etc. Consider this code:

[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();
            }
        }

Lots of repetitions. I can move the code to another function, but still I have 2 templates, I have 2 functions. Is there a way to make 1 template for / provisions / that will cover all possible "?" scripts?

+3
source share
2 answers

QA hint : context.Devices.Where(aa => aa.DeviceId == deviceId).FirstOrDefault();may be shortened tocontext.Devices.Find(deviceId);

QA hint : from p in context.Positions ...you can create a view in your table, and instead ... select new GPSPosition { ... }just write normalcontext.PositionViews.Where(x => x.DeviceKey == d.DeviceKey).ToList();

QA. .AsNoTracking() .

QA. . :

public List<GPSPosition> GetDevicePositions(string deviceId, int limit = 20)
{
// you code here
}

, WCF . :

UriTemplate WebGet WebInvoke WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters , .. .

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451296&wa=wsignin1.0

+2

.

Take(n) n , , , :

  public List<GPSPosition> GetDevicePositions(string deviceId)
  {
    return GetDevicePositions2(deviceId, int.MaxValue)
  }

.

+3

All Articles