Source

MongoDB.Emitter / MongoDB.Emitter / WrapperFactory_DefaultConversionDelegates.cs

Full commit
/*
MongoDB.Emitter:  Emits strongly-typed wrappers for MongoDB.Driver.Document objects
http://objectcloud.com
(C) 2010 Andrew Rondeau
MIT License  (Open Source)
*/

using System;
using System.Collections.Generic;
using System.Text;

using MongoDB.Driver;

namespace MongoDB.Emitter
{
    // Default conversion delegates are kept here for ease of integration
    public partial class WrapperFactory
    {
        private void RegisterDefaultConversionDelegates()
        {
            // Feel free to add and submit conversion delegates that "make sense" to build into the emitter
            // Typically, these should be things like primitives (long, double,) strings, and simple structures
            // like DateTime and Guid.  Complicated types, like images, are best kept as optional packages.
            // Don't forget that some structures and primitives will need both regular and nullable versions.
            // See long and long?

            RegisterConversionDelegates<long>(
                delegate(object me, object to)
                {
                    return Convert.ToInt64(to);
                },
                delegate(object me, long from)
                {
                    return from;
                });

            RegisterConversionDelegates<long?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return Convert.ToInt64(to);
                },
                delegate(object me, long? from)
                {
                    if (null == from)
                        return null;

                    return from.Value;
                });

            RegisterConversionDelegates<int>(
                delegate(object me, object to)
                {
                    return Convert.ToInt32(to);
                },
                delegate(object me, int from)
                {
                    return from;
                });

            RegisterConversionDelegates<int?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return Convert.ToInt32(to);
                },
                delegate(object me, int? from)
                {
                    if (null == from)
                        return null;

                    return from.Value;
                });

            RegisterConversionDelegates<double>(
                delegate(object me, object to)
                {
                    return Convert.ToDouble(to);
                },
                delegate(object me, double from)
                {
                    return from;
                });

            RegisterConversionDelegates<double?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return Convert.ToDouble(to);
                },
                delegate(object me, double? from)
                {
                    if (null == from)
                        return null;

                    return from.Value;
                });

            RegisterConversionDelegates<bool>(
                delegate(object me, object to)
                {
                    return Convert.ToBoolean(to);
                },
                delegate(object me, bool from)
                {
                    return from;
                });

            RegisterConversionDelegates<bool?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return Convert.ToBoolean(to);
                },
                delegate(object me, bool? from)
                {
                    if (null == from)
                        return null;

                    return from.Value;
                });

            RegisterConversionDelegates<DateTime>(
                delegate(object me, object to)
                {
                    return Convert.ToDateTime(to);
                },
                delegate(object me, DateTime from)
                {
                    if (DateTimeKind.Utc != from.Kind)
                        throw new ArgumentException("Mongo DateTimes must always be UTC, use .ToUniversalTime() to convert");

                    return from;
                });

            RegisterConversionDelegates<DateTime?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return Convert.ToDateTime(to);
                },
                delegate(object me, DateTime? from)
                {
                    if (null == from)
                        return null;

                    if (DateTimeKind.Utc != from.Value.Kind)
                        throw new ArgumentException("Mongo DateTimes must always be UTC, use .ToUniversalTime() to convert");

                    return from.Value;
                });

            RegisterConversionDelegates<TimeSpan>(
                delegate(object me, object to)
                {
                    return TimeSpan.FromMilliseconds(Convert.ToDouble(to));
                },
                delegate(object me, TimeSpan from)
                {
                    return from.TotalMilliseconds;
                });

            RegisterConversionDelegates<TimeSpan?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return TimeSpan.FromMilliseconds(Convert.ToDouble(to));
                },
                delegate(object me, TimeSpan? from)
                {
                    if (null == from)
                        return null;

                    return from.Value.TotalMilliseconds;
                });

            RegisterConversionDelegates<string>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return to.ToString();
                },
                delegate(object me, string from)
                {
                    return from;
                });

            RegisterConversionDelegates<Binary>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return (Binary)to;
                },
                delegate(object me, Binary from)
                {
                    return from;
                });

            RegisterConversionDelegates<byte[]>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return ((Binary)to).Bytes;
                },
                delegate(object me, byte[] from)
                {
                    return new Binary(from);
                });

            /*RegisterConversionDelegates<Guid>(
                delegate(object me, object to)
                {
                    Binary binary = (Binary)to;
                    return new Guid(binary.Bytes);
                },
                delegate(object me, Guid from)
                {
                    Binary toReturn = new Binary(from.ToByteArray());
                    toReturn.Subtype = Binary.TypeCode.Uuid;

                    return toReturn;
                });

            RegisterConversionDelegates<Guid?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    Binary binary = (Binary)to;
                    return new Guid(binary.Bytes);
                },
                delegate(object me, Guid? from)
                {
                    if (null == from)
                        return null;

                    Binary toReturn = new Binary(from.Value.ToByteArray());
                    toReturn.Subtype = Binary.TypeCode.Uuid;

                    return toReturn;
                });*/

            RegisterConversionDelegates<Guid>(
                delegate(object me, object to)
                {
                    return (Guid)to;
                },
                delegate(object me, Guid from)
                {
                    return from;
                });

            RegisterConversionDelegates<Guid?>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;

                    return (Guid)to;
                },
                delegate(object me, Guid? from)
                {
                    if (null == from)
                        return null;

                    return from.Value;
                });

            RegisterConversionDelegates<Document>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;
                    else
                        return (Document)to;
                },
                delegate(object me, Document from)
                {
                    return from;
                });

            RegisterConversionDelegates<Oid>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;
                    else
                        return (Oid)to;
                },
                delegate(object me, Oid from)
                {
                    return from;
                });

            RegisterConversionDelegates<DBRef>(
                delegate(object me, object to)
                {
                    if (null == to)
                        return null;
                    else
                        return (DBRef)to;
                },
                delegate(object me, DBRef from)
                {
                    return from;
                });

            RegisterConversionDelegates<object>(
                delegate(object me, object to)
                {
                    return to;
                },
                delegate(object me, object from)
                {
                    return from;
                });
        }
    }
}