ale*_*x.g 5 .net c# architecture c#-5.0
有两个类,NodeBase和ContentSectionNode继承自抽象类NodeBase,我想知道是否有任何方法可以避免在ContentSectionNode构造函数中重复一段代码,同时也委托给基类构造函数.
抽象的NodeBase类ctors看起来像这样:
protected NodeBase(string tagType, string content)
: this()
{
TagType = tagType;
Content = content;
}
protected NodeBase(Guid? parentId, int? internalParentId, string tagType, string content)
: this(tagType, content)
{
ParentId = parentId;
InternalParentId = internalParentId;
}
Run Code Online (Sandbox Code Playgroud)
ContentSectionNode类ctors看起来像这样:
public ContentSectionNode(Guid createdBy)
: this()
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
:base(tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何方法可以避免重复
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
Run Code Online (Sandbox Code Playgroud)
阻止ContentSectionNode类的所有ctors.请注意,_createdBy,_createdAt和UpdatedBy,UpdatedAt字段/ props只能从ContentSectionNode类访问,并且只能在那里设置.
该项目使用的是C#5.0,因此没有自动属性初始值设定项.谢谢!
像这样?
public ContentSectionNode(Guid createdBy)
: this(createdBy,null,null, null, null)
{
}
public ContentSectionNode(Guid createdBy, string tagType, string content)
: this(createdBy, null, null tagType, contect)
{
}
public ContentSectionNode(Guid createdBy, Guid? parentId, int? internalParentId, string tagType, string content)
: base(parentId, internalParentId, tagType, content)
{
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |