如果 C# 和 JavaScript 应用程序相互通信,则 DTO 的命名约定
Naming conventions for DTOs if C# and JavaScript Apps are talking to each other
TypeScript class 实体的命名约定说我应该在 TypeScript 中为我的属性名称使用驼峰式命名。
很喜欢:
export class Bird {
type: string;
nameOfBird: string;
}
但是 C# 中的命名约定说我应该在 class 属性前加上 _:
public class Bird {
public string _type {get; set;}
public string _nameOfBird {get; set;}
}
但是当在我的应用程序之间将它们作为 JSON 发送时,我遇到了冲突,因为我不知道我是否应该在我的 JSON 对象中使用 camelCase 或 _case。而且它似乎也使编组更加困难。
你是怎么处理的?忽略其中一条准则还是在两者之间进行编组?
我不知道属性的 _ 前缀是 C# 命名约定,但是,假设您是对的,至少对于您的公司而言,有一种非常好的方法可以解决您刚刚提出的悖论呈现。创建:
private string _nameOfBird;
public string nameOfBird {
get {
return _nameOfBird;
}
set {
_nameOfBird = value;
}
}
这样你就尊重了这两个约定。
你可以同时实现 - 只需使用序列化属性:
public class Bird {
[JsonProperty("type")]
public string _type {get; set;}
[JsonProperty("nameOfBird")]
public string _nameOfBird {get; set;}
}
甚至
[JsonProperty("type")]
public string AnyCompletelyDifferentName { get; set; }
除此之外,C# 中没有关于 public 成员前缀的约定。但是,也许您的公司中有一个。
TypeScript class 实体的命名约定说我应该在 TypeScript 中为我的属性名称使用驼峰式命名。
很喜欢:
export class Bird {
type: string;
nameOfBird: string;
}
但是 C# 中的命名约定说我应该在 class 属性前加上 _:
public class Bird {
public string _type {get; set;}
public string _nameOfBird {get; set;}
}
但是当在我的应用程序之间将它们作为 JSON 发送时,我遇到了冲突,因为我不知道我是否应该在我的 JSON 对象中使用 camelCase 或 _case。而且它似乎也使编组更加困难。
你是怎么处理的?忽略其中一条准则还是在两者之间进行编组?
我不知道属性的 _ 前缀是 C# 命名约定,但是,假设您是对的,至少对于您的公司而言,有一种非常好的方法可以解决您刚刚提出的悖论呈现。创建:
private string _nameOfBird;
public string nameOfBird {
get {
return _nameOfBird;
}
set {
_nameOfBird = value;
}
}
这样你就尊重了这两个约定。
你可以同时实现 - 只需使用序列化属性:
public class Bird {
[JsonProperty("type")]
public string _type {get; set;}
[JsonProperty("nameOfBird")]
public string _nameOfBird {get; set;}
}
甚至
[JsonProperty("type")]
public string AnyCompletelyDifferentName { get; set; }
除此之外,C# 中没有关于 public 成员前缀的约定。但是,也许您的公司中有一个。