Increase flexibility of constructors, e.g., for pure functional transformations
See original GitHub issueDescription
In the context of pure functional transformations, it would be beneficial to construct OpenXmlElement
instances very flexibly. For example, one might want to construct a new element in the following way:
return new Paragraph(
new ParagraphProperties(new ParagraphStyleId { Val = "MyStyleId" }),
FunctionReturningIEnumerableOfRun(someParams));
Information
- .NET Target: All
- DocumentFormat.OpenXml Version: Latest
See discussion in #627.
Observed
Using the Paragraph
class as an example, the subclasses of OpenXmlElement
define the following constructors:
public Paragraph()
{
}
public Paragraph(IEnumerable<OpenXmlElement> childElements)
: base(childElements)
{
}
public Paragraph(params OpenXmlElement[] childElements)
: base(childElements)
{
}
public Paragraph(string outerXml)
: base(outerXml)
{
}
With those constructors, the example instance can’t be created, because we can’t combine an OpenXmlElement
and an IEnumerable<OpenXmlElement>
.
Expected
We should be able to create instances in the way shown in the description.
Design Idea
We could borrow the design from the System.Xml.Linq.XElement
class, which, owing to how you can construct instances, is instrumental in pure functional transformations.
The two key XElement
constructors for pure functional transformations are:
public XElement(XName name, object content)
: this(name)
{
this.AddContentSkipNotify(content);
}
public XElement(XName name, params object[] content)
: this(name, (object) content)
{
}
Note how the second constructor delegates to the first one. Thus, the magic happens in the AddContentSkipNotify(object)
method, which works with pretty much any meaningful actual parameter you throw at it.
Thus, using the Paragraph
example, we could replace:
Paragraph(IEnumerable<OpenXmlElement>)
withParagraph(object)
andParagraph(params OpenXmlElement[])
withParagraph(params object[])
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
@twsouthwick, yes, please do that.
@twsouthwick, let me explore the extension method route. My initial thought is that this would enable the same approach while only requiring a little more code.