为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
绝地无双 2023-08-20 14:34:35
我有两种方法: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();  }}正如代码中的注释所标记的,两种方法中的验证是相同的。在这两种方法中,都有一小部分代码不相同。两种方法的返回类型也不同。我正在尝试将方法内的验证重构为一个单独的方法。
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

您在代码中声明了每种方法的验证都是相同的......但事实并非如此。


从获取相关对象:


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

{

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

}

从获取相关对象:


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

{

    return BadRequest("The cardinality is not multiple");

}

话虽这么说,我会做这样的事情,并将验证合并到一个方法中以减少代码重复,添加一个参数以在多个/单个基数之间切换。


未经测试,所以我不知道它是否真的有效;)


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

{

    var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: false);

    if (validationError) return validationError;


    try

    {

        var relationValue = metaObject.GetObject<IDataObject>(relation);

        return new ActionResult<IDataObject>(relationValue);

    }

    catch (InvalidAuthorizationException)

    {

        return Unauthorized();

    }

}


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

{

    var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: true);

    if (validationError) return validationError;


    try

    {

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


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

    }

    catch (InvalidAuthorizationException)

    {

        return Unauthorized();

    }

}


private ActionResult ValidateRequest([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName, bool allowMultipleCardinality)

{

    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 (AllowMultipleCardinality)

    {

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

        {

            return BadRequest("The cardinality is not multiple");

        }

    }

    else

    {

        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.");

    }

}



查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信