有两个类,nodebase和contentsectionnode,它们继承自抽象类nodebase,我想知道是否有任何方法可以避免在contentsectionnode构造函数中重复一段代码,同时也可以委托给基类构造函数。
抽象的nodebase类构造函数如下所示:
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;
}
contentSectionnode类的ctor如下所示:
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;
}
我想知道是否有什么方法可以避免重复
_createdBy = createdBy;
_createdAt = DateTime.Now;
UpdatedAt = _createdAt;
UpdatedBy = _createdBy;
阻止ContentSectionNode类的所有CTORs。
请不要让_createdby、_createdat和updatedby、updatedat字段/属性只能从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;
}