Example #1
0
        /// <summary>
        ///     Initializes the database, collections, indexes, and partition keys.
        /// </summary>
        public async Task InitializeDatabase()
        {
            DatabaseResponse response = await _client.CreateDatabaseIfNotExistsAsync(DatabaseName);

            Type[] types = Assembly.GetExecutingAssembly().GetTypes();

            Dictionary <string, ContainerProperties> containers = new Dictionary <string, ContainerProperties>();

            foreach (Type type in types)
            {
                ContainerAttribute containerSpecification = type.GetCustomAttribute <ContainerAttribute>(false);

                if (containerSpecification == null)
                {
                    continue;
                }

                string containerName = type.Name;

                if (containerSpecification != null && !string.IsNullOrEmpty(containerSpecification.Name))
                {
                    containerName = containerSpecification.Name;
                }

                if (!containers.ContainsKey(containerName))
                {
                    // Add to the dictionary if we don't have one already for this name.
                    containers[containerName] = new ContainerProperties {
                        Id = containerName
                    };
                }

                ContainerProperties containerProperties = containers[containerName];
            }

            // Iterate the containers and provision them.
            foreach (string key in containers.Keys)
            {
                // All containers have explicit ParititionKey definition.
                containers[key].PartitionKeyPath = "/PartitionKey";

                ContainerResponse containerResponse = await response.Database.CreateContainerIfNotExistsAsync(containers[key]);
            }
        }
Example #2
0
        /// <summary>
        ///     Gets a container from the default database using the specified name.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <returns>An instance of the specified container.</returns>
        public Container GetContainer <T>() where T : DomainEntityBase
        {
            Type type = typeof(T);

            if (ContainersByType.ContainsKey(type))
            {
                return(ContainersByType[type]);
            }

            ContainerAttribute containerDefinition = type.GetCustomAttribute <ContainerAttribute>(false);

            string containerName = (string.IsNullOrEmpty(containerDefinition.Name) ? type.Name : containerDefinition.Name);

            Container container = GetContainer(containerName);

            // cache it
            ContainersByType[type] = container;

            return(container);
        }