Esempio n. 1
0
        public void Mongo_Configuration_Supports_Lambda_Syntax_Registration()
        {
            MongoConfiguration.Initialize(r => r.For <User>(u => u.ForProperty(user => user.FirstName).UseAlias("first")));
            var alias = MongoConfiguration.GetPropertyAlias(typeof(User), "FirstName");

            Assert.Equal("first", alias);
        }
        private static string[] GetDeepAlias(Type type, string[] graph)
        {
            var graphParts  = new string[graph.Length];
            var typeToQuery = type;

            for (var i = 0; i < graph.Length; i++)
            {
                if (graph[i].EndsWith("|Ind"))
                {
                    graphParts[i] = graph[i];
                    continue;
                }

                var property = BSON.ReflectionHelper.FindProperty(typeToQuery, graph[i]);
                graphParts[i] = MongoConfiguration.GetPropertyAlias(typeToQuery, graph[i]);

                if (property.PropertyType.IsGenericType)
                {
                    typeToQuery = property.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    typeToQuery = property.PropertyType.HasElementType ? property.PropertyType.GetElementType() : property.PropertyType;
                }
            }

            return(graphParts);
        }
Esempio n. 3
0
 public void Mongo_Configuration_Should_AutoMap_Id_Property()
 {
     Assert.Equal("_id", MongoConfiguration.GetPropertyAlias(typeof(IdMap0), "_ID"));
     Assert.Equal("_id", MongoConfiguration.GetPropertyAlias(typeof(IdMap1), "TheID"));
     Assert.Equal("_id", MongoConfiguration.GetPropertyAlias(typeof(IdMap2), "ID"));
     Assert.Equal("_id", MongoConfiguration.GetPropertyAlias(typeof(IdMap3), "id"));
     Assert.Equal("_id", MongoConfiguration.GetPropertyAlias(typeof(IdMap4), "Id"));
 }
Esempio n. 4
0
        public void Mongo_Configuration_Can_Remove_Mapping()
        {
            MongoConfiguration.Initialize(r => r.For <User>(u => u.ForProperty(h => h.LastName).UseAlias("lName")));
            //confirm that mapping was set.
            Assert.Equal("lName", MongoConfiguration.GetPropertyAlias(typeof(User), "LastName"));

            MongoConfiguration.RemoveMapFor <User>();
            //confirm that mapping was unset.
            Assert.Equal("LastName", MongoConfiguration.GetPropertyAlias(typeof(User), "LastName"));
        }
Esempio n. 5
0
        public void Mongo_Configuration_Echos_Unmapped_Property_Names()
        {
            MongoConfiguration.Initialize(r => r.For <User>(u => u.ForProperty(user => user.FirstName)
                                                            .UseAlias("first")) /*.WithProfileNamed("Sample")*/);

            var first = MongoConfiguration.GetPropertyAlias(typeof(User), "FirstName");
            var last  = MongoConfiguration.GetPropertyAlias(typeof(User), "LastName");

            Assert.Equal("first", first);
            Assert.Equal("LastName", last);
        }
        private string VisitAlias(MemberExpression m)
        {
            var alias = MongoConfiguration.GetPropertyAlias(m.Expression.Type, m.Member.Name);
            var id    = ReflectionHelper.GetHelperForType(m.Expression.Type).FindIdProperty();

            if (id != null && id.Name == alias)
            {
                alias = "_id";
            }

            return(alias);
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the fully qualified and mapped retval from the member expression.
        /// </summary>
        /// <param retval="mex"></param>
        /// <returns></returns>
        public static string GetPropertyAlias(this MemberExpression mex)
        {
            var retval   = "";
            var parentEx = mex.Expression as MemberExpression;

            if (parentEx != null)
            {
                //we need to recurse because we're not at the root yet.
                retval += GetPropertyAlias(parentEx) + ".";
            }
            retval += MongoConfiguration.GetPropertyAlias(mex.Expression.Type, mex.Member.Name);
            return(retval);
        }
Esempio n. 8
0
        /// <summary>
        /// Actually write the property bytes.
        /// </summary>
        /// <param retval="document">The document.</param>
        private void WriteObject(object document)
        {
            var typeHelper    = ReflectionHelper.GetHelperForType(document.GetType());
            var idProperty    = typeHelper.FindIdProperty();
            var documentType  = document.GetType();
            var discriminator = typeHelper.GetTypeDiscriminator();

            if (String.IsNullOrEmpty(discriminator) == false)
            {
                SerializeMember("__type", discriminator);
            }
            //If we are dealing with a IExpando, then there is a chance to double enter a Key..
            // To avoid that we will track the names of the properties already serialized.
            List <string> processedFields = new List <string>();

            foreach (var property in typeHelper.GetProperties())
            {
                var name = property == idProperty && !IsDbReference(property.DeclaringType)
                               ? "_id"
                               : MongoConfiguration.GetPropertyAlias(documentType, property.Name);

                object value;
                if (property.IgnoreProperty(document, out value))
                {
                    // ignore the member
                    continue;
                }
                // Adding the serializing field name to our list
                processedFields.Add(name);
                // serialize the member
                SerializeMember(name, value);
            }

            var fly = document as IExpando;

            if (fly != null)
            {
                foreach (var f in fly.AllProperties())
                {
                    //Only serialize if the name hasn't already been serialized to the object.
                    if (!processedFields.Contains(f.PropertyName))
                    {
                        SerializeMember(f.PropertyName, f.Value);
                    }
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Loads magic properties.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <param name="idProperty">The id property.</param>
        /// <returns></returns>
        private static IDictionary <string, MagicProperty> LoadMagicProperties(IEnumerable <PropertyInfo> properties, PropertyInfo idProperty)
        {
            var magic = new Dictionary <string, MagicProperty>(StringComparer.CurrentCultureIgnoreCase);

            foreach (var property in properties)
            {
                if (property.GetCustomAttributes(_ignoredType, true).Length > 0 ||
                    property.GetIndexParameters().Length > 0)
                {
                    continue;
                }

                //HACK: this is a latent BUG, if MongoConfiguration is altered after stashing the type helper, we die.
                var alias = MongoConfiguration.GetPropertyAlias(property.DeclaringType, property.Name);

                var name = (property == idProperty && alias != "$id") ? "$_id" : alias;
                magic.Add(name, new MagicProperty(property, property.DeclaringType));
            }

            return(magic);
        }