【问题标题】:Trying to eliminate redundant code execution for validation and entity试图消除验证和实体的冗余代码执行
【发布时间】:2015-02-23 05:37:11
【问题描述】:

我正在寻找是否有办法消除我的方法对谷歌地图计算长/纬度坐标的两个调用之一。

这是我的方法。

    public static GeocoderCoordinates GetCoordinates(string region)
    {
        WebRequest request = WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" + HttpUtility.UrlEncode(region));

       using (WebResponse response = request.GetResponse())
       {
          using (Stream stream = response.GetResponseStream())
          {
             XDocument document = XDocument.Load(new StreamReader(stream));

             XElement longitudeElement = document.Descendants("lng").FirstOrDefault();
             XElement latitudeElement = document.Descendants("lat").FirstOrDefault();

             if (longitudeElement != null && latitudeElement != null)
             {
                return new GeocoderCoordinates
                {
                   Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                   Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                };
             }
          }
        }
        return null;
    }

我第一次调用这个方法是为了验证。

internal class ValidateLocationAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var location = value as string;

        GeocoderCoordinates coordinates = Geocoding.GetCoordinates(location);
        if (coordinates == null)
            return false;

        return true;
    }
}

如果没有找到位置,则返回 null - 验证失败。 第二次调用它是在控制器中设置我的实体内的经度/纬度坐标。

[HttpPost]
    public ActionResult Edit(EditStudentViewModel viewModel)
    {   
        if (ModelState.IsValid)
        {
            Student student = studentRepository.Find(User.Identity.GetUserId());

            if (student == null)
            {
                var newStudent = new Student
                {
                    AspNetUserRefId = viewModel.AspNetUserRefId,
                    CatchPhrase = viewModel.CatchPhrase,
                    StartedPracticing = Convert.ToInt16(viewModel.SelectedYearId),
                    LocationPoints = Geocoding.GetDbGeography(viewModel.Location),
                    Location = viewModel.Location,

所以我运行这个方法两次只是为了插入/更新一个学生。好像有点多余。

当控制器中的代码正在运行时,是否有办法触发/设置验证状态,所以当用户调用此方法时,我不必调用此方法两次(一次用于验证,一次用于设置实际值)提交表格?

我考虑过缓存,但认为这不是一个好主意,除非有人能指出什么。

【问题讨论】:

  • 我没有找到另一个对 GetCoordinates 的调用。
  • 你在哪里应用ValidateLocationAttribute?
  • 我将其应用于视图中显示的视图模型成员。这是一个文本框......所以用户输入一个位置前。旧金山,验证方法运行。然后,如果所有验证都通过了控制器操作方法,它会再次调用 geolocation 方法以获取位置并在提交到数据库之前设置 long/lat 值

标签: c# asp.net-mvc redundancy asp.net-validators


【解决方案1】:

如果您认为使用文本框上的属性预先应用验证可为用户提供价值(早期反馈),请保持原样。考虑到解决方案的价值和清洁度,两次调用一点也不差。

第二个选项是您可以删除该属性,并在控制器操作中执行验证。如果验证失败,则显示具有所有相同数据但文本框值(位置)的错误消息的相同表单。用户需要选择另一个位置然后提交。

这是一种权衡。

重要提示:您可以通过将区域名称存储在您的数据库中来优化您的解决方案,并且仅当您的数据库中不存在该区域名称时才使用 Google API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多