Example #1
0
        private IDocumentStore SetStore(
            DocumentStoreParameters parameters)
        {
            var documentConvention = new DocumentConvention
            {
                JsonContractResolver = new PropertiesOnlyContractResolver(),
                FindTypeTagName      = parameters.FindTypeTagName ?? (t => t.Name),
                FindIdentityPropertyNameFromEntityName =
                    parameters.FindIdentityPropertyNameFromEntityName ?? (entityName => "Id"),
                CustomizeJsonSerializer = serializer => serializer.TypeNameHandling = TypeNameHandling.All,
                TransformTypeTagNameToDocumentKeyPrefix = parameters.TransformTypeTagNameToDocumentKeyPrefix ?? (s => s)
            };

            documentConvention.DocumentKeyGenerator = (dbName, databaseCommands, entity) =>
            {
                var entityType  = entity.GetType();
                var typeTagName = documentConvention.FindTypeTagName(entityType);
                var idProperty  = documentConvention.FindIdentityPropertyNameFromEntityName(typeTagName);
                var property    = entityType.GetProperty(idProperty);
                var id          = property.GetValue(entity);

                var prefix = documentConvention.TransformTypeTagNameToDocumentKeyPrefix(typeTagName);
                return(string.IsNullOrEmpty(prefix)
                    ? id.ToString()
                    : string.Concat(prefix, documentConvention.IdentityPartsSeparator, id));
            };

            documentConvention.FindFullDocumentKeyFromNonStringIdentifier = (id, type, allowNull) =>
            {
                var typeTagName = documentConvention.FindTypeTagName(type);
                var prefix      = documentConvention.TransformTypeTagNameToDocumentKeyPrefix(typeTagName);
                return(string.IsNullOrEmpty(prefix)
                    ? id.ToString()
                    : string.Concat(prefix, documentConvention.IdentityPartsSeparator, id));
            };

            var store = this.createStrategy.New(
                documentConvention,
                parameters.DatabaseName,
                parameters.ConnectionStringName ?? ConnectionStringName);

            store.Initialize();

            return(store);
        }
Example #2
0
 public void AddStore(
     DocumentStoreParameters parameters)
 {
     this.stores[parameters.DatabaseName] = this.SetStore(
         parameters);
 }