2016-08-31 37 views
5

NodeBase ve soyut sınıf NodeBase'den miras alınan ContentSectionNode adlı iki sınıfa sahip olmak, ContentSectionNode yapıcılarında bir kod bloğunu tekrar etmekten kaçınmanın herhangi bir yolu olup olmadığını bilmek isterim. Ayrıca temel sınıf kurucularına delege. C# temel yapıcılarla çalışırken tekrar etmekten kaçının

soyut NodeBase sınıf vektörlerin içine klonlanabilir

şuna benzer:

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 sınıf vektörlerin içine klonlanabilir bunlar gibi görünür: Ben önlemek gibi bir ihtimal var ise

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; 
} 

ben bilmek istiyorum ContentSectionNode sınıfının tüm katörlerinde

bloklarını tekrarlamak. Lütfen _createdBy, _createdAt ve UpdatedBy, UpdateAt alanlarının/props'lerinin yalnızca ContentSectionNode sınıfından erişilebilir olduğunu ve sadece orada ayarlanabileceğini unutmayın.

Proje C# 5.0 kullanıyor, bu nedenle otomatik özellik başlatıcıları yok. Teşekkürler!

cevap

7

Bunu beğendiniz mi?

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; 
} 
+1

Bu işe yarıyor gibi görünüyor :) Teşekkürler! Çözüm o kadar basit ki onu göremedim. Tekrar teşekkürler! –