diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/Database/Contexts/QuestionContext.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/Database/Contexts/QuestionContext.cs index 182e0f2..1b10519 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/Database/Contexts/QuestionContext.cs +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/Database/Contexts/QuestionContext.cs @@ -1,15 +1,15 @@ using EasyMicroservices.QuestionsMicroservice.Database.Entities; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; namespace EasyMicroservices.QuestionsMicroservice.Database.Contexts { public class QuestionContext : RelationalCoreContext { - IDatabaseBuilder _builder; - public QuestionContext(IDatabaseBuilder builder) + IEntityFrameworkCoreDatabaseBuilder _builder; + public QuestionContext(IEntityFrameworkCoreDatabaseBuilder builder) : base(builder) { - _builder = builder; } public DbSet Questions { get; set; } diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/EasyMicroservices.QuestionsMicroservice.Database.csproj b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/EasyMicroservices.QuestionsMicroservice.Database.csproj index 3dbf52f..fea1f4e 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/EasyMicroservices.QuestionsMicroservice.Database.csproj +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Database/EasyMicroservices.QuestionsMicroservice.Database.csproj @@ -11,8 +11,9 @@ - - + + + diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Domain/EasyMicroservices.QuestionsMicroservice.Domain.csproj b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Domain/EasyMicroservices.QuestionsMicroservice.Domain.csproj index 7cf293b..27c7341 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.Domain/EasyMicroservices.QuestionsMicroservice.Domain.csproj +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.Domain/EasyMicroservices.QuestionsMicroservice.Domain.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DatabaseBuilder.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DatabaseBuilder.cs index 6db0656..6daebdc 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DatabaseBuilder.cs +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DatabaseBuilder.cs @@ -1,4 +1,5 @@ -using EasyMicroservices.QuestionsMicroservice.Database; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; +using EasyMicroservices.QuestionsMicroservice.Database; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; @@ -9,16 +10,18 @@ namespace EasyMicroservices.QuestionsMicroservice { - public class DatabaseBuilder : IDatabaseBuilder + public class DatabaseBuilder : IEntityFrameworkCoreDatabaseBuilder { - readonly IConfiguration config = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); + IConfiguration _configuration; + public DatabaseBuilder(IConfiguration configuration) + { + _configuration = configuration; + } public void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseInMemoryDatabase("QuestionDatabase"); - //optionsBuilder.UseSqlServer(config.GetConnectionString("local")); + //optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local")); } } } diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DependencyManager.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DependencyManager.cs deleted file mode 100644 index fb89dbf..0000000 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/DependencyManager.cs +++ /dev/null @@ -1,68 +0,0 @@ -using EasyMicroservices.QuestionsMicroservice.Interfaces; -using EasyMicroservices.QuestionsMicroservice; -using EasyMicroservices.Configuration.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.Cores.Database.Logics; -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.Cores.Interfaces; -using EasyMicroservices.Database.EntityFrameworkCore.Providers; -using EasyMicroservices.Database.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Providers; -using EasyMicroservices.Mapper.Interfaces; -using EasyMicroservices.QuestionsMicroservice.Database.Contexts; -using EasyMicroservices.QuestionsMicroservice.Interfaces; -using System; -using System.Linq; - -namespace EasyMicroservices.QuestionsMicroservice -{ - public class DependencyManager : IDependencyManager - { - public virtual IConfigProvider GetConfigProvider() - { - throw new NotImplementedException(); - } - - public virtual IContractLogic GetContractLogic() - where TEntity : class, IIdSchema - where TResponseContract : class - { - return new LongIdMappedDatabaseLogicBase(GetDatabase().GetReadableOf(), GetDatabase().GetWritableOf(), GetMapper(), GetUniqueIdentityManager()); - } - - public virtual IDatabase GetDatabase() - { - return new EntityFrameworkCoreDatabaseProvider(new QuestionContext(new DatabaseBuilder())); - } - - public static string DefaultUniqueIdentity { get; set; } - public static long MicroserviceId { get; set; } - public static IUniqueIdentityManager UniqueIdentityManager { get; set; } - public virtual IUniqueIdentityManager GetUniqueIdentityManager() - { - if (UniqueIdentityManager == null) - UniqueIdentityManager = new DefaultUniqueIdentityManager(DefaultUniqueIdentity, MicroserviceId); - return UniqueIdentityManager; - } - - public virtual IMapperProvider GetMapper() - { - var mapper = new CompileTimeMapperProvider(new EasyMicroservices.Mapper.SerializerMapper.Providers.SerializerMapperProvider(new EasyMicroservices.Serialization.Newtonsoft.Json.Providers.NewtonsoftJsonProvider(new Newtonsoft.Json.JsonSerializerSettings() - { - ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore - }))); - - foreach (var type in typeof(IDependencyManager).Assembly.GetTypes()) - { - if (typeof(IMapper).IsAssignableFrom(type)) - { - var instance = Activator.CreateInstance(type, mapper); - var returnTypes = type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x => x.ReturnType != typeof(object) && x.Name == "Map").Select(x => x.ReturnType).ToArray(); - mapper.AddMapper(returnTypes[0], returnTypes[1], (IMapper)instance); - } - } - return mapper; - } - } -} diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/EasyMicroservices.QuestionsMicroservice.StartUp.csproj b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/EasyMicroservices.QuestionsMicroservice.StartUp.csproj index 90bec68..5609006 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/EasyMicroservices.QuestionsMicroservice.StartUp.csproj +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/EasyMicroservices.QuestionsMicroservice.StartUp.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/StartUp.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/StartUp.cs deleted file mode 100644 index d8cafa5..0000000 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/StartUp.cs +++ /dev/null @@ -1,19 +0,0 @@ -using EasyMicroservices.QuestionsMicroservice.Helpers; -using EasyMicroservices.QuestionsMicroservice.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyMicroservices.QuestionsMicroservice -{ - public class StartUp - { - public Task Run(IDependencyManager dependencyManager) - { - ApplicationManager.Instance.DependencyManager = dependencyManager; - return Task.CompletedTask; - } - } -} diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/WhiteLabelManager.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/WhiteLabelManager.cs deleted file mode 100644 index 1329c21..0000000 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.StartUp/WhiteLabelManager.cs +++ /dev/null @@ -1,112 +0,0 @@ -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.QuestionsMicroservice.Interfaces; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using WhiteLables.GeneratedServices; -using EasyMicroservices.QuestionsMicroservice; - -namespace EasyMicroservices.QuestionsMicroservice -{ - public class WhiteLabelManager - { - public WhiteLabelManager(IServiceProvider serviceProvider, IDependencyManager dependencyManager) - { - _serviceProvider = serviceProvider; - _dependencyManager = dependencyManager; - } - - private readonly IServiceProvider _serviceProvider; - private readonly IDependencyManager _dependencyManager; - public static HttpClient HttpClient { get; set; } = new HttpClient(); - - string GetDefaultUniqueIdentity(ICollection whiteLables, long? parentId) - { - var found = whiteLables.FirstOrDefault(x => x.ParentId == parentId); - if (found == null) - { - return ""; - } - return $"{DefaultUniqueIdentityManager.GenerateUniqueIdentity(found.Id)}-{GetDefaultUniqueIdentity(whiteLables, found.Id)}".Trim('-'); - } - - public async Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes) - { - if (dbContextTypes.IsNullOrEmpty()) - return; - var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, HttpClient); - var whiteLabels = await whiteLabelClient.GetAllAsync(); - DependencyManager.DefaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels.Result, null); - - var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, HttpClient); - var microservices = await microserviceClient.GetAllAsync(); - var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase)); - if (foundMicroservice == null) - { - foundMicroservice = new WhiteLables.GeneratedServices.MicroserviceContract() - { - InstanceIndex = 1, - Name = microserviceName, - Description = "Automatically added" - }; - var addMicroservice = await microserviceClient.AddAsync(foundMicroservice); - foundMicroservice.Id = addMicroservice.Result; - } - DependencyManager.MicroserviceId = foundMicroservice.Id; - - var uniqueIdentityManager = _dependencyManager.GetUniqueIdentityManager() as DefaultUniqueIdentityManager; - - var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient); - var microserviceContextTables = await microserviceContextTableClient.GetAllAsync(); - - HashSet addedInWhitLabels = new HashSet(); - foreach (var contextTableContract in microserviceContextTables.Result) - { - uniqueIdentityManager.InitializeTables(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName, contextTableContract.ContextTableId); - addedInWhitLabels.Add(uniqueIdentityManager.GetContextTableName(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName)); - } - - foreach (var contextType in dbContextTypes) - { - var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, HttpClient); - var contextTables = await contextTableClient.GetAllAsync(); - using var insctanceOfContext = _serviceProvider.GetService(contextType) as DbContext; - string contextName = uniqueIdentityManager.GetContextName(contextType); - foreach (var entityType in insctanceOfContext.Model.GetEntityTypes()) - { - string tableName = entityType.ServiceOnlyConstructorBinding.RuntimeType.Name; - var tableFullName = uniqueIdentityManager.GetContextTableName(foundMicroservice.Id, contextType.Name, tableName); - if (!addedInWhitLabels.Contains(tableFullName)) - { - if (microserviceContextTables.Result.Any(x => x.ContextName == contextName && x.TableName == tableName && x.MicroserviceId == foundMicroservice.Id)) - continue; - var contextTable = contextTables.Result.FirstOrDefault(x => x.ContextName == contextName && x.TableName == tableName); - if (contextTable == null) - { - contextTable = new WhiteLables.GeneratedServices.ContextTableContract() - { - ContextName = contextName, - TableName = tableName, - }; - var contextTableResult = await contextTableClient.AddAsync(contextTable); - contextTable.Id = contextTableResult.Result; - } - var addedMicroserviceContextTable = await microserviceContextTableClient.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContextTableContract() - { - ContextName = contextName, - TableName = tableName, - MicroserviceName = microserviceName, - MicroserviceId = foundMicroservice.Id, - ContextTableId = contextTable.Id - }); - uniqueIdentityManager.InitializeTables(foundMicroservice.Id, contextName, tableName, contextTable.Id); - } - } - } - } - } -} diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/AnswerController.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/AnswerController.cs index 00a6b72..d2db5be 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/AnswerController.cs +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/AnswerController.cs @@ -1,5 +1,6 @@ using Contents.GeneratedServices; using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.QuestionsMicroservice.Contracts.Common; @@ -14,20 +15,22 @@ public class AnswerController : SimpleQueryServiceController _questionlogic; private readonly IContractLogic _contractlogic; - private readonly IConfiguration _config; + public string _contentRoot; private readonly ContentClient _contentClient; - private string _contentRoot; + private readonly IConfiguration _config; - public AnswerController(IContractLogic questionlogic, IContractLogic contractLogic, IConfiguration config) : base(contractLogic) + readonly IUnitOfWork unitOfWork; + public AnswerController(IUnitOfWork _unitOfWork, IConfiguration config) : base(_unitOfWork) { - _contractlogic = contractLogic; - _questionlogic = questionlogic; + unitOfWork = _unitOfWork; + _questionlogic = _unitOfWork.GetContractLogic(); + _contractlogic = _unitOfWork.GetContractLogic(); _config = config; - _contentRoot = _config.GetValue("RootAddresses:Content"); _contentClient = new(_contentRoot, new HttpClient()); } + public override async Task> Add(CreateAnswerRequestContract request, CancellationToken cancellationToken = default) { var checkQuestionId = await _questionlogic.GetById(new GetIdRequestContract() { Id = request.QuestionId }); diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/QuestionController.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/QuestionController.cs index 3664ef8..d2dbc1c 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/QuestionController.cs +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Controllers/QuestionController.cs @@ -1,6 +1,7 @@ using Contents.GeneratedServices; using EasyMicroservices.ContentsMicroservice.Clients.Helpers; using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.QuestionsMicroservice.Contracts.Common; @@ -22,15 +23,19 @@ public class QuestionController : SimpleQueryServiceController contractLogic, IContractLogic answerLogic, IConfiguration config) : base(contractLogic) + + readonly IUnitOfWork unitOfWork; + public QuestionController(IUnitOfWork _unitOfWork, IConfiguration config) : base(_unitOfWork) { - _contractlogic = contractLogic; - _answerLogic = answerLogic; + unitOfWork = _unitOfWork; + _contractlogic = _unitOfWork.GetContractLogic(); + _answerLogic = _unitOfWork.GetContractLogic(); _config = config; _contentRoot = _config.GetValue("RootAddresses:Content"); _contentClient = new(_contentRoot, new HttpClient()); } + public override async Task> Add(CreateQuestionRequestContract request, CancellationToken cancellationToken = default) { var addQuestionResult = await base.Add(request, cancellationToken); @@ -97,7 +102,7 @@ await _contentClient.HardDeleteByIdAsync(new Int64DeleteRequestContract [HttpPost] public async Task> GetAllQuestionsWithAnswers(GetAllQuestionsWithAnswersRequestContract request) { - var questions = await _contractlogic.GetAllByUniqueIdentity(request, query => query.Include(x => x.Answers)); + var questions = await _contractlogic.GetAllByUniqueIdentity(request, Cores.DataTypes.GetUniqueIdentityType.All, query => query.Include(x => x.Answers)); if (!questions) return questions.ToListContract(); diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/EasyMicroservices.QuestionsMicroservice.WebApi.csproj b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/EasyMicroservices.QuestionsMicroservice.WebApi.csproj index ab22977..cc7c785 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/EasyMicroservices.QuestionsMicroservice.WebApi.csproj +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/EasyMicroservices.QuestionsMicroservice.WebApi.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Program.cs b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Program.cs index 8c63064..26dca71 100644 --- a/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Program.cs +++ b/src/CSharp/EasyMicroservices.QuestionsMicroservice.WebApi/Program.cs @@ -1,165 +1,38 @@ -using EasyMicroservices.QuestionsMicroservice.Database; using EasyMicroservices.QuestionsMicroservice.Database.Contexts; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Reflection; -using Microsoft.Extensions.DependencyInjection; -using EasyMicroservices.QuestionsMicroservice.Database.Entities; -using EasyMicroservices.QuestionsMicroservice.Contracts; -using EasyMicroservices.QuestionsMicroservice.Interfaces; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using EasyMicroservices.QuestionsMicroservice; -using EasyMicroservices.QuestionsMicroservice.Contracts.Common; -using EasyMicroservices.QuestionsMicroservice.Contracts.Requests; namespace EasyMicroservices.QuestionsMicroservice.WebApi { public class Program { - public static async Task Main(string[] args) { - var builder = WebApplication.CreateBuilder(args); - - IConfiguration config = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); - - // Add services to the container. - //builder.Services.AddAuthorization(); - builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(options => - { - options.SchemaFilter(); - options.SchemaFilter(); - }); - - //builder.Services.AddDbContext(options => - // options.UseSqlServer(builder.Configuration.GetConnectionString(config.GetConnectionString("local"))) - //); - - //builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - string webRootPath = @Directory.GetCurrentDirectory(); - - builder.Services.AddHttpContextAccessor(); - builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddScoped(serviceProvider => new DatabaseBuilder()); - - builder.Services.AddScoped(service => new DependencyManager()); - builder.Services.AddScoped(service => new WhiteLabelManager(service, service.GetService())); - builder.Services.AddTransient(serviceProvider => new QuestionContext(serviceProvider.GetService())); - builder.Services.AddTransient(serviceProvider => config); - - //builder.Services.AddScoped(serviceProvider => new FileManagerProvider()); - //builder.Services.AddScoped(); - - //builder.Services.AddScoped(serviceProvider => new FileManager()); - //builder.Services.AddScoped(); - - var app = builder.Build(); - app.UseDeveloperExceptionPage(); - // Configure the HTTP request pipeline. - app.UseSwagger(); - app.UseSwaggerUI(); - - app.UseHttpsRedirection(); - app.UseAuthorization(); - app.MapControllers(); - - - //CreateDatabase(); - - using (var scope = app.Services.CreateScope()) - { - using var context = scope.ServiceProvider.GetService(); - await context.Database.EnsureCreatedAsync(); - //await context.Database.MigrateAsync(); - await context.DisposeAsync(); - var service = scope.ServiceProvider.GetService(); - await service.Initialize("Question", config.GetValue("RootAddresses:WhiteLabel"), typeof(QuestionContext)); - } - - StartUp startUp = new StartUp(); - await startUp.Run(new DependencyManager()); - app.Run(); + var app = CreateBuilder(args); + var build = await app.Build(); + build.MapControllers(); + build.Run(); } - static void CreateDatabase() + static WebApplicationBuilder CreateBuilder(string[] args) { - using (var context = new QuestionContext(new DatabaseBuilder())) - { - if (context.Database.EnsureCreated()) - { - //auto migration when database created first time - - //add migration history table - - string createEFMigrationsHistoryCommand = $@" -USE [{context.Database.GetDbConnection().Database}]; -SET ANSI_NULLS ON; -SET QUOTED_IDENTIFIER ON; -CREATE TABLE [dbo].[__EFMigrationsHistory]( - [MigrationId] [nvarchar](150) NOT NULL, - [ProductVersion] [nvarchar](32) NOT NULL, - CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY CLUSTERED -( - [MigrationId] ASC -)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] -) ON [PRIMARY]; -"; - context.Database.ExecuteSqlRaw(createEFMigrationsHistoryCommand); - - //insert all of migrations - var dbAssebmly = context.GetType().Assembly; - foreach (var item in dbAssebmly.GetTypes()) - { - if (item.BaseType == typeof(Migration)) - { - string migrationName = item.GetCustomAttributes().First().Id; - var version = typeof(Migration).Assembly.GetName().Version; - string efVersion = $"{version.Major}.{version.Minor}.{version.Build}"; - context.Database.ExecuteSqlRaw("INSERT INTO __EFMigrationsHistory(MigrationId,ProductVersion) VALUES ({0},{1})", migrationName, efVersion); - } - } - } - context.Database.Migrate(); - } + var app = StartUpExtensions.Create(args); + app.Services.Builder(); + app.Services.AddTransient((serviceProvider) => new UnitOfWork(serviceProvider)); + app.Services.AddTransient(serviceProvider => new QuestionContext(serviceProvider.GetService())); + app.Services.AddTransient(); + StartUpExtensions.AddWhiteLabel("Questions", "RootAddresses:WhiteLabel"); + return app; } - } - public class GenericFilter : ISchemaFilter - { - public void Apply(OpenApiSchema schema, SchemaFilterContext context) - { - var type = context.Type; - - if (type.IsGenericType == false) - return; - - schema.Title = $"{type.Name[0..^2]}<{type.GenericTypeArguments[0].Name}>"; - } - } - - public class XEnumNamesSchemaFilter : ISchemaFilter - { - private const string NAME = "x-enumNames"; - public void Apply(OpenApiSchema model, SchemaFilterContext context) + public static async Task Run(string[] args, Action use) { - var typeInfo = context.Type; - // Chances are something in the pipeline might generate this automatically at some point in the future - // therefore it's best to check if it exists. - if (typeInfo.IsEnum && !model.Extensions.ContainsKey(NAME)) - { - var names = Enum.GetNames(context.Type); - var arr = new OpenApiArray(); - arr.AddRange(names.Select(name => new OpenApiString(name))); - model.Extensions.Add(NAME, arr); - } + var app = CreateBuilder(args); + use?.Invoke(app.Services); + var build = await app.Build(); + build.MapControllers(); + build.Run(); } } } \ No newline at end of file diff --git a/src/CSharp/EasyMicroservicesQuestionsMicroservice.Tests/EasyMicroservices.QuestionsMicroservice.Tests.csproj b/src/CSharp/EasyMicroservicesQuestionsMicroservice.Tests/EasyMicroservices.QuestionsMicroservice.Tests.csproj index a7225be..6644d6b 100644 --- a/src/CSharp/EasyMicroservicesQuestionsMicroservice.Tests/EasyMicroservices.QuestionsMicroservice.Tests.csproj +++ b/src/CSharp/EasyMicroservicesQuestionsMicroservice.Tests/EasyMicroservices.QuestionsMicroservice.Tests.csproj @@ -11,11 +11,11 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all