重构类似的方法但具有不同的返回类型

我有两种方法:


public ActionResult<IEnumerable<IDataObject>> GetRelatedObjects([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName)

{

  if (string.IsNullOrWhiteSpace(primaryKey))

  {

    return BadRequest("Primary key cannot be empty.");

  }


  if (!TryGetClass(className, out var metaClass))

  {

    return NotFound($"Class [{className}] does not exist.");

  }


  if (!metaClass.Properties.TryGetValue(relationName, out var property))

  {

    return NotFound($"Class [{metaClass}] does not contain property [{relationName}].");

  }


  if (!(property is IMetaRelation relation))

  {

    return BadRequest($"Property [{relationName}] is not a relation.");

  }


  if (relation.Target.Cardinality != MetaCardinality.Multiple)

  {

    return BadRequest(`$"Relation [{relationName}] has multiple cardinality."`);

  }


  if (!_primaryKeyHandler.TryParsePrimaryKey(metaClass, primaryKey, out var primaryKeyValues))

  {

    return BadRequest("Cannot convert the primaryKey to the correct type.");

  }


  if (!_dataCalculator.SupportsData(metaClass))

  {

    return BadRequest($"The class [{className}] is not persisted");

  }


  var metaObject = _dataAccess.GetObject<IDataObject>(DataSession, metaClass, primaryKeyValues);

  if (metaObject == null)

  {


    return NotFound($"The metaObject for the primaryKey [{primaryKey}] nad the class [{className}] cannot be empty.");

  }






  // The validations above are the same as for GetRelatedObject()





  try

  {

    var list = _dataAccess.GetRelatedList<IDataObject>(DataSession, metaClass, primaryKeyValues, relation);


    return new ActionResult<IEnumerable<IDataObject>>(list);

  }

  catch (InvalidAuthorizationException)

  {

    return Unauthorized();

  }

}


正如代码中的注释所标记的,两种方法中的验证是相同的。在这两种方法中,都有一小部分代码不相同。两种方法的返回类型也不同。


我正在尝试将方法内的验证重构为一个单独的方法。


绝地无双
浏览 104回答 1
1回答

慕沐林林

您在代码中声明了每种方法的验证都是相同的......但事实并非如此。从获取相关对象:if (relation.Target.Cardinality == MetaCardinality.Multiple){&nbsp; &nbsp; return BadRequest($"Relation [{relationName}] has multiple cardinality.");}从获取相关对象:if (relation.Target.Cardinality != MetaCardinality.Multiple){&nbsp; &nbsp; return BadRequest("The cardinality is not multiple");}话虽这么说,我会做这样的事情,并将验证合并到一个方法中以减少代码重复,添加一个参数以在多个/单个基数之间切换。未经测试,所以我不知道它是否真的有效;)public ActionResult<IDataObject> GetRelatedObject([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName){&nbsp; &nbsp; var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: false);&nbsp; &nbsp; if (validationError) return validationError;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var relationValue = metaObject.GetObject<IDataObject>(relation);&nbsp; &nbsp; &nbsp; &nbsp; return new ActionResult<IDataObject>(relationValue);&nbsp; &nbsp; }&nbsp; &nbsp; catch (InvalidAuthorizationException)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Unauthorized();&nbsp; &nbsp; }}public ActionResult<IEnumerable<IDataObject>> GetRelatedObjects([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName){&nbsp; &nbsp; var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: true);&nbsp; &nbsp; if (validationError) return validationError;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var list = _dataAccess.GetRelatedList<IDataObject>(DataSession, metaClass, primaryKeyValues, relation);&nbsp; &nbsp; &nbsp; &nbsp; return new ActionResult<IEnumerable<IDataObject>>(list);&nbsp; &nbsp; }&nbsp; &nbsp; catch (InvalidAuthorizationException)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Unauthorized();&nbsp; &nbsp; }}private ActionResult ValidateRequest([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName, bool allowMultipleCardinality){&nbsp; &nbsp; if (string.IsNullOrWhiteSpace(primaryKey))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return BadRequest("Primary key cannot be empty.");&nbsp; &nbsp; }&nbsp; &nbsp; if (!TryGetClass(className, out var metaClass))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return NotFound($"Class [{className}] does not exist.");&nbsp; &nbsp; }&nbsp; &nbsp; if (!metaClass.Properties.TryGetValue(relationName, out var property))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return NotFound($"Class [{metaClass}] does not contain property [{relationName}].");&nbsp; &nbsp; }&nbsp; &nbsp; if (!(property is IMetaRelation relation))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return BadRequest($"Property [{relationName}] is not a relation.");&nbsp; &nbsp; }&nbsp; &nbsp; if (AllowMultipleCardinality)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (relation.Target.Cardinality != MetaCardinality.Multiple)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return BadRequest("The cardinality is not multiple");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (relation.Target.Cardinality == MetaCardinality.Multiple)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return BadRequest($"Relation [{relationName}] has multiple cardinality.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!_primaryKeyHandler.TryParsePrimaryKey(metaClass, primaryKey, out var primaryKeyValues))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return BadRequest("Cannot convert the primaryKey to the correct type.");&nbsp; &nbsp; }&nbsp; &nbsp; if (!_dataCalculator.SupportsData(metaClass))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return BadRequest($"The class [{className}] is not persisted");&nbsp; &nbsp; }&nbsp; &nbsp; var metaObject = _dataAccess.GetObject<IDataObject>(DataSession, metaClass, primaryKeyValues);&nbsp; &nbsp; if (metaObject == null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return NotFound($"The metaObject for the primaryKey [{primaryKey}] nad the class [{className}] cannot be empty.");&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP