Archive for the ‘Application Development’ Category

C# and streams

C# uses a lot of streams, or at least it seems so in some of the code examples I have looked at.
Having said that I felt the need to read up on the Stream class. The stream class is an abstract class and an abstract acts as a type of base class from which other classes can inherit. And just to recap, abstract methods do not have a method body and must be implemented by the class inheriting from it. A method marked as virtual can be overridden but it does not have to be implemented. Methods that are marked as abstract can be overriden.

So the stream class is an abstract base class for a set of other classes that do various operations such as reading content from files, reading files in a directory, etc. Each of the classes that inherit from the base stream class can be seen as a type of stream. Streams can be read from, written to and they support seeking.

The classes that inherit from the Stream class can be divided into three broad categories:

  • Classes that are used for File I/O
  • Classes that are used for reading and writing to streams
  • Common IO stream classes

The reason for taking a look at these classes is because I was looking at a PowerPoint presentation recently. More specifically I was looking at PowerPoint 2007 (pptx) and its underlying XML. If you save a Powerpoint 2007 as XML and you look at the source you will note that there are sections called pkg:binaryData. It got me wondering that maybe that same binary data can be processed in C# code. Take a look at the following sample C# code:

            Image img = Image.FromFile(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            byte[] myByte = File.ReadAllBytes(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            Console.WriteLine(myByte.Length);
            MemoryStream stream = new MemoryStream(myByte);
            int lengthOfByte = Convert.ToInt32(stream.Length);
            byte[] mySecondByte = ReadFully(stream, lengthOfByte);
            Console.WriteLine(mySecondByte.Length);
            Image img2 = Image.FromStream(stream);
            img2.Save(@"C:\Users\fredrike\Documents\LEGAL SUITE3.bmp");

Notice a couple of things I have done in the code. I created a Image class instance and read it from an existing bitmap image. I then create a byte array and used the static ReadAllBytes method supplied by the File class. I then just simply wrote the length of the file to the screen because in the next couple of lines I create a MemoryStream instance using the byte array, after which I also get the length property of the MemoryStream instance because I use it in a function called ReadFully:

        public static byte[] ReadFully(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

I got this from Jon Skeet’s website. Be sure to check out his other C# articles. In my code I then created a second byte array which is generated by the ReadFully method. Once the bytes have finished reading I created a second Image instance, img2 and pass it the MemoryStream instance, and I then call img2’s Save method and create a second image based on the first one.

So my question is, can the binary data from Powerpoint presentations be used in the same way?

  • Share/Bookmark

Some thoughts on Windows Forms Development

Yesterday I posted about Making static content more dynamic. I actually started developing a small C# windows forms application in combination with a MS Access database. The windows forms application will, for now, generate all the HTML needed for the website and the MS Access database will be used to centralise information. This is not the most modern or flashiest approaches but it does lend itself to saving some money and if the marketing strategy is good and accurate might even deliver good results. Obviously as soon as the customer changes hosting plans the need for static HTML content might go out the window altogether. You can still, however, incorporate the same Windows forms application, through WCF services.

Windows forms seemed so strange, because the domain is so different to web-based apps, seeing that I havent done any in a very long time. But the C# code remains very much the same, which is awesome.

  • Share/Bookmark

Making static content more dynamic

I was helping some with their website yesterday and the hosting package they have does not allow for dynamic languages, such as PHP or Python, and there is no database either. So basically you have a situation where you can only upload static HTML pages, which is limiting yes, but not such an entirely bad thing. Managing the content for such a website only has two or three realistic options:

  • Use IDE such as Dreamweaver, which also implies more costs
  • Manually edit the HTML pages yourself, which can become nightmarish
  • Create some managed (C#) code that can automate HTML generation

All three approaches involve content being uploaded, which is not the worst thing ever, but it does mean that updates do not occur immediately. Also remember that a budget constraint is the biggest factor here. The idea is that you do not incur any additional monthly costs. I also think you can build content updates to social networks as well.

  • Share/Bookmark

Linq to XML and DataSet to XML

On Monday I started working through some LINQ to XML and it got me so excited that I misunderstood some project requirements that ended up being quite wrong. It did not, however, diminish my enthusiasm. I ended up using a small part of the code in the end anyway, even though it was not in a production environment.

Lets say you have a collection of XML documents and you would like to know what elements appear in all of them, so at the end of it all you have a single of list of elements that appear between all of them. This is how I did it:

            DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["XMLDirectory"].ToString());
            Dictionary values = new Dictionary();
            FileInfo[] fi = di.GetFiles();
            Response.Write("
");
            Response.Write("
");
            foreach (FileInfo file in fi)
            {
                //Response.Write(@"


");
            }
            Response.Write("

");
            Response.Write("
");
            Response.Write(@"


");
            Response.Write("

");
            Response.Write("
"); //Response.Write("" + file.Name + "
"); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; settings.ConformanceLevel = ConformanceLevel.Fragment; //Console.WriteLine(file.Name); XmlReader reader = XmlReader.Create(file.Directory + @"\" + file.Name, settings); XDocument document = XDocument.Load(reader); IEnumerable elems = from items in document.Descendants("Bond") select items; foreach (XElement elem in elems) { IEnumerable childElems = from items in elem.Descendants() select items; foreach (XElement xelem in childElems) { //Response.Write(xelem.Name.ToString() + " " + xelem.Parent.Name.ToString() + "
"); if (!values.ContainsKey(xelem.Name.ToString())) { values.Add(xelem.Name.ToString(), xelem.Parent.Name.ToString()); } } } //Response.Write("
"); Response.Write("Combined"); var combined = values .OrderBy(kv => kv.Value); foreach (KeyValuePair kvp in combined) { Response.Write(kvp.Key + " " + kvp.Value + ""); } Response.Write("
");

I use the DirectoryInfo class on a directory, XMLPath in this instance. I then declare an array of type FileInfo, which I then use in a foreach (Enumerate) loop using the FileInfo class. I then use the XMLReader class to read the XML document that is represented through the FileInfo class to read the XML. Inside the foreach loop I create an enumerable instance of XElement called elems which uses LINQ to XML to select all the items. I then create a second enumerable instance of XElements called childElems. I then iterate (enumerate) through the childElems instance and add the Name of the element as the key and the parent as a value to a dictionary of type string,string which I declared earlier. This will loop through all files in the directory and generate a dictionary with combined values. Once its in a dictionary you can simply enumerate through it. I ordered it first by using the OrderBy extension method. Once it has been ordered I enumerate through it with a KeyValuePair and display the combined list. This approach has apparent weaknesses in that it uses the name of a node as a key and the parent as the value. What if two nodes are equal but have different parents? It served a purpose for me though.

The second piece of code I would like to show is an example of converting a dataset to xml and returning the raw XML. Let’s say you have an arbitrary function and you want to return the raw XML of a dataset, you could use:

            StringWriter writer = new StringWriter();
            transactionDS.WriteXml(writer);
            xmlizedString = writer.ToString();

            return xmlizedString;
  • Share/Bookmark

More code vs less code vs readability vs efficiency

After my blog post yesterday on File.ReadAllLines() vs StreamReader I have been thinking that more code does not necessarily mean more, or does it? I mean syntactically the first one is a one liner and the second one is a few lines, but I do feel that the latter adds more value. Why? Well firstly because streams seem to be an important part and concept in the .NET framework and secondly the streamreader class seems to dispose of resources a bit better. It also seems as if my implementation of the streamreader class deals with something the first option has still to deal with. I mean you have to assign a string array and then somewhere you have to iterate through the content of the array, whereas with the streamreader you can do all of that once. Looping through the contents that gets read by the streamreader class instance can be achieved by either appending the results to a stringbuilder instance or by using a function that implements the IEnumerable interface and uses the yield keyword. Using a function that implements IEnumerable means that you can run a foreach against it. I do honestly find this approach a lot more expressive. Its also interesting to note that the streamreader class performs better in some cases.

  • Share/Bookmark

Do we spend enough time designing?

In my current work environment it seems as if I spend way more time pushing out code than spending time making sure the quality of the code is good. Quality in this context means that you actually spend some time before you code planning the structure of the program you are about to create. In its simplest terms this is simply penciling some ideas down and drawing a few basic flowcharts. Doing this enables you to see things unfold a bit clearer, because I have found myself in a situation where I started coding and I got to a stage where the logic of the code went sour and it meant recoding everything or parts of it. With a bit of planning and design beforehand this can be avoided.

On a deeper, more complicated level, detailed specification building that incorporates requirements definitions and the full SDLC type-of-thing, obviously provides a different approach. But I think when you get to a deep and complex level you are addressing a big group of developers. Or can the same principles be applied to smaller projects?

I do find that I draw a lot of contextual diagrams in my current work, because I like having a contextual overview of what I am doing and what the system I am busy with attempts to achieve. A contextual diagram is usually a very basic diagram that just shows the system on a high level, with the idea being that you drill down to deeper detail from there. I remember using an analysis methodology called SSADM (Structured System Analysis and Design Methodology) and remember it as being very, very structured. You would start your diagram at the contextual level and drill down until the very lowest level. I do find OOAD (Object Oriented Analysis and Design) easier in a sense though, because you can directly translate objects in the real world to objects in the system. But I still draw contextual diagrams for OOAD.

The point though is that planning a piece of code is as important as the code itself and is a discipline.

  • Share/Bookmark

File.ReadAllLines() vs StreamReader

A couple of days ago I forced myself to memorize the C# code for reading from a file (text, html), and for it I used the StreamReader class (on MSDN):

try
{
    using(StreamReader reader = new StreamReader('filename'))
    {
            string s;
            while((s = reader.ReadLine()) != null)
            {
                   Console.WriteLine(s);
            }
     }
}
catch(Exception ex)
{
      Console.WriteLine(ex.Message);
}

This is in comparison to the File class which has a method called ReadAllLines, and its biggest benefit is that it results in less code. Is less more? I’m not sure yet. With the StreamReader class you can create a method that implements IEnumerable which allows you to use the foreach on it. I found the sample for this on http://stackoverflow.com/questions/286533/filestream-streamreader-problem-in-c.

public static IEnumerable ReadLines(string path)
    {
        using (StreamReader reader = File.OpenText(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }

foreach(string line in ReadLines(file))
{
    Console.WriteLine(line);
}

It is also mentioned that the StreamReader approach works nicely with Linq. Another thing that was mentioned is that the StreamReader uses a concept called lazy evaluation and this increases performance.

Does the less code option result in better performance just because it uses less lines of code? I’m a bit undecided. Maybe I will have a better opinion in the future.

  • Share/Bookmark

Is JavaScript object oriented?

When I started my career about 5 or so years ago I didnt know much about JavaScript, but I knew something about object oriented analysis and design. OOAD in this case was the classical approach to OO where an object is an instance, which means that each thing or object that you create in a system must first be instantiated, and this is something that does not happen in JavaScript at all really. Everything in JavaScript is object-based, there is no class instantiating as in the classical OO approach. The other thing that is quite notable is that it takes a bit of a work around to create some sort of inheritance in JavaScript, its not something that exists in the language itself. Similarly a thing like polymorphism is not a language feature as it is in say C#. With all that being said though, it does not mean that these features cannot be created in JavaScript, it just means JavaScript kinda works better without them.

It’s amazing then to hear an experienced JavaScript developer then speak of JavaScript being object oriented because it supports basic dot notation, even though the language itself is not object oriented in the classical sense.

  • Share/Bookmark

Concept, CV Updater

One of the things that I see a lot on websites of other developers is a resume or CV section. It seems only logical that a developer would want to show off their pedigree, but I’m guessing a lot of developers would not mind some extra work (and extra cash). Creating a resume for your website is relatively straight forward, just create it! You just create some HTML page and add the information and its done, or is it? Well I found myself thinking in the shower (weird place to get an idea) and I thought that it would be really useful to write some code that made the maintenance of a resume or CV easy. Useful code does not have to be unnecessarily complex, because it achieves a certain function or purpose.

I consider maintaining my CV easy when I can edit the content of my CV and export the CV in as many formats possible. So basically I want my CV stored in one place where I can update it on a continuous basis, and when needed download it in Word, for instance. I’m sure that what I am doing has probably been done many times before, but I guess part of being a developer gives you the freedom to create your own solutions. And something I think developers should always do is use their own IP (intellectual property) to create solutions that they themselves use.

So for the CV solution I am thinking of using a very basic XML document with a very basic structure of sections and subsections. Think about it, a CV or resume is made up of sections and subsections. For instance I have a personal information section with subsections for my name, address, etc. Thus you can derive a quick schema:




  
    
      
    
  


Once the CV is in XML I can pretty much do with it what I want both in terms of modifying its content and exporting it. I actually started this idea by typing it out using Inkscape, simply because I dont have Word installed on my desktop machine, but also because it allows me to draw diagrams and go mad. I started off by defining some generic XML structure (without using XSD):



    
          
                CVSubsection Value
          
    

As you can see from the code that a CV could be made up of multiple sections and that each section could have multiple subsections. If you relate this structure to a more practical example:



    
          
                Fredrik
          
          
                Erasmus
          
    
    
           
                BSc Computer studies
           
    

It fits into the structure quite nicely. Each section will only have a single attribute called “name”, and each subsection will also only have a single attribute called “name”, but the subsection will have a node value, whereas the section name will not. The question begs though, what if you want to say where you got your qualification from? How would you structure that? Well you could add a custom attribute to the section called “institution”, which would change your xml:

    
           
                BSc Computer studies
           
    

The basic operations for the sections and subsections would mostly involve adding,editing and removing items. I think Linq to XML would perform this quite well.

  • Share/Bookmark

Building a case for ORM

Okay, so I have been guilty in the past for not implementing any form of ORM (Object Relational Model) for some of the projects I have written. The main reason for this is/was that the projects I have worked on have never really used them. But the more I code the more I realise I sometimes find myself re-coding the same stuff. I mean my understanding of an ORM tool is that it creates a set or sets of classes that map to one or more databases. But why is this beneficial and why does this improve my life as a developer. I started out by reading this article on Wikipedia, which is not as detailed as I had hoped, but I guess its a starting point, because it lead me to this article. I also found this article and watched the video and from it got to understand that for the relational world to co-exist with the OO world several things need to be considered:

  • Inheritance: In the OO world a User object might serve as a base class for users of type teacher or student. How do you map this relationship in the relational database?
  • Granularity: This happens if the classes do not match up to the tables in the database. You might for example have a situation where a User is associated to a country. In your database you might represent this as two tables, but in your class you might represent the country as a property or field of the User class. So there exists a mismatch. How do you map this situation?
  • Identity and Equality: Lets say you query a database for a User with ID 100. The result you get back will always point to user with ID 100. If you look at it from an OO perspective, will a User with ID point to the same object, or multiple instances of it?
  • Association (directionality): In an OO model you associate classes with each other by placing references in each class of the other. In a relational model you create the associations by placing a foreign key in a table.
  • Type systems: The types in a database are quite different from those in an OO environment. For instance a VARCHAR with a size of 100 is not the same as a string, because a string is dependant on the amount of memory available.

I then read two StackOverflow topics, one here and one here. From what I could gather ORM’s are not great at performing bulk operations, but what they lack in that area they make up for in developer productivity.

I remember coming across an article that compared the performance between various ORM mappers on this website.

  • Share/Bookmark
Get Adobe Flash playerPlugin by wpburn.com wordpress themes