229 lines
6.5 KiB
C#
229 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MongoDB.Bson.Serialization.Conventions;
|
|
using xCommons.Extensions;
|
|
using xDataService.Configuration;
|
|
using xDataService.Constants;
|
|
using xDataService.Db;
|
|
using xDataService.DI;
|
|
using xDataService.Interfaces;
|
|
|
|
namespace xDataService.Models
|
|
{
|
|
/// <summary>
|
|
/// Data Base Descriptor ...
|
|
/// </summary>
|
|
public abstract class XDatabaseDescriptor : IXDatabaseDescriptor
|
|
{
|
|
//
|
|
#region Props ...
|
|
/// <summary>
|
|
/// Database Name ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual string Name { get; set; }
|
|
|
|
/// <summary>
|
|
/// Database Provider ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual XDbProviders Provider { get; set; }
|
|
|
|
/// <summary>
|
|
/// Db Context...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual Type Context { get; set; } = null;
|
|
|
|
/// <summary>
|
|
/// Database Connecion String ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual string ConnectionString { get; set; }
|
|
|
|
/// <summary>
|
|
/// Data Service Configuration ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual XDataBaseConfiguration Configuration { get; set; }
|
|
|
|
/// <summary>
|
|
/// Repositories Descriptions ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public virtual IList<XRepositoryDescriptor> Repositories { get; set; } = null;
|
|
|
|
/// <summary>
|
|
/// Mongo Convention Packs ...
|
|
/// </summary>
|
|
public ConventionPack ConventionPacks { get; set; } = null;
|
|
|
|
/// <summary>
|
|
/// DbContext Options Builder ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public Action<dynamic> OptionsBuilder { get; set; } = null;
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
/// <summary>
|
|
/// Constructor of Descriptor ...
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="repositories"></param>
|
|
public XDatabaseDescriptor(
|
|
string name,
|
|
IList<XRepositoryDescriptor> repositories = null
|
|
)
|
|
{
|
|
//
|
|
Name = name;
|
|
Repositories = repositories;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Configure ...
|
|
/// <summary>
|
|
/// Configure Database Descriptor using Registered Services ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
public void Configure(
|
|
IServiceCollection services
|
|
)
|
|
{
|
|
//
|
|
bool isValid = !Name.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var xDataServiceConfiguration = services.GetRegisteredService<XDataServiceConfiguration>();
|
|
isValid =
|
|
!xDataServiceConfiguration.IsNullOrDefault() &&
|
|
xDataServiceConfiguration.Databases.HasChild() &&
|
|
xDataServiceConfiguration.Databases.Keys.Any(k => k == Name);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var database = xDataServiceConfiguration.Databases[Name];
|
|
isValid = !database.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
Configure(database);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure Database Descriptor using IConfiguration ...
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public void Configure(
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
bool isValid = !Name.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var xDataServiceConfiguration = configuration.GetXDataServiceConfiguration();
|
|
isValid =
|
|
!xDataServiceConfiguration.IsNullOrDefault() &&
|
|
xDataServiceConfiguration.Databases.HasChild() &&
|
|
xDataServiceConfiguration.Databases.Keys.Any(k => k == Name);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var database = xDataServiceConfiguration.Databases[Name];
|
|
isValid = !database.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
Configure(database);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure Database Descriptor using Database Configuration ...
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public void Configure(
|
|
XDataBaseConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
Configuration = configuration;
|
|
Provider = configuration.Provider;
|
|
ConnectionString = configuration.ConnectionString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure Database Descriptor using Provider and Connection String ...
|
|
/// </summary>
|
|
/// <param name="provider"></param>
|
|
/// <param name="connectionString"></param>
|
|
public void Configure(
|
|
XDbProviders provider,
|
|
string connectionString
|
|
)
|
|
{
|
|
//
|
|
Provider = provider;
|
|
ConnectionString = connectionString;
|
|
Configuration = new XDataBaseConfiguration
|
|
{
|
|
Provider = provider,
|
|
ConnectionString = connectionString
|
|
};
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// Data Base Descriptor ...
|
|
/// </summary>
|
|
/// <typeparam name="TContext"></typeparam>
|
|
public abstract class XDatabaseDescriptor<TContext> : XDatabaseDescriptor, IXDatabaseDescriptor<TContext>
|
|
where TContext : XDbContext
|
|
{
|
|
/// <summary>
|
|
/// Constructor of Descriptor ...
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="context"></param>
|
|
/// <param name="repositories"></param>
|
|
public XDatabaseDescriptor(
|
|
string name,
|
|
IList<XRepositoryDescriptor> repositories = null
|
|
) : base(
|
|
name: name,
|
|
repositories: repositories
|
|
)
|
|
{
|
|
Context = typeof(TContext);
|
|
}
|
|
}
|
|
} |