这里我example="this does not work"为 swashbuckle 框架添加了 XML 文档参数。
/// <summary>Gets a user by id.</summary>
/// <param name="id" example="this does not work">The id of user.</param>
/// <returns>got user.</returns>
[HttpGet("Get")]
[ProducesResponseType(typeof(UserDTO), 200)]
[ProducesResponseType(typeof(AppException), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Get(string id)
{
return HandleResult(await _userServices.GetUserAsync(id));
}
Run Code Online (Sandbox Code Playgroud)
但示例值不会出现在 Swagger UI 中。
我应该去哪里寻找问题?
我有很多类,它们具有类似的映射对象操作。abstract class为了保持代码干燥,我想为所有这些类创建基础。baseabstract class将具有如下通用映射函数:
public TEntity GetEntity(Result<TServiceClientEntity> res)
{
entity = MappingProfiles.TryMap(res.Value);
//some logic with result here
return entity;
}
Run Code Online (Sandbox Code Playgroud)
结果类:
public class Result<T>
{
public T Value{ get; set; }
//some more properties..
}
Run Code Online (Sandbox Code Playgroud)
但问题是我想不出如何映射通用类的方法:
public static class MappingProfiles
{
public static T2 TryMap<T,T2>(T t)
{
return (T2)Map((Real_T_type)t); //f.e.: the type is ExampleFrom
}
public static ExampleTo Map(ExampleFrom from)
{
return new ExampleTo
{
exapleValue = from.exapleValue
};
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我还希望TryMap通用方法使用我预定义的Map手动方法进行映射。
我尝试创建具有一定数量的非空属性(> 7)的类。
如果我这样做:
public class Meeting
{
public Name Name { get; set; }
public Person ResponsiblePerson { get; set; }
public Description Description {get; set; }
public Category Category { get; set; }
public Type Type { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public List<Person> Attendees { get; set; }
public Meeting()
{
Attendees = new List<Person>();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的警告: “退出构造函数时,不可为空的属性 '...' 必须包含非空值。请考虑将该属性声明为可空。”
我想到了一个变体列表:
默认值删除了这个警告,但我认为,它只会添加无用的值,将来无论如何都需要更改。
如果我初始化构造函数中的所有属性,此警告就会消失,但构造函数将具有 >7 个参数,这将是一场噩梦。 …