patterncsharpModerate
Simple specific classes design
Viewed 0 times
designsimpleclassesspecific
Problem
I am new to c#, and rather new to design-patterns. I want to create a simple and a most elegant solution for loading and validating a xml file and later on extracting its fields.
I've started with a generic
So please, have no mercy !!
```
public interface IData
{
String GetDataString();
}
class XMLValidator
{
private StringBuilder verificationErrorsList = new StringBuilder();
public string ValidationSchema { get; set; }
public String ValidationErrors
{
get { return verificationErrorsList.ToString(); }
}
public bool HasErrors
{
get {return (verificationErrorsList.Length > 0);}
}
public void Validate(IData xmlData)
{
try
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
XmlReaderSettings rs = new XmlReaderSettings();
rs.ValidationType = ValidationType.Schema;
rs.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(rs_ValidationEventHandler);
rs.Schemas.Add(null, ValidationSchema);
rs.CloseInput = true;
rs.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader r = new StringReader(xmlData.GetDataString());
using (XmlReader reader = XmlReader.Create(r, rs))
{
try
{
while (reader.Read()) { }
}
catch (Xm
I've started with a generic
IData interface, that gets implemented by a XmlFileData class, but I feel that there is much more to be improved upon, but I lack the knowledge to do it (from design, to naming variables and methods and so on).So please, have no mercy !!
```
public interface IData
{
String GetDataString();
}
class XMLValidator
{
private StringBuilder verificationErrorsList = new StringBuilder();
public string ValidationSchema { get; set; }
public String ValidationErrors
{
get { return verificationErrorsList.ToString(); }
}
public bool HasErrors
{
get {return (verificationErrorsList.Length > 0);}
}
public void Validate(IData xmlData)
{
try
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
XmlReaderSettings rs = new XmlReaderSettings();
rs.ValidationType = ValidationType.Schema;
rs.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(rs_ValidationEventHandler);
rs.Schemas.Add(null, ValidationSchema);
rs.CloseInput = true;
rs.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader r = new StringReader(xmlData.GetDataString());
using (XmlReader reader = XmlReader.Create(r, rs))
{
try
{
while (reader.Read()) { }
}
catch (Xm
Solution
A few things:
-
-
The interface is a bit ambiguous - which method am I supposed to use
As of now it doesn't really provide enough value to warrant it's existence. You'd be better of having
-
Consider passing the
-
In the validator when the reader throws an exception because there was a problem reading from the stream for example then you catch those as validation errors which I'd consider technically wrong. Your validator should catch validation errors - i.e. anything that has to do with invalid XML. Everything else should not be of its concern. So don't catch
-
IData is not a particularly good name for the interface as it represents a source of data rather than the data itself. Therefor IDataSource seems more appropriate.-
The interface is a bit ambiguous - which method am I supposed to use
GetDataStream or GetDataString? Why use one or the other? Could I call both multiple times and interchangeably?As of now it doesn't really provide enough value to warrant it's existence. You'd be better of having
XmlValidator accept a TextReader and let the calling code deal with how to wrap it around the source.-
Consider passing the
ValidationSchema as part of the constructor for the XmlValidator - it's an integral part of its validation logic.-
In the validator when the reader throws an exception because there was a problem reading from the stream for example then you catch those as validation errors which I'd consider technically wrong. Your validator should catch validation errors - i.e. anything that has to do with invalid XML. Everything else should not be of its concern. So don't catch
Exception - be more specific.Context
StackExchange Code Review Q#40287, answer score: 10
Revisions (0)
No revisions yet.