Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 35
  • Trackbacks - 76

 

Value Object and its collection

Here's a nifty way to build a Value Object for consumption across your app and then have access to each type as a collection.

First the test:

        [Test]

        public void CanLoadStreetTypesList()

        {

            IList<StreetTypes> types = StreetTypes.ALL;

            foreach(StreetTypes type in types)

            {

                Console.Out.WriteLine(type.Name + " - " + type.Abbreviation);

            }

            Assert.IsTrue(types.Count>0);

        }

So basically I want a list of all the types of a StreetTYpes Value object.

Here's my StreetTypes VO :

    public sealed class StreetTypes

    {

        private string _name;

        private string _abbreviation;

 

        public string Name

        {

            get { return _name; }

        }

 

        public string Abbreviation

        {

            get { return _abbreviation; }

        }

 

        private StreetTypes()

        {

        }

        private StreetTypes(string name, string abbreviation)

        {

            _name = name;

            _abbreviation = abbreviation;

        }

        private static IList<T> All<T>(T obj)

        {

            IList<T> types = new List<T>();

            FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public);

            foreach (FieldInfo field in fields)

            {

                if (field.FieldType.Equals(typeof(T)))

                {

                    types.Add((T)field.GetValue(obj));

                }

            }

 

            return types;

        }

 

        public static readonly StreetTypes ACCESS_ROAD = new StreetTypes("Access Road", "AcRd");

        public static readonly StreetTypes ALLEY = new StreetTypes("Alley", "Aly");

        public static readonly StreetTypes AVENUE = new StreetTypes("Avenue", "Ave");

        public static readonly StreetTypes BOULEVARD = new StreetTypes("Boulevard", "Blvd");

        public static readonly StreetTypes BRIDGE = new StreetTypes("Bridge", "Brg");

        public static readonly StreetTypes CIRCLE = new StreetTypes("Circle", "Cir");

        public static readonly StreetTypes COURT = new StreetTypes("Court", "Ct");

        public static readonly StreetTypes CUTOFF = new StreetTypes("Cutoff", "Cto");

        public static readonly StreetTypes DRIVE = new StreetTypes("Drive", "Dr");

        public static readonly StreetTypes EXPRESSWAY = new StreetTypes("Expressway", "Expy");

        public static readonly StreetTypes EXTENSION = new StreetTypes("Extension", "Ext");

        public static readonly StreetTypes FREEWAY = new StreetTypes("Freeway", "Fwy");

        public static readonly StreetTypes HIGHWAY = new StreetTypes("Highway", "Hwy");

        public static readonly StreetTypes KEY = new StreetTypes("Key", "Ky");

        public static readonly StreetTypes LANE = new StreetTypes("Lane", "Ln");

        public static readonly StreetTypes LOOP = new StreetTypes("Loop", "Loop");

        public static readonly StreetTypes OVERPASS = new StreetTypes("Overpass", "Opas");

        public static readonly StreetTypes PARK = new StreetTypes("Park", "Park");

        public static readonly StreetTypes PARKWAY = new StreetTypes("Parkway", "Pkwy");

        public static readonly StreetTypes RAMP = new StreetTypes("Ramp", "Ramp");

        public static readonly StreetTypes ROAD = new StreetTypes("Road", "Rd");

        public static readonly StreetTypes RISE = new StreetTypes("Rise", "Rise");

        public static readonly StreetTypes STREET = new StreetTypes("Street", "St");

        public static readonly StreetTypes TERRACE = new StreetTypes("Terrace", "Ter");

        public static readonly StreetTypes TRAIL = new StreetTypes("Trail", "Trl");

        public static readonly StreetTypes TUNNEL = new StreetTypes("Tunnel", "Tunl");

        public static readonly StreetTypes WALK = new StreetTypes("Walk", "Walk");

        public static readonly StreetTypes WAY = new StreetTypes("Way", "Way");

        public static IList<StreetTypes> ALL

        {

            get

            {

                return All<StreetTypes>(new StreetTypes());

            }

        }

 

 

    }



The ALL property returns a collection that uses reflection in the embedded All(T obj) method. This All method is great because I can use it to reflect on any similar Value Object that wants to spit out a collection of all iits static type/fields. Now when I add a new streettype (since that happens so much), the collection will 'reflect' the changes without relying on me adding it to the collection.


Feedback

# re: Value Object and its collection

Gravatar Mike,
Very slick. My only improvements would be:

// Make Street Types as immutable as possible
private readonly string _name;
private readonly string _abbreviation;

// Cache All
private static IList<StreetTypes> _all;

public static IList<StreetTypes> All
{
get
{
if( _all == null )
//...Only reflect the first time
return _all;
}
}

// Override equals, gethashcode, ==, !=

and move All<T> to a static Utility class.

-Bill 8/14/2006 4:40 AM | Bill Pierce

# re: Value Object and its collection

Gravatar Thanks Bill...I got a similar tip from someone else and implemented...I appreciate the feedback! 8/14/2006 5:11 AM | Mike

Post a comment





 

Please add 4 and 1 and type the answer here:

 

 

Copyright © Mike Nichols