我有两种方法:
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();
}
}
正如代码中的注释所标记的,两种方法中的验证是相同的。在这两种方法中,都有一小部分代码不相同。两种方法的返回类型也不同。
我正在尝试将方法内的验证重构为一个单独的方法。
慕沐林林
相关分类