c# - 如何使这段代码变干?

标签 c# wcf rest

我正在使用 C#/wcf 编写 RESTful 服务,需要在 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 个函数。有什么方法可以为/positions/制作 1 个模板来覆盖所有可能的“?”场景?

最佳答案

Take enumerates source and yields elements until count elements have been yielded or source contains no more elements.

Take(n)返回最多 n 个项目,但如果可用的项目较少,您可以重写:

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

然后将返回所有项目。

关于c# - 如何使这段代码变干?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245733/

相关文章:

c# - 检查字节是否为 0x00

WCF Rest - 在一个 ServiceContract 中混合 HTTP 和 HTTPS

c# - 从 Android KSoap2 使用在 Mono 上运行的 WCF Soap 服务

sql - 查询和 WCF 服务的最佳实践

c# - 如何在 C# 中创建 REST Web 服务来处理 Docusign Connect 服务?

swift - 在 Swift 中发布休息调用

c# - 将 Ninject 与 ASP.NET MVC 和服务层结合使用

c# - 通用方法枚举到字符串转换

java - 在 Java 中实现 REST API 客户端和 JSON 解析器的最简单方法?

c# - 如何只修改 LINQ 投影中的一个或两个字段?