Thanks in advance for this. I donβt quite understand how and when to use theses, so I try to think about it in every project Iβm working on to see if it will ever be clicked;) |
Also, the combination of accessibility levels (private, protected, internal) with the keywords static, abstract, and override tends to leave me confused. How to define this method / property / class ....
This is not all a big mystery for me, but some projects encode me in circles when they deal with these topics.
With that said
I have an application that reads an XML document and outputs text and image files. Im also saves all the information in the database. It works for me well.
XML has a standard implementation with required fields and is used by several organizations to send data to my application. All organizations should use (at least) the necessary nodes / elements that are described in the XML Implementation Guide.
So, I want to have a default data object type in order to be able to output a specific organization data type for the required elements. (If this object will be used, these are the fields that must be implemented).
If org. just uses the default requirements, I can use the default object. If they use additional (optional) fields, I must create a new type that inherits the default type.
My first thought was to use an abstract class that had protected properties for my minimum requirements:
public abstract partial class AbstractDataObject { protected string DataObjectName; protected DateTime? DataObjectDate; etc... }
Then, if the organization just uses the required node elements and optional elements, I can use the default object.
internal partial class DefaultDataObject : AbstractDataObject { public new string DataObjectName { get; set; } public new DateTime? DataObjectDate { get; set; } etc... }
But if the organization uses the optional fields of the required node, I can use the derived data object of the organization.
internal sealed partial class OranizationDataObject : AbstractDataObject { public new string DataObjectName { get; set; } public new DateTime? DataObjectDate { get; set; } etc...
Do I need an abstract class? It seems to me that I can just create a DefaultDataObject (something like):
internal partial class DefaultDataObject { public virtual string DataObjectName { get; set; } public virtual DateTime? DataObjectDate { get; set; } etc... }
And then:
internal sealed partial class OranizationDataObject : DefaultDataObject { public override string DataObjectName { get; set; } public override DateTime? DataObjectDate { get; set; } etc...
I'm just trying to figure out how to define these objects so that I can reuse them for each organization. Both methods seem to work, but I hope to understand how to determine them correctly.
Getting XML into the above objects:
public DefaultDataObject ExtractXmlData(XContainer root) { var myObject = (from t in root. Elements("ElementA").Elements("ElementB") select new DefaultDataObject() { DataObjectName = (String)t.Element("ChildElement1"), DataObjectDate = Program.TryParseDateTime((String) t.Elements("ChildElement2") .ElementAtOrDefault(0) ), etc....
OR
public OranizationDataObject ExtractXmlData(XContainer root) { var myObject = (from t in root. Elements("ElementA").Elements("ElementB") select new OranizationDataObject() { DataObjectName = (String)t.Element("ChildElement1"), DataObjectDate = Program.TryParseDateTime( (String)t.Elements("ChildElement2") .ElementAtOrDefault(0)), DataObjectCode = (String)t.Element("ChildElement3"),
etc....
Thanks again for reading. Remember to consult your waiting staff.
Joe