Posts Tagged “.net”
In my previous post, I talked about how we could use System.Xml.Serialization.XmlSerializer and System.Runtime.Serialization.DataContractSerializer to parse XML into an object tree. I pointed out that DataContractSerializer in my mind has some advantages in that types, properties, and members to use with the deserialization do not need to be public. On the downside, DataContractSerializer puts a quite limiting constraint on the XML format in that it cannot parse XML attributes. This is as far as I know, an absolute constraint that cannot easily be circumvented. Thus, regrettably, if we are not in control of the XML format, DataContractSerializer is sometimes useless.
In those situations, we can still use XmlSerializer. In order to achieve the same encapsulation with XmlSerializer, we have to adjust our model a bit. Here’s one suggestion on how to do this:
My approach to this is inspired by Josh Bloch‘s Builder pattern. The idea is changing the classes used for deserialization from being domain objects to being builder objects that build domain objects. This has another advantage in that our domain objects are not “polluted” with attributes and interfaces related to deserialization and are 100% plain old CLR objects (POCOs). So, lets first do a change our “deserialization” class (formerly ‘Country’) to a builder object, like so:
public class CountryBuilder
{
[XmlElement(ElementName = "name")]
public string Name;
[XmlElement(ElementName = "iso-3166-alpha-2-code")]
public string Code;
public Country Build()
{
return new Country(Name, Code);
}
}
Note here that our builder object has a Build() method which returns the domain object ‘Country’. This is the object that we will pass on to our clients. The Country class now represents our domain object:
public class Country
{
private readonly string _name, _code;
internal Country(string name, string code)
{
_name = name;
_code = code;
}
public string Name { get { return _name; } }
public string Code { get { return _code; } }
}
We have now restricted access to the creation of Country objects in that the constructor is internal, and it is also immutable (cannot change state once created) though making its fields readonly. It only exposes getters for its internal state. We can then do the same to our list of countries. The builder object for countries would look like this:
[XmlRoot("countries")]
public class Countries
{
[XmlElement(ElementName="country")]
public CountryBuilder[] countries;
public IEnumerable<Country> Build()
{
return countries.Select<CountryBuilder , Country>(x => x.Build());
}
}
The code for doing the serialization will then look like this:
string xml = ...;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CountriesBuilder));
var builder = xmlSerializer.Deserialize(new StringReader(inputXml)) as CountriesBuilder;
IEnumerable<Country> cs = builder.Build();
What we have achieved now is that we now can control the accessibility and encapsulation of our domain model. The builder objects are still public to anyone, but that really does not matter much in my mind. Thus, a relatively swift parsing of XML of various formats into a well designed object model.
No Comments »
If you want to parse XML in .NET, you have a lot of options to choose from. You can use XmlDocument to parse the XML into a DOM tree, you can use the XmlReader to write an efficient “pull” parser, or you can leverage some of the features provided with various serialization APIs.
Given the case where you have a fairly straightforward XML document (not too deep document tree, not too complex set of attributes and elements) that maps pretty well to your domain model, the serialization options is in my mind a good choice that requires little coding. Compared with this approach, using XmlDocument seems to be a bit of an overkill if you don’t need advanced traversal of the document, and writing a parser by hand using XmlReader seems to require quite a bit of coding.
So, given the following sample XML document, I will investigate the serialization options:
<countries>
<country>
<iso-3166-alpha-2-code>AF</iso-3166-alpha-2-code>
<name>Afghanistan</name>
</country>
<country>
<iso-3166-alpha-2-code>AX</iso-3166-alpha-2-code>
<name>Åland Islands</name>
</country>
<country>
<iso-3166-alpha-2-code>AL</iso-3166-alpha-2-code>
<name>Albania</name>
</country>
</countries>
Using System.Xml.XmlSerializer
The first option that came to mind, was to use the XmlSerializer object to deserialize the XML into C# (or VB for that matter) objects. It first requires that I annotate my object model in order to tell the serializer how to deserialize the XML:
[XmlRoot("countries")]
public class Countries
{
[XmlElement(ElementName="country")]
public Country[] countries;
}
public class Country
{
[XmlElement(ElementName = "name")]
public string Name;
[XmlElement(ElementName = "iso-3166-alpha-2-code")]
public string Code;
}
Then, I can use the serializer to deserialize the code:
string xml = ...;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Countries));
Countries c = xmlSerializer.Deserialize(new StringReader(xml)) as Countries;
Pretty sweet, heh? Definitely. However, this has some drawbacks. If I want my Country class to be a well designed domain object that follows good OO design principles, I probably would like to encapsulate my data. Furthermore, I might want to restrict the creation of such objects from other parts of the code. In order for XmlSerializer to create my object, it requires that my types are public and that all properties or fields to set are public as well. What to do if I want to enforce my objects to be immutable once handed off to other parts of the code?
Using System.Runtime.Serialization.DataContractSerializer
Luckily, the serialization API that come with Windows Communication Framework has some neat features that fit like a glove. When defining my data model, it does not require that the types, neither the properties nor fields to set are public. Actually, I can restrict access to the type, its default constructor, and any of the properties or fields that I want to be deserialized! w00t!
So, this is what the Country class will looks like:
[DataContract(Name="country", Namespace="")]
internal class Country : IExtensibleDataObject
{
private Country() { }
[DataMember(Name="name")]
public string Name { get; private set; }
[DataMember(Name = "iso-3166-alpha-2-code")]
public string Code { get; private set; }
public ExtensionDataObject ExtensionData { get; set; }
}
The XML file contains a list of countries, and luckily, we have the CollectionDataContractAttribute to denote an element that is a list of elements. It supports generics, so that we can define our class as a strongly typed list:
[CollectionDataContract(Name="countries", Namespace="")]
internal class Countries : List<Country>, IExtensibleDataObject
{
public ExtensionDataObject ExtensionData { get; set; }
}
And that’s it. Now we can deserialize:
string xml = ...;
DataContractSerializer ser = new DataContractSerializer(typeof(Countries));
using (StringReader stringReader = new StringReader(xml))
{
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
Countries countries = (Countries)ser.ReadObject(xmlReader);
}
}
Alternatively, our result could be typed as a list of countries:
IList<Country> countries = (IList<Country>)ser.ReadObject(xmlReader);
Note that there is a limitation in the latter method in that deserializing XML attributes is not supported. Thus, an XML document like the following would not work:
<country iso-3166-alpha-2-code="AF">
<name>Afghanistan</name>
</country>
<country iso-3166-alpha-2-code="AX">
<name>Åland Islands</name>
</country>
<country iso-3166-alpha-2-code="AL">
<name>Albania</name>
</country>
</countries>
This will, however, work using the XmlSerializer.
1 Comment »
My experience is that Spring.NET configuration files tend to grow very large. As far as I can figure, there are two principal problems that arise from this:
- The configuration files get difficult to read and maintain
- It gets easier to introduce errors in the configuration because of its size
In general, I am in favour of keeping configuration files as small as possible. I often work with web applications that can (quite) easily be redeployed to the production environment, hence I always ask the question “will this value ever change between environments or deployments” when considering introducing a new configuration part.
Now, the Spring XML configuration usually serves two main purposes; to wire together the application, and to provide values that should be possible to change between deployments of the application or for different environments. The first purpose, I would argue does not necessarily need to be in the XML configuration. Rather, if this is done in code, we get the benefit that the compiler will tell us right away if there are typos or missing references. If this wiring is in the XML configuration file, such errors will not surface until the application starts.
So, the question that I had, was how Spring context wiring could be combined in code and in XML. I found one way of doing it, but it is only applicable to singleton objects.
Say, for instance that we have an object “something” that we wish to have configured in XML:
<object id="something" type="SpringTest.Something, SpringTest" singleton="false"/>
Then, we have a class that we want to initialize in code:
class Foo
{
public Foo() { }
private Something _s;
Something S
{
set { _s = value; }
get { return _s; }
}
}
Now, we see that Foo has a dependency on Something; it needs an instance of Something to be injected. We can use the Spring context to do this after we have created the instance of Foo:
IApplicationContext context = ContextRegistry.GetContext();
Foo f = new Foo();
context.ConfigureObject(f, "fooPrototype");
But Spring does not yet know that the Foo instance needs to be injected Something. Hence, we need to tell Spring that by creating what I would call a “prototype” or “template” object configuration:
<object id="fooPrototype" type="ContextTestProject.Foo, ContextTestProject">
<property name="S" ref="something"></property>
</object>
The final step is then to register our newly created object in the Spring context:
XmlApplicationContext xmlContext = context as XmlApplicationContext;
xmlContext.ObjectFactory.RegisterSingleton("foo", f);
After this, the Foo instance is available for the application in the Spring context.
No Comments »
Posted by: vidarkongsli in Software development, Software testing, tags: .net, automation, c#, http, internet explorer, internetexplorer, statuscode, watin, web test
One question have troubled me for some time when automating Internet Explorer (actually, I am doing web testing with Watin): how to test for HTTP status codes. Finally, I figured out how to do this. The lies in an event that the InternetExplorer object raises when navigation is unsuccessful.
I ended up with writing a C# helper class:
using System.Net;
using WatiN.Core;
using SHDocVw;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
namespace Test
{
public class NavigationObserver
{
private HttpStatusCode _statusCode;
public NavigationObserver(IE ie)
{
InternetExplorer internetExplorer = (InternetExplorer)ie.InternetExplorer;
internetExplorer.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(IeNavigateError);
}
public void ShouldHave(HttpStatusCode expectedStatusCode)
{
if (!_statusCode.Equals(expectedStatusCode))
{
Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Wrong status code. Expected {0}, but was {1}",
expectedStatusCode, _statusCode));
}
}
private void IeNavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
_statusCode = (HttpStatusCode)StatusCode;
}
}
}
Note that I use Visual Studio test runner to run my web tests. Then, I can use this in my test:
using (IE ie = new IE())
{
NavigationObserver observer = new NavigationObserver(ie);
ie.GoTo("http://some.where.com");
observer.ShouldHave(HttpStatusCode.NotFound);
}
1 Comment »
Jakub, one of the project members on my current project pointed me to this blog entry explaining a threading issue with Directory<K,V> in .NET: Hashtable and Dictionary thread safety considerations. I know that I earlier have written code exactly like the one in the blog entry, so this was a bit of an eye-opener.
No Comments »
For reasons previously unclear to me, I have not really felt comfortable with ADO.NET DataSets. With regards to topics like testability, object orientation, and encapsulation they always left a bitter taste in my mouth. Furthermore, I have not come across any really good use for them, which nourished my mistrust even more. (I am not saying that there aren’t any good uses, though). So, the other day I started to look deeper into the matter to try to find some more solid arguments.
The first clue I got from David Veeneman‘s article “ADO.NET for the Object-Oriented Programmer – Part One“, where he claims that “ADO.NET doesn’t work with object designs because it’s not supposed to work with objects!” and that the best way to use ADO.NET in an object-oriented design is not to use it. Basically, using ADO.NET “all the way” – including DataSets – will result in a data-driven application rather than an object-oriented application. But I want object-oriented…
In my application, I would like to have my data in business objects, and not in DataSets. Basic concept of encapsulation. I want to place data and operations on that data in my class, hiding the nitty-gritty details from the outside world. As Jeremy D. Miller points out, you cannot embed any real logic in a DataSet, and you have to be careful about duplication of logic. Another point he makes, which I think is very important, is that DataSets are clumsy to use inside an automated tests in terms of test setup. This is exactly the same as I have experienced. Easy testability is something you should look for in your application/library/technology/gizmo.
So, is there any time DataSet should be used? According to Scott Mitchell, (in his article ‘Why I Don’t Use DataSets in My ASP.NET Applications‘) Data sets should only be used in
- In a desktop, WinForms application
- For sending/receiving remote database information or for allowing communication between disparate platforms
Scott then goes on to conclude that he generally recommends using DataReaders in web applications rather than DataSets. As he points out, you might be tempted to use DataSets to cache data from the database, but argues that you’d probably be better off storing custom objects instead as it is more efficient, and removes the tight coupling to database tables.
Lo, the conclusion. If I were you, I would hesitate using DataSets for anything than small, data driven applications. Here’s why:
- Results in a non-object oriented application, which in turn hurts lowers cohesion, tightens coupling, and hurts encapsulation
- Testability suffers. DataSets are very awkward to test in unit tests. Forget about TDD.
- Tight coupling between database design and the rest of the applications. Makes change cumbersome.
- YAGNI – DataSets offer a lot of functionality that you probably don’t need. It boils down to design/develop-time efficiency vs. run-time efficiency (pointed out in More On Why I Don’t Use DataSets in My ASP.NET Applications (also by Scott Mitchell). Personally, I think that when the application grows larger, the design/develop-time efficiency is more or lest lost, and maintainability problems with DataSets set in.
No Comments »
I sometimes wonder how software certification test questions are created. In many occations, the questions seem like they are first and foremost made to show off new and fancy functionality in the framework. In other cases, the questions seem to have been thrown in there just to make the test harder to pass, rather than making sure the candidate has actual insight. Funny thing is that when trying the latter, the question may reveal really poor API design. Take, for instance the following example.
You are a Web application developer for a company. Which of the following would you use to define that column2 has a foregin key relationship with column1:
-
DataColumn column2 = DataSet1.Tables["Orders"].Columns["CustID"];
DataColumn column1 = DataSet1.Tables["Customers"].Columns["CustID"];
DataSet1.Relations.Add(relCustOrder);
DataRelation relCustOrder = new DataRelation("CustomersOrders", column1, column2);
-
DataColumn column1 = DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn column2 = DataSet1.Tables["Orders"].Columns["CustID"];
DataRelation relCustOrder = new DataRelation("CustomersOrders", column2, column1);
DataSet1.Relations.Add(relCustOrder);
So, the difficulty here lies in remembering the correct parameter order for the DataRelation constructor. First of all, the question of remembering this aspect of the API is not a question of insight. Secondly, when considering the user-friendlyness of the API, this reveals a rediciously poor design. Why? If I, for instance, were to review the code above, the code gives me no clue about the direction of the relationship. Which is the primary key column, and which is the foreign key column? Either I would have to remember the constructor parameter order, look at the database design, or look at some other documentation. Preferably, the code should be able to tell me. Consider the following (fantasy) code:
DataColumn column1 = DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn column2 = DataSet1.Tables["Orders"].Columns["CustID"];
DataRelation relCustOrder = column2.isForeginKeyFor(column1);
DataSet1.Relations.Add(relCustOrder);
Much better. Now, the DataColum class has an instance method I can use to define the relationship in a manner that is both much easier to rembember, and easier to read. But still, this has potential for improvement. Why do I need the last line of code? Columns column1 and column2 belong to a DataTable, and that DataTable belong to the DataSet, then the isForeginKeyFor method should be able to propagate the new relation to the DataSet on its own, like so:
ParentRelations.Add(theNewRelation);
Then, we would get rid of an extra line of boilerplate code, and the code snippet would be like so:
DataColumn column1 = DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn column2 = DataSet1.Tables["Orders"].Columns["CustID"];
column2.isForeginKeyFor(column1);
No Comments »
|