Last active
September 26, 2019 12:10
-
-
Save gabrielcalegari/17795883a12c03dd1116e75517521bde to your computer and use it in GitHub Desktop.
Proposal for Generic Document Architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDocumentPart | |
{ | |
string Content { get; } | |
} | |
public interface IDocumentBody : IDocumentPart | |
{ | |
} | |
public interface IDocumentHeader : IDocumentPart | |
{ | |
} | |
public interface IDocumentFooter : IDocumentPart | |
{ | |
} | |
public class Document | |
{ | |
public IReadOnlyCollection<IDocumentPart> Parts => _parts; | |
private readonly List<IDocumentPart> _parts; | |
public Document(IDocumentBody body) | |
{ | |
body = body ?? throw new ArgumentNullException(nameof(body)); | |
_parts = new List<IDocumentPart> { body }; | |
} | |
public bool AddPart(IDocumentPart part) | |
{ | |
if (_parts.Exists(p => p.GetType() == part.GetType())) | |
return false; | |
_parts.Add(part); | |
return true; | |
} | |
public bool TryGetPart<T>(out T part) where T : IDocumentPart | |
{ | |
part = (T) _parts.SingleOrDefault(p => p.GetType() == typeof(T)); | |
return part != null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A document is made by its parts and that could be extended for other parts. An existence condition for document is that it needs always a body. A document can not have more than one instance of the same part.