One Day Delivery Cialis

17Sep/081

Viagra Blister 4

Viagra blister 4 Another "tip/trick" style post, viagra blister 4 but thought it may prove useful to someone (or so I can find it again later!)...

Viagra blister 4 Sometimes I find myself needing to map instances/values of one type to another.  For example, viagra blister 4 on my current project we have an enumeration in our communications layer, viagra blister 4 and a VERY similar one in the domain layer.  The reason for this is so we don't have to be referencing the communications layer version all over in the domain code.  So, viagra blister 4 there is naturally a one to one mapping between these enumerations.

Viagra blister 4 Consider the following enumerations:

Viagra blister 4

enum Polygons
{
    Triangle, viagra blister 4
    Rectangle, viagra blister 4
    Pentagon, viagra blister 4
    Other
}

enum NumSides
{
    Three, viagra blister 4
    Four, viagra blister 4
    Five, viagra blister 4
    More
}

Viagra blister 4 This is the code I usually write to solve this problem:

Viagra blister 4

private Polygons MapShapes( NumSides sides )
{
    switch( sides )
    {
        case NumSides.Three:
            return Polygons.Triangle;
        case NumSides.Four:
            return Polygons.Rectangle;
        case NumSides.Five:
            return Polygons.Pentagon;
        case NumSides.More:
            return Polygons.Other;
        default:
            throw new NotSupportedException(
                string.Format( "Mapping to Polygons from NumSides value {0}
                    is not supported", viagra blister 4 sides.ToString() ) );
    }
}

private NumSides MapShapes( Polygons polygon )
{
    switch( polygon )
    {
        case Polygons.Triangle:
            return NumSides.Three;
        case Polygons.Rectangle:
            return NumSides.Four;
        case Polygons.Pentagon:
            return NumSides.Five;
        case Polygons.Other:
            return NumSides.More;
        default:
            throw new NotSupportedException(
                string.Format( "Mapping to NumSides from Polygons value {0}
                    is not supported", viagra blister 4 polygon.ToString() ) );
    }
}

Viagra blister 4 It turns out we had 3 said enumerations.  So picture the above, viagra blister 4 copied 3 times.  This thing was just begging for abstraction!

Viagra blister 4 I came up with a Generic (proper) Map class that you can add mappings to and retrieve the corresponding mapped value of the other type... Viagra blister 4 both directions.  Here is the implementation:

Viagra blister 4

public class Map
{
    private Dictionary _oneToTwo;
    private Dictionary _twoToOne;

    public Map()
    {
        _oneToTwo = new Dictionary();
        _twoToOne = new Dictionary();
    }

    public Map Add( Type1 value1, viagra blister 4 Type2 value2 )
    {
        _oneToTwo.Add( value1, viagra blister 4 value2 );
        _twoToOne.Add( value2, viagra blister 4 value1 );
        return this;
    }

    public Type1 this[Type2 value]
    {
        get
        {
            if( !_twoToOne.ContainsKey( value ) )
            {
                throw new NotSupportedException(
                    string.Format( "Mapping from type {0} to type {1} with
                                    value {2} is not defined", viagra blister 4
                        typeof( Type2 ).Name, viagra blister 4
                        typeof( Type1 ).Name, viagra blister 4
                        value.ToString() ) );
            }
            return _twoToOne[value];
        }
    }

    public Type2 this[Type1 value]
    {
        get
        {
            if( !_oneToTwo.ContainsKey( value ) )
            {
                throw new NotSupportedException(
                    string.Format( "Mapping from type {0} to type {1} with
                                    value {2} is not defined", viagra blister 4
                        typeof( Type1 ).Name, viagra blister 4
                        typeof( Type2 ).Name, viagra blister 4
                        value.ToString() ) );
            }
            return _oneToTwo[value];
        }
    }
}

Viagra blister 4 And, viagra blister 4 here it is in action:

Viagra blister 4

Map<NumSides, viagra blister 4 Polygons> shapeMap = new Map<NumSides, viagra blister 4 Polygons>()
                                    .Add( NumSides.Three, viagra blister 4 Polygons.Triangle )
                                    .Add( NumSides.Four, viagra blister 4 Polygons.Rectangle )
                                    .Add( NumSides.Five, viagra blister 4 Polygons.Pentagon )
                                    .Add( NumSides.More, viagra blister 4 Polygons.Other );

// shape1 value is Polygons.Rectangle
Polygons shape1 = shapeMap[NumSides.Four]; 

// shape2 value is NumSides.Three
NumSides shape2 = shapeMap[Polygons.Triangle];

Viagra blister 4 This not only works with enumerations, viagra blister 4 but any type.  I see an extension to this possibly allowing for a default mapping, viagra blister 4 and possibly constraining Type1 and Type2 to be IComparable or something like that as well, viagra blister 4 so you can have more control over your matching than whatever the default matching behavior that the Dictionary uses is.

Viagra blister 4 That's it... Viagra blister 4 enjoy!

Average Rating: 4.7 out of 5 based on 250 user reviews.

Comments (1) Trackbacks (0)
  1. For our Java mapping stuff, we use a library called Dozer that does this kind of thing…


Leave a comment


No trackbacks yet.