Skip to content

Instantly share code, notes, and snippets.

@gabrielcalegari
Last active September 26, 2019 12:10
Show Gist options
  • Save gabrielcalegari/17795883a12c03dd1116e75517521bde to your computer and use it in GitHub Desktop.
Save gabrielcalegari/17795883a12c03dd1116e75517521bde to your computer and use it in GitHub Desktop.
Proposal for Generic Document Architecture
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;
}
}
@gabrielcalegari
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment