Ejemplo n.º 1
0
        /// <summary>
        /// Add Registry DbContext to the DI system.
        /// </summary>
        /// <typeparam name="TContext">The IRegistryDbContext to use.</typeparam>
        /// <param name="services">The service collection.</param>
        /// <param name="storeOptionsAction">The store options action.</param>
        /// <returns></returns>
        public static IServiceCollection AddRegistryDbContext <TContext>(this IServiceCollection services, Action <RegistryStoreOptions> storeOptionsAction = null)
            where TContext : DbContext, IRegistryDbContext
        {
            var options = new RegistryStoreOptions();

            services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);

            if (options.ResolveDbContextOptions != null)
            {
                services.AddDbContext <TContext>(options.ResolveDbContextOptions);
            }
            else
            {
                services.AddDbContext <TContext>(dbCtxBuilder => options.ConfigureDbContext?.Invoke(dbCtxBuilder));
            }

            services.AddScoped <IRegistryDbContext, TContext>();

            return(services);
        }
        /// <summary>
        /// Configures the service registry model
        /// </summary>
        /// <param name="modelBuilder">The model builder.</param>
        /// <param name="storeOptions">The store options.</param>
        public static void ConfigureServiceRegistry(this ModelBuilder modelBuilder, RegistryStoreOptions storeOptions)
        {
            if (!string.IsNullOrWhiteSpace(storeOptions.DefaultSchema))
            {
                modelBuilder.HasDefaultSchema(storeOptions.DefaultSchema);
            }

            modelBuilder.Entity <Service>(service =>
            {
                service.ToTable(storeOptions.Service);
                service.HasKey(x => x.Id);

                service.Property(x => x.ServiceId).HasMaxLength(200).IsRequired();
                service.Property(x => x.DisplayName).HasMaxLength(1000);

                service.HasIndex(x => x.ServiceId).IsUnique();

                service.HasMany(x => x.Endpoints).WithOne(x => x.Service).HasForeignKey(x => x.ServiceId).IsRequired().OnDelete(DeleteBehavior.Cascade);
                service.HasMany(x => x.IpAddresses).WithOne(x => x.Service).HasForeignKey(x => x.ServiceId).IsRequired().OnDelete(DeleteBehavior.Cascade);
                service.HasMany(x => x.PublicUrls).WithOne(x => x.Service).HasForeignKey(x => x.ServiceId).IsRequired().OnDelete(DeleteBehavior.Cascade);
            });

            modelBuilder.Entity <ServiceEndpoint>(endpoint =>
            {
                endpoint.ToTable(storeOptions.ServiceEndpoint);
                endpoint.Property(x => x.EndpointUri).HasMaxLength(2000).IsRequired();
            });

            modelBuilder.Entity <ServiceIpAddress>(endpoint =>
            {
                endpoint.ToTable(storeOptions.ServiceIpAddress);
                endpoint.Property(x => x.IpAddress).HasMaxLength(16).IsRequired();
            });

            modelBuilder.Entity <ServicePublicUrl>(endpoint =>
            {
                endpoint.ToTable(storeOptions.ServicePublicUrl);
                endpoint.Property(x => x.Url).HasMaxLength(2000).IsRequired();
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RegistryDbContext"/> class.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="storeOptions">The store options.</param>
 /// <exception cref="ArgumentNullException">storeOptions</exception>
 public RegistryDbContext(DbContextOptions <RegistryDbContext> options, RegistryStoreOptions storeOptions)
     : base(options, storeOptions)
 {
 }