Esempio n. 1
0
        public void WithCustomizedTagNameAndIdentityProperty()
        {
            var id = string.Empty;

            using (var store = NewDocumentStore())
            {
                var defaultFindIdentityProperty = store.Conventions.FindIdentityProperty;
                store.Conventions.FindIdentityProperty = property =>
                                                         typeof(IEntity).IsAssignableFrom(property.DeclaringType)
                                          ? property.Name == "Id2"
                                          : defaultFindIdentityProperty(property);

                store.Conventions.FindTypeTagName = type =>
                                                    typeof(IDomainObject).IsAssignableFrom(type)
                                                                        ? "domainobjects"
                                                                        : DocumentConvention.DefaultTypeTagName(type);

                using (var session = store.OpenSession())
                {
                    var domainObject = new DomainObject();
                    session.Store(domainObject);
                    var domainObject2 = new DomainObject();
                    session.Store(domainObject2);
                    session.SaveChanges();
                    id = domainObject.Id2;
                }
                var matchingDomainObjects = store.OpenSession().Query <IDomainObject>().Where(_ => _.Id2 == id).ToList();
                Assert.Equal(matchingDomainObjects.Count, 1);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the database
        /// </summary>
        /// <param name="store">The documentstore with connection parameters already configured</param>
        /// <param name="converters">Custom type converters</param>
        private static void Initialize(IDocumentStore store, IEnumerable <JsonConverter> converters)
        {
            _store = store;

            //Indexes
            _store.Conventions.FindTypeTagName = type =>
            {
                if (typeof(IDomainEvent).IsAssignableFrom(type))
                {
                    return("Events");
                }

                return(DocumentConvention.DefaultTypeTagName(type));
            };

            _store.Initialize();

            //Register custom type converter
            store.Conventions.CustomizeJsonSerializer = serializer =>
            {
                foreach (var converter in converters)
                {
                    serializer.Converters.Add(converter);
                }
            };

            IndexCreation.CreateIndexes(Assembly.GetCallingAssembly(), _store);
        }
Esempio n. 3
0
        public DatabaseModule()
        {
            var documentConvention =
                new DocumentConvention
            {
                FindTypeTagName =
                    type =>
                {
                    if (typeof(Favorite).IsAssignableFrom(type))
                    {
                        return("favorites");
                    }

                    return(DocumentConvention.DefaultTypeTagName(type));
                },
                FindClrTypeName =
                    type => type.AssemblyQualifiedName,
                CustomizeJsonSerializer =
                    serializer =>
                {
                    serializer.Binder = new CustomSerializationBinder();
                }
            };

            _documentStore               = new EmbeddableDocumentStore();
            _documentStore.Conventions   = documentConvention;
            _documentStore.DataDirectory = Path.Combine(AppConstants.AppDataFolder, "Database");

            #if DEBUG
            _documentStore.UseEmbeddedHttpServer = true;
            #endif
        }
 private static void SetRavenCollectionName(IEnumerable <object> events, IDocumentSession session, EventStream stream)
 {
     if (Conventions.FindAggregateTypeForEventType != null)
     {
         var aggregateType = Conventions.FindAggregateTypeForEventType(events.First().GetType());
         session.Advanced.GetMetadataFor(stream)[Constants.RavenEntityName] =
             DocumentConvention.DefaultTypeTagName(aggregateType);
     }
 }
Esempio n. 5
0
        protected override void ConfigureApplicationContainer(IContainer existingContainer)
        {
            // Perform registation that should have an application lifetime
            ApplicationBootstrapper.Init();

            var documentStore = new DocumentStore
            {
                Url             = "http://localhost:8080",
                DefaultDatabase = "Todo"
            };

            documentStore.Conventions.FindTypeTagName = type =>
            {
                if (typeof(Domain.IEvent).IsAssignableFrom(type))
                {
                    return(type.Name);
                }

                //Crude way of making the State id's a little bit prettier
                if (type.Name.EndsWith("State"))
                {
                    return(Inflector.Pluralize(type.Name.Remove(type.Name.LastIndexOf("State", StringComparison.InvariantCulture), 5)));
                }

                return(DocumentConvention.DefaultTypeTagName(type));
            };

            documentStore.Initialize();

            existingContainer.Configure(cfg =>
            {
                cfg.For <IDocumentStore>().Use(documentStore).Singleton();
                cfg.Scan(scanner =>
                {
                    scanner.AssemblyContainingType <CreateTodoItem>();
                    scanner.AssemblyContainingType <IMediator>();
                    scanner.AddAllTypesOf(typeof(IRequestHandler <,>));
                    scanner.AddAllTypesOf(typeof(IEventHandler <>));
                    scanner.AddAllTypesOf(typeof(IPreRequestHandler <>));
                    scanner.AddAllTypesOf(typeof(IPostRequestHandler <,>));
                    scanner.WithDefaultConventions();
                });

                cfg.For <IDocumentSession>()
                .Use(ctx => ctx.GetInstance <IDocumentStore>()
                     .OpenSession());

                cfg.For <IManageUnitOfWork>()
                .Use <RavenDbUnitOfWork>();

                cfg.For(typeof(IRequestHandler <,>))
                .DecorateAllWith(typeof(MediatorPipeline <,>));

                cfg.For <ITodoItemRepository>().Use <TodoItemRepository>();
            });
        }
Esempio n. 6
0
        private static string FindTypeTagName(Type type)
        {
            var collectionAttribute = type.GetCustomAttribute <CollectionAttribute>();

            if (collectionAttribute == null)
            {
                return(DocumentConvention.DefaultTypeTagName(type));
            }
            else
            {
                return(collectionAttribute.Name);
            }
        }
        public CustomizeCollectionAssignmentForEntities()
        {
            var store = new DocumentStore();

            #region custom_collection_name
            store.Conventions.FindTypeTagName = type =>
            {
                if (typeof(Category).IsAssignableFrom(type))
                {
                    return("ProductGroups");
                }

                return(DocumentConvention.DefaultTypeTagName(type));
            };
            #endregion
        }
Esempio n. 8
0
        void InitializeDocumentStore(string address)
        {
            try
            {
                _documentStore = new DocumentStore
                {
                    Conventions =
                    {
                        FindTypeTagName = type =>
                        {
                            if (typeof(IUser).IsAssignableFrom(type))
                            {
                                return("IUser");
                            }
                            if (typeof(IActivity).IsAssignableFrom(type))
                            {
                                return("IActivity");
                            }
                            if (typeof(IDevice).IsAssignableFrom(type))
                            {
                                return("IDevice");
                            }
                            if (typeof(IResource).IsAssignableFrom(type))
                            {
                                return("IResource");
                            }
                            if (typeof(INotification).IsAssignableFrom(type))
                            {
                                return("INotification");
                            }
                            return(DocumentConvention.DefaultTypeTagName(type));
                        }
                    }
                };

                _documentStore.ParseConnectionString("Url = " + address);
                _documentStore.Initialize();

                LoadStore();
                SubscribeToChanges();
                OnConnectionEstablished();
            }
            catch
            {
                throw new Exception("RavenDB data bases " + DatabaseName + "_is not running or not found on url " + address);
            }
        }
Esempio n. 9
0
        protected override void ModifyStore(Raven.Client.Embedded.EmbeddableDocumentStore documentStore)
        {
            base.ModifyStore(documentStore);
            documentStore.Conventions = new DocumentConvention
            {
                CustomizeJsonSerializer    = serializer => serializer.TypeNameHandling = TypeNameHandling.All,
                DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites,
                FindTypeTagName            = type =>
                {
                    if (typeof(Account).IsAssignableFrom(type))
                    {
                        return("Accounts");
                    }

                    return(DocumentConvention.DefaultTypeTagName(type));
                },
            };
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes the database
        /// </summary>
        /// <param name="store">The documentstore with connection parameters already configured</param>
        private static void Initialize(IDocumentStore store)
        {
            _store = store;

            //Indexes
            _store.Conventions.FindTypeTagName = type =>
            {
                if (typeof(IDomainEvent).IsAssignableFrom(type))
                {
                    return("Events");
                }

                return(DocumentConvention.DefaultTypeTagName(type));
            };

            _store.Initialize();

            IndexCreation.CreateIndexes(Assembly.GetCallingAssembly(), _store);
        }
Esempio n. 11
0
        private static EmbeddableDocumentStore InitializeDb()
        {
            var documentStore = new EmbeddableDocumentStore
            {
                DefaultDatabase = "Triage",
                DataDirectory   = ConfigurationManager.AppSettings["Triage.RavenDbDirectory"] ?? "~/../Triage.Database"
            };

            documentStore.Initialize();

            documentStore.Conventions.FindTypeTagName = type =>
                                                        type.IsSubclassOf(typeof(Message))
                    ? DocumentConvention.DefaultTypeTagName(typeof(Message))
                    : DocumentConvention.DefaultTypeTagName(type);


            IndexCreation.CreateIndexes(typeof(MeasureSummaryIndex).Assembly, documentStore);
            return(documentStore);
        }
Esempio n. 12
0
 public void OtherWays()
 {
     #region other_ways_1
     DocumentStore store = new DocumentStore()
     {
         Conventions =
         {
             FindTypeTagName = type =>
             {
                 if (typeof(Animal).IsAssignableFrom(type))
                 {
                     return("Animals");
                 }
                 return(DocumentConvention.DefaultTypeTagName(type));
             }
         }
     };
     #endregion
 }
Esempio n. 13
0
 public static IDocumentStore CreateDocumentStore()
 {
     try
     {
         ServiceStartupLogger.LogMessage("Start RavenHelper.CreateDocumentStore, creating document store");
         var documentStore = new DocumentStore
         {
             ConnectionStringName = "RavenDB",
             Conventions          =
             {
                 FindTypeTagName               = type =>
                 {
                     if (typeof(SettingsBase).IsAssignableFrom(type))
                     {
                         return("SettingsBases");
                     }
                     if (typeof(ConnectionSettingBase).IsAssignableFrom(type))
                     {
                         return("ConnectionSettingBases");
                     }
                     return(DocumentConvention.DefaultTypeTagName(type));
                 },
                 MaxNumberOfRequestsPerSession = AppSettingsHelper.GetIntSetting("RavenMaxNumberOfRequestsPerSession", 30000)
             }
         };
         ServiceStartupLogger.LogMessage("RavenHelper.CreateDocumentStore, calling Initialize");
         documentStore.Initialize();
         ServiceStartupLogger.LogMessage("RavenHelper.CreateDocumentStore, creating indexes");
         IndexCreation.CreateIndexes(typeof(MMDB.DataService.Data.Jobs.DataServiceJobBase <>).Assembly, documentStore);
         ServiceStartupLogger.LogMessage("RavenHelper.CreateDocumentStore, diabling all caching");
         documentStore.DatabaseCommands.DisableAllCaching();
         ServiceStartupLogger.LogMessage("Done RavenHelper.CreateDocumentStore");
         return(documentStore);
     }
     catch (Exception err)
     {
         ServiceStartupLogger.LogMessage("RavenHelper.CreateDocumentStore error: " + err.ToString());
         throw;
     }
 }