public static void IEntityIdMappingConvention()
        {
            var noIdConventions = new ConventionProfile();

            noIdConventions.SetIdMemberConvention(new NamedIdMemberConvention("Id")); // no names
            noIdConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());
            ConventionRegistry.Register("noIdConvention", noIdConventions, t => true);
        }
        /// <summary>
        /// Initializes the driver after it has been instantiated.
        /// </summary>
        /// <param name="cxInfo">the serialized connection properties.</param>
        /// <param name="context">The driver object</param>
        /// <param name="executionManager">The current Query Execution Manager for this query</param>
        public override void InitializeContext(IConnectionInfo cxInfo, object context, QueryExecutionManager executionManager)
        {
            base.InitializeContext(cxInfo, context, executionManager);

            //since the type is generated dynamically we can only access it by reflection
            ConnectionProperties props = propsSerializer.Deserialize(cxInfo.DriverData);

            PropertyInfo pinf = context.GetType().GetProperty("SqlTabWriter", BindingFlags.Instance | BindingFlags.Public);

            pinf.SetValue(context, executionManager.SqlTranslationWriter, null);

            if (props.InitializationQuery != null)
            {
                MethodInfo customInitialize = context.GetType().GetMethod("DoCustomInit", new[] { typeof(ConnectionProperties) });
                customInitialize.Invoke(context, new object[] { props });
            }

            MethodInfo init = context.GetType().GetMethod("InitCollections", BindingFlags.Instance | BindingFlags.Public);

            init.Invoke(context, new object[] { });



            if (!mSerializersAlreadyRegistered && props.CustomSerializers != null)
            {
                List <Assembly> assemblies =
                    props.AssemblyLocations.Select(LoadAssemblySafely).ToList();

                foreach (var pair in props.CustomSerializers)
                {
                    var type       = assemblies.Select(a => a.GetType(pair.Key)).FirstOrDefault(x => x != null);
                    var serializer = assemblies.Select(a => a.GetType(pair.Value)).FirstOrDefault(x => x != null);
                    if (type == null || serializer == null)
                    {
                        return;
                    }
                    //throw new Exception(string.Format("Unable to initialize custom serializer {0} for type {1}", pair.Value, pair.Key));

                    BsonSerializer.RegisterSerializer(type, (IBsonSerializer)Activator.CreateInstance(serializer));
                }

                mSerializersAlreadyRegistered = true;
            }

            if (props.AdditionalOptions.BlanketIgnoreExtraElements)
            {
                var conventions = new ConventionProfile();
                conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());

                BsonClassMap.RegisterConventions(conventions, t => true);
            }

            //set as default
            MongoDB.Bson.IO.JsonWriterSettings.Defaults.Indent = true;
        }
        private IQueryable GetQueryableCollection(string connectionString, Dictionary <string, Type> providerTypes, string collectionName)
        {
            var collectionType = CreateDynamicTypeForCollection(collectionName, providerTypes);

            var conventions = new ConventionProfile();

            conventions.SetIdMemberConvention(new NamedIdMemberConvention(MongoMetadata.MappedObjectIdName));
            conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());
            BsonClassMap.RegisterConventions(conventions, t => t == collectionType);

            return(InterceptingProvider.Intercept(
                       new MongoQueryableResource(connectionString, collectionName, collectionType),
                       new ResultExpressionVisitor()));
        }
Exemplo n.º 4
0
        public RepositorioMongo()
        {
            var url    = new MongoUrl(GetMongoDbConnectionString());
            var client = new MongoClient(url);
            var server = client.GetServer();

            database   = server.GetDatabase(url.DatabaseName);
            Collection = database.GetCollection <T>(typeof(T).Name.ToLower());

            //Ajuda no migration, se tiver campo a mais no banco ele ignora
            var conventions = new ConventionProfile();

            conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());
            BsonClassMap.RegisterConventions(conventions, (type) => true);
        }
Exemplo n.º 5
0
        public Repositorio()
        {
            var connectionString = new MongoConnectionStringBuilder(ConfigurationManager.ConnectionStrings["AeeBanco"].ConnectionString);

            server     = MongoServer.Create(connectionString);
            db         = server.GetDatabase(connectionString.DatabaseName);
            Collection = db.GetCollection <T>(typeof(T).Name.ToLower());

            //Corrige a hora no sevidor do banco
            DateTimeSerializationOptions.Defaults = new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document);

            var minhaConvensao = new ConventionProfile();

            minhaConvensao.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());
            BsonClassMap.RegisterConventions(minhaConvensao, (type) => true);
        }
Exemplo n.º 6
0
        public void AfterPropertiesSet()
        {
            var defaultProfile = ConventionProfile.GetDefault();

            _profile = new ConventionProfile();
            _filter  = new ConventionFilterHelper(IncludeFilters, ExcludeFilters);

            _profile.SetDefaultValueConvention(DefaultValueConvention ?? defaultProfile.DefaultValueConvention);
            _profile.SetElementNameConvention(ElementNameConvention ?? defaultProfile.ElementNameConvention);
            _profile.SetExtraElementsMemberConvention(ExtraElementsMemberConvention ?? defaultProfile.ExtraElementsMemberConvention);
            _profile.SetIdGeneratorConvention(IdGeneratorConvention ?? defaultProfile.IdGeneratorConvention);
            _profile.SetIdMemberConvention(IdMemberConvention ?? defaultProfile.IdMemberConvention);
            _profile.SetIgnoreExtraElementsConvention(IgnoreExtraElementsConvention ?? defaultProfile.IgnoreExtraElementsConvention);
            _profile.SetIgnoreIfDefaultConvention(IgnoreIfDefaultConvention ?? defaultProfile.IgnoreIfDefaultConvention);
            _profile.SetIgnoreIfNullConvention(IgnoreIfNullConvention ?? defaultProfile.IgnoreIfNullConvention);
            _profile.SetMemberFinderConvention(MemberFinderConvention ?? defaultProfile.MemberFinderConvention);
            _profile.SetSerializationOptionsConvention(SerializationOptionsConvention ?? defaultProfile.SerializationOptionsConvention);

            BsonClassMap.RegisterConventions(_profile, _filter.Filter);
        }