NetInverse Developers Blog

January 5, 2010
Category: .Net — Tags: , , , , , — admin @ 9:59 pm

If you use WCF to create web services, you need to define your data contracts by using DataContract/DataMember attributes.
Actually you can use them directly for serialization/deserialization in your code. Following is a sample of how to use DataContract/DataMember attributes for fast object Xml serialization and deserialization.

using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;

namespace DataContractSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student()
            {
                Age = 12,
                Name = "David"
            };

            DataContractSerializer dcs = new DataContractSerializer(typeof(Student));
            StringBuilder sb = new StringBuilder();

            using(XmlWriter writer = XmlWriter.Create(sb))
            {
                dcs.WriteObject(writer, student);
            }
            string xml = sb.ToString();

            using (XmlTextReader reader = new XmlTextReader(new StringReader(xml)))
            {
                Student newStudent = (Student) dcs.ReadObject(reader, true);
            }
        }
    }

    [DataContract(Namespace="http://www.netinverse.com")]
    public class Student
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int Age { get; set; }
    }
}
June 20, 2009
Category: .Net, Agile — Tags: , , — admin @ 12:11 am

Pragmatic programmers use feedback to drive their development and personal processes. The most valuable feedback you can get while coding comes from unit testing. Now in it’s second edition, Pragmatic Unit Testing in C# with NUnit, 2nd Ed. will show you how to do software unit testing, of course, but more importantly will show you what to test.

About this book

New for the Second Edition:

  • Updated for NUnit 2.4 (C#, .NET 2.0, Visual Studio 2005, and Mono)
  • More NUnit assert methods
  • New String and Collection assertion support
  • Better support for multiple-platform development (Mono and .NET)
  • Higher-level setup and teardown fixtures
  • …and more!

Without good tests in place, coding can become a frustrating game of “whack-a-mole.” That’s the carnival game where the player strikes at a mechanical mole; it retreats and another mole pops up on the opposite side of the field. The moles pop up and down so fast that you end up flailing your mallet helplessly as the moles continue to pop up where you least expect them. You need automated testing and regression testing to keep the moles from popping up.

You don’t test a bridge by driving a single car over it right down the middle lane on a clear, calm day. Yet many programmers approach testing that same way—one pass right down the middle and they call it “tested.” Pragmatic programmers can do better than that!

With this book, you will:

  • Write better code, faster
  • Discover the best hiding places where C# bugs breed
  • Learn how to think of all the things that could go wrong
  • Test pieces of code without using the whole .NET project
  • Use NUnit to simplify your C# test code
  • Test effectively with the whole team

Real software unit testing will make your life easier. It will make your software design and architecture better and drastically reduce the amount of time you spend debugging you .NET code.

June 18, 2009
Category: .Net — Tags: , — admin @ 11:21 pm

Igor Ostrovsky’s LINQ Tips:

Check out Igor’s site.

April 4, 2009
Category: .Net — Tags: , , , , — admin @ 5:23 pm

Following utility function EnumToArray takes an enum type and returns an array populated with each enum item. You can tweak the code a little bit to make the function to be EnumToList easily.

The sample code also demonstrates how to use Enumerable.Except to get the delta of two arrays of same type T.

using System;
using System.Collections.Generic;
using System.Linq;

namespace IEnumerableTest
{
    public static class Utils
    {
        public static T[] EnumToArray<T>()
        {
            Type enumType = typeof(T);
            if (enumType.BaseType != typeof(Enum))
            {
                throw new ArgumentException("T must be a System.Enum");
            }
            return (Enum.GetValues(enumType) as IEnumerable<T>).ToArray();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Convert Enum type to an array
            RoleType[] allRoles = Utils.EnumToArray();

            //Use IEnumerable.Except to get part of the array.
            RoleType[] localUserRoles = new RoleType[] { RoleType.LocalAdmin, RoleType.LocalUser, RoleType.Guest };

            RoleType[] domainUserRoles = allRoles.Except(localUserRoles).ToArray();
        }

        enum RoleType
        {
            DomainAdmin,
            LocalAdmin,
            DomainUser,
            LocalUser,
            Guest
        }
    }
}

©2009 NetInverse. All rights reserved. Powered by WordPress