diff --git a/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs b/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs deleted file mode 100644 index 0d921ab..0000000 --- a/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -using NewArchitectureLab.Apps.Product; -using Pole.Application.Command; -using Pole.Application.Cqrs; -using Pole.Domain.UnitOfWork; -using Pole.Grpc.ExtraType; -using PoleSample.Apis.Product; -using Product.Api.Domain.Event; -using Product.Api.Domain.ProductTypeAggregate; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace Product.Api.Application.CommandHandler -{ - public class AddProductTypeCommandHandler : ICommandHandler, CommonCommandResponse> - { - private readonly IProductTypeRepository _productTypeRepository; - private readonly IUnitOfWork _unitOfWork; - public AddProductTypeCommandHandler(IProductTypeRepository productTypeRepository, IUnitOfWork unitOfWork) - { - _productTypeRepository = productTypeRepository; - _unitOfWork = unitOfWork; - } - public async Task Handle(Command request, CancellationToken cancellationToken) - { - var productType = new Domain.ProductTypeAggregate.ProductType(Guid.NewGuid().ToString("N"), request.Data.Name); - - _productTypeRepository.Add(productType); - ProductTypeAddedDomainEvent productTypeAddedDomainEvent = new ProductTypeAddedDomainEvent - { - ProductTypeId = productType.Id, - ProductTypeName = productType.Name - }; - - productType.AddDomainEvent(productTypeAddedDomainEvent); - var result = await _productTypeRepository.SaveEntitiesAsync(); - - await _unitOfWork.CompeleteAsync(); - return CommonCommandResponse.SuccessResponse; - } - } -} diff --git a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs deleted file mode 100644 index 310ca57..0000000 --- a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Pole.Application.EventBus; -using Pole.Domain; -using Product.Api.Domain.Event; -using Product.Api.Domain.ProductAggregate; -using Product.IntegrationEvents; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace Product.Api.Application.DomainEventHandler -{ - public class AddDefaultProductWhenProductTypeAdded2DomainEventHandler : IDomainEventHandler - { - private readonly IProductRepository _productRepository; - private readonly IEventBus _eventBus; - public AddDefaultProductWhenProductTypeAdded2DomainEventHandler(IProductRepository productRepository, IEventBus eventBus) - { - _productRepository = productRepository; - _eventBus = eventBus; - } - - public async Task Handle(ProductTypeAddedDomainEvent request, CancellationToken cancellationToken) - { - Product.Api.Domain.ProductAggregate.Product product = new Product.Api.Domain.ProductAggregate.Product(Guid.NewGuid().ToString("N"), request.ProductTypeName, 100, request.ProductTypeId); - _productRepository.Add(product); - var backId = Guid.NewGuid().ToString("N"); - ProductAddedIntegrationEvent productAddedIntegrationEvent = new ProductAddedIntegrationEvent() - { - BacketId = backId, - Price = product.Price, - ProductName = product.Name, - ProductId = product.Id - }; - - await _eventBus.Publish(productAddedIntegrationEvent, product.Id); - await _productRepository.SaveEntitiesAsync(); - } - } -} diff --git a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs deleted file mode 100644 index e2f6e73..0000000 --- a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Pole.Domain; -using Product.Api.Domain.Event; -using Product.Api.Domain.ProductAggregate; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace Product.Api.Application.DomainEventHandler -{ - public class AddDefaultProductWhenProductTypeAddedDomainEventHandler : IDomainEventHandler - { - private readonly IProductRepository _productRepository; - public AddDefaultProductWhenProductTypeAddedDomainEventHandler(IProductRepository productRepository) - { - _productRepository = productRepository; - } - - public async Task Handle(ProductTypeAddedDomainEvent request, CancellationToken cancellationToken) - { - Product.Api.Domain.ProductAggregate.Product product = new Product.Api.Domain.ProductAggregate.Product(Guid.NewGuid().ToString("N"), request.ProductTypeName, 100, request.ProductTypeId); - _productRepository.Add(product); - await _productRepository.SaveEntitiesAsync(); - } - } -} diff --git a/samples/apis/Product.Api/Application/IntegrationEvent/CallBack/ProductAddedIntegrationEventCallback.cs b/samples/apis/Product.Api/Application/IntegrationEvent/CallBack/ProductAddedIntegrationEventCallback.cs deleted file mode 100644 index 32a05ba..0000000 --- a/samples/apis/Product.Api/Application/IntegrationEvent/CallBack/ProductAddedIntegrationEventCallback.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Pole.ReliableMessage.Abstraction; -using Product.Api.Application.Query.Abstraction; -using Product.Api.Domain.ProductAggregate; -using Product.IntegrationEvents; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Application.IntegrationEvent.CallBack -{ - public class ProductAddedIntegrationEventCallback : IReliableEventCallback - { - private readonly IProductQuery _productQuery; - public ProductAddedIntegrationEventCallback(IProductQuery productQuery) - { - _productQuery = productQuery; - } - - - public async Task Callback(string callbackParemeter) - { - return await _productQuery.GetById(callbackParemeter) != null; - } - } -} diff --git a/samples/apis/Product.Api/Application/Query/Abstraction/IProductQuery.cs b/samples/apis/Product.Api/Application/Query/Abstraction/IProductQuery.cs deleted file mode 100644 index 700bb05..0000000 --- a/samples/apis/Product.Api/Application/Query/Abstraction/IProductQuery.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Pole.Core.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Application.Query.Abstraction -{ - public interface IProductQuery: IScopedDenpendency - { - Task GetById(string id); - } -} diff --git a/samples/apis/Product.Api/Application/Query/EfCoreProductQuery.cs b/samples/apis/Product.Api/Application/Query/EfCoreProductQuery.cs deleted file mode 100644 index c33c379..0000000 --- a/samples/apis/Product.Api/Application/Query/EfCoreProductQuery.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Product.Api.Application.Query.Abstraction; -using Product.Api.Infrastructure; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Application.Query -{ - public class EfCoreProductQuery : IProductQuery - { - private readonly ProductDbContext _productDbContext; - - public EfCoreProductQuery(ProductDbContext productDbContext) - { - _productDbContext = productDbContext; - } - - public Task GetById(string id) - { - return _productDbContext.Products.FirstOrDefaultAsync(m => m.Id == id); - } - } -} diff --git a/samples/apis/Product.Api/Controllers/ProductTypeController.cs b/samples/apis/Product.Api/Controllers/ProductTypeController.cs new file mode 100644 index 0000000..699593a --- /dev/null +++ b/samples/apis/Product.Api/Controllers/ProductTypeController.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Orleans; +using Product.Api.Grains.Abstraction; + +namespace Product.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ProductTypeController : ControllerBase + { + private readonly IClusterClient clusterClient; + public ProductTypeController(IClusterClient clusterClient) + { + this.clusterClient = clusterClient; + } + [HttpGet("AddProductType")] + public void AddProductType(string name = "test") + { + var newId = Guid.NewGuid().ToString("N").ToLower(); + var grain = clusterClient.GetGrain(newId); + grain.AddProductType(newId, name); + } + } +} \ No newline at end of file diff --git a/samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/Product.cs b/samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/Product.cs deleted file mode 100644 index 1feda2c..0000000 --- a/samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/Product.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Pole.Domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Domain.ProductAggregate -{ - public class Product : Entity, IAggregateRoot - { - public Product(string id ,string name,long price, string productTypeId) - { - Id = id; - Name = name; - Price = price; - ProductTypeId = productTypeId; - } - public string Name { get;private set; } - public long Price { get;private set; } - public string ProductTypeId { get;private set; } - } -} diff --git a/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/IProductTypeRepository.cs b/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/IProductTypeRepository.cs deleted file mode 100644 index 0c66155..0000000 --- a/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/IProductTypeRepository.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Pole.Domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Domain.ProductTypeAggregate -{ - public interface IProductTypeRepository:IRepository - { - } -} diff --git a/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/ProductType.cs b/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/ProductType.cs index 060ab03..58f6d9d 100644 --- a/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/ProductType.cs +++ b/samples/apis/Product.Api/Domain/AggregatesModel/ProductTypeAggregate/ProductType.cs @@ -1,18 +1,14 @@ -using Pole.Domain; +using Pole.Core.Domain; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Product.Api.Domain.ProductTypeAggregate +namespace Product.Api.Domain.AggregatesModel.ProductTypeAggregate { public class ProductType : Entity, IAggregateRoot { - public ProductType(string id,string name) - { - Id = id; - Name = name; - } - public string Name { get;private set; } + public string Name { get; set; } } + } diff --git a/samples/apis/Product.Api/Domain/Event/ProductTypeAddedDomainEvent.cs b/samples/apis/Product.Api/Domain/Event/ProductTypeAddedDomainEvent.cs deleted file mode 100644 index 2822920..0000000 --- a/samples/apis/Product.Api/Domain/Event/ProductTypeAddedDomainEvent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Pole.Domain; -using PoleSample.Apis.Product; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Domain.Event -{ - public class ProductTypeAddedDomainEvent: IDomainEvent - { - public string ProductTypeName { get; set; } - public string ProductTypeId { get; set; } - } -} diff --git a/samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/IProductRepository.cs b/samples/apis/Product.Api/Grains/Abstraction/IProductTypeGrain.cs similarity index 69% rename from samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/IProductRepository.cs rename to samples/apis/Product.Api/Grains/Abstraction/IProductTypeGrain.cs index 15a8cca..ae914b0 100644 --- a/samples/apis/Product.Api/Domain/AggregatesModel/ProductAggregate/IProductRepository.cs +++ b/samples/apis/Product.Api/Grains/Abstraction/IProductTypeGrain.cs @@ -1,13 +1,13 @@ -using Pole.Domain; +using Orleans; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Product.Api.Domain.ProductAggregate +namespace Product.Api.Grains.Abstraction { - public interface IProductRepository : IRepository + public interface IProductTypeGrain: IGrainWithStringKey { - + Task AddProductType(string id, string name); } } diff --git a/samples/apis/Product.Api/Grains/ProductTypeGrain.cs b/samples/apis/Product.Api/Grains/ProductTypeGrain.cs new file mode 100644 index 0000000..fc7ac18 --- /dev/null +++ b/samples/apis/Product.Api/Grains/ProductTypeGrain.cs @@ -0,0 +1,27 @@ +using Orleans; +using Product.Api.Domain.AggregatesModel.ProductTypeAggregate; +using Product.Api.Grains.Abstraction; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Product.Api.Grains +{ + public class ProductTypeGrain : Grain, IProductTypeGrain + { + public async Task AddProductType(string id, string name) + { + if (State != null) return false; + + ProductType productType = new ProductType + { + Id = id, + Name = name + }; + State = productType; + await WriteStateAsync(); + return true; + } + } +} diff --git a/samples/apis/Product.Api/Grpc/ProductTypeService.cs b/samples/apis/Product.Api/Grpc/ProductTypeService.cs deleted file mode 100644 index 57568e9..0000000 --- a/samples/apis/Product.Api/Grpc/ProductTypeService.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Grpc.Core; -using NewArchitectureLab.Apps.Product; -using Pole.Application.Command; -using Pole.Application.Cqrs; -using Pole.Grpc.ExtraType; -using PoleSample.Apis.Product; -using Product.Api.Infrastructure; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Grpc -{ - public class ProductTypeService: PoleSample.Apis.Product.ProductType.ProductTypeBase - { - private readonly ICommandBus _commandBus; - public ProductTypeService(ICommandBus commandBus) - { - _commandBus = commandBus; - } - public override Task Add(AddProductTypeRequest request, ServerCallContext context) - { - var cpmmand = new Command(request); - return _commandBus.Send(cpmmand); - } - } -} diff --git a/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductEntityTypeConfiguration.cs b/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductEntityTypeConfiguration.cs deleted file mode 100644 index 7ac6aa8..0000000 --- a/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductEntityTypeConfiguration.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Infrastructure.EntityConfigurations -{ - public class ProductEntityTypeEntityTypeConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable(nameof(Product)); - - builder.Property(m => m.Id).HasMaxLength(32); - builder.Property(m => m.Name).HasMaxLength(256).IsRequired(); - builder.Property(m => m.ProductTypeId).HasMaxLength(32).IsRequired(); - - builder.Ignore(m => m.DomainEvents); - - builder.HasIndex(m => m.ProductTypeId); - builder.HasKey(m => m.Id); - } - } -} diff --git a/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityTypeConfiguration.cs b/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityConfiguration.cs similarity index 95% rename from samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityTypeConfiguration.cs rename to samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityConfiguration.cs index b7ad4e2..c85b8bd 100644 --- a/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityTypeConfiguration.cs +++ b/samples/apis/Product.Api/Infrastructure/EntityConfigurations/ProductTypeEntityConfiguration.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Product.Api.Domain.ProductTypeAggregate; +using Product.Api.Domain.AggregatesModel.ProductTypeAggregate; using System; using System.Collections.Generic; using System.Linq; @@ -22,4 +22,5 @@ namespace Product.Api.Infrastructure.EntityConfigurations builder.HasKey(m => m.Id); } } + } diff --git a/samples/apis/Product.Api/Infrastructure/ProductDbContext.cs b/samples/apis/Product.Api/Infrastructure/ProductDbContext.cs index 0fd7ae9..2f8ca17 100644 --- a/samples/apis/Product.Api/Infrastructure/ProductDbContext.cs +++ b/samples/apis/Product.Api/Infrastructure/ProductDbContext.cs @@ -1,9 +1,9 @@ -using MediatR; -using Microsoft.EntityFrameworkCore; -using Pole.EntityframeworkCore; +using Microsoft.EntityFrameworkCore; +using Product.Api.Domain.AggregatesModel.ProductTypeAggregate; using Product.Api.Infrastructure.EntityConfigurations; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; @@ -11,8 +11,8 @@ namespace Product.Api.Infrastructure { public class ProductDbContext : DbContext { - public DbSet Products { get; set; } - public DbSet ProductTypes { get; set; } + //public DbSet Products { get; set; } + public DbSet ProductTypes { get; set; } public ProductDbContext(DbContextOptions options) : base(options) { @@ -21,8 +21,9 @@ namespace Product.Api.Infrastructure { base.OnModelCreating(builder); - builder.ApplyConfiguration(new ProductEntityTypeEntityTypeConfiguration()); + //builder.ApplyConfiguration(new ProductEntityTypeEntityTypeConfiguration()); builder.ApplyConfiguration(new ProductTypeEntityTypeConfiguration()); } + } } diff --git a/samples/apis/Product.Api/Infrastructure/Repository/ProductRepository.cs b/samples/apis/Product.Api/Infrastructure/Repository/ProductRepository.cs deleted file mode 100644 index ef852f1..0000000 --- a/samples/apis/Product.Api/Infrastructure/Repository/ProductRepository.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Pole.Domain.EntityframeworkCore; -using Pole.Domain.UnitOfWork; -using Product.Api.Domain.ProductAggregate; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Infrastructure.Repository -{ - public class ProductRepository : EFCoreRepository, IProductRepository - { - public ProductRepository(IServiceProvider serviceProvider) : base(serviceProvider) - { - - } - } -} diff --git a/samples/apis/Product.Api/Infrastructure/Repository/ProductTypeRepository.cs b/samples/apis/Product.Api/Infrastructure/Repository/ProductTypeRepository.cs deleted file mode 100644 index 99d517f..0000000 --- a/samples/apis/Product.Api/Infrastructure/Repository/ProductTypeRepository.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Pole.Domain.EntityframeworkCore; -using Pole.Domain.UnitOfWork; -using Product.Api.Domain.ProductTypeAggregate; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Infrastructure.Repository -{ - public class ProductTypeRepository : EFCoreRepository, IProductTypeRepository - { - public ProductTypeRepository(IServiceProvider serviceProvider) : base(serviceProvider) - { - } - } -} diff --git a/samples/apis/Product.Api/Migrations/20200108090435_init.Designer.cs b/samples/apis/Product.Api/Migrations/20200108090435_init.Designer.cs deleted file mode 100644 index 2839322..0000000 --- a/samples/apis/Product.Api/Migrations/20200108090435_init.Designer.cs +++ /dev/null @@ -1,49 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -using Product.Api.Infrastructure; - -namespace Product.Api.Migrations -{ - [DbContext(typeof(ProductDbContext))] - [Migration("20200108090435_init")] - partial class init - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) - .HasAnnotation("ProductVersion", "3.1.0") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - modelBuilder.Entity("Product.Api.Domain.ProductAggregate.Product", b => - { - b.Property("Id") - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.Property("Name") - .HasColumnType("character varying(256)") - .HasMaxLength(256); - - b.Property("Price") - .HasColumnType("bigint"); - - b.Property("ProductId") - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.HasKey("Id"); - - b.HasIndex("ProductId"); - - b.ToTable("Product"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.cs b/samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.cs deleted file mode 100644 index 39fa3af..0000000 --- a/samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Product.Api.Migrations -{ - public partial class Modify_Product_ProductId : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "ProductId", - table: "Product", - maxLength: 32, - nullable: false, - oldClrType: typeof(string), - oldType: "character varying(32)", - oldMaxLength: 32, - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "Name", - table: "Product", - maxLength: 256, - nullable: false, - oldClrType: typeof(string), - oldType: "character varying(256)", - oldMaxLength: 256, - oldNullable: true); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "ProductId", - table: "Product", - type: "character varying(32)", - maxLength: 32, - nullable: true, - oldClrType: typeof(string), - oldMaxLength: 32); - - migrationBuilder.AlterColumn( - name: "Name", - table: "Product", - type: "character varying(256)", - maxLength: 256, - nullable: true, - oldClrType: typeof(string), - oldMaxLength: 256); - } - } -} diff --git a/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.Designer.cs b/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.Designer.cs deleted file mode 100644 index f5c3fb3..0000000 --- a/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.Designer.cs +++ /dev/null @@ -1,67 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -using Product.Api.Infrastructure; - -namespace Product.Api.Migrations -{ - [DbContext(typeof(ProductDbContext))] - [Migration("20200114090656_Add_Product")] - partial class Add_Product - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) - .HasAnnotation("ProductVersion", "3.1.0") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - modelBuilder.Entity("Product.Api.Domain.ProductAggregate.Product", b => - { - b.Property("Id") - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.Property("Name") - .IsRequired() - .HasColumnType("character varying(256)") - .HasMaxLength(256); - - b.Property("Price") - .HasColumnType("bigint"); - - b.Property("ProductTypeId") - .IsRequired() - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.HasKey("Id"); - - b.HasIndex("ProductTypeId"); - - b.ToTable("Product"); - }); - - modelBuilder.Entity("Product.Api.Domain.ProductTypeAggregate.ProductType", b => - { - b.Property("Id") - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.Property("Name") - .IsRequired() - .HasColumnType("character varying(256)") - .HasMaxLength(256); - - b.HasKey("Id"); - - b.ToTable("ProductType"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.cs b/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.cs deleted file mode 100644 index f059bdf..0000000 --- a/samples/apis/Product.Api/Migrations/20200114090656_Add_Product.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Product.Api.Migrations -{ - public partial class Add_Product : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropIndex( - name: "IX_Product_ProductId", - table: "Product"); - - migrationBuilder.DropColumn( - name: "ProductId", - table: "Product"); - - migrationBuilder.AddColumn( - name: "ProductTypeId", - table: "Product", - maxLength: 32, - nullable: false, - defaultValue: ""); - - migrationBuilder.CreateTable( - name: "ProductType", - columns: table => new - { - Id = table.Column(maxLength: 32, nullable: false), - Name = table.Column(maxLength: 256, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ProductType", x => x.Id); - }); - - migrationBuilder.CreateIndex( - name: "IX_Product_ProductTypeId", - table: "Product", - column: "ProductTypeId"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "ProductType"); - - migrationBuilder.DropIndex( - name: "IX_Product_ProductTypeId", - table: "Product"); - - migrationBuilder.DropColumn( - name: "ProductTypeId", - table: "Product"); - - migrationBuilder.AddColumn( - name: "ProductId", - table: "Product", - type: "character varying(32)", - maxLength: 32, - nullable: false, - defaultValue: ""); - - migrationBuilder.CreateIndex( - name: "IX_Product_ProductId", - table: "Product", - column: "ProductId"); - } - } -} diff --git a/samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.Designer.cs b/samples/apis/Product.Api/Migrations/20200217053642_Init.Designer.cs similarity index 78% rename from samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.Designer.cs rename to samples/apis/Product.Api/Migrations/20200217053642_Init.Designer.cs index 66db2dd..48fe715 100644 --- a/samples/apis/Product.Api/Migrations/20200108092455_Modify_Product_ProductId.Designer.cs +++ b/samples/apis/Product.Api/Migrations/20200217053642_Init.Designer.cs @@ -9,18 +9,18 @@ using Product.Api.Infrastructure; namespace Product.Api.Migrations { [DbContext(typeof(ProductDbContext))] - [Migration("20200108092455_Modify_Product_ProductId")] - partial class Modify_Product_ProductId + [Migration("20200217053642_Init")] + partial class Init { protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) - .HasAnnotation("ProductVersion", "3.1.0") + .HasAnnotation("ProductVersion", "3.1.1") .HasAnnotation("Relational:MaxIdentifierLength", 63); - modelBuilder.Entity("Product.Api.Domain.ProductAggregate.Product", b => + modelBuilder.Entity("Product.Api.Domain.AggregatesModel.ProductTypeAggregate.ProductType", b => { b.Property("Id") .HasColumnType("character varying(32)") @@ -31,19 +31,9 @@ namespace Product.Api.Migrations .HasColumnType("character varying(256)") .HasMaxLength(256); - b.Property("Price") - .HasColumnType("bigint"); - - b.Property("ProductId") - .IsRequired() - .HasColumnType("character varying(32)") - .HasMaxLength(32); - b.HasKey("Id"); - b.HasIndex("ProductId"); - - b.ToTable("Product"); + b.ToTable("ProductType"); }); #pragma warning restore 612, 618 } diff --git a/samples/apis/Product.Api/Migrations/20200108090435_init.cs b/samples/apis/Product.Api/Migrations/20200217053642_Init.cs similarity index 72% rename from samples/apis/Product.Api/Migrations/20200108090435_init.cs rename to samples/apis/Product.Api/Migrations/20200217053642_Init.cs index 13f19f7..1e5abb4 100644 --- a/samples/apis/Product.Api/Migrations/20200108090435_init.cs +++ b/samples/apis/Product.Api/Migrations/20200217053642_Init.cs @@ -2,34 +2,27 @@ namespace Product.Api.Migrations { - public partial class init : Migration + public partial class Init : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "Product", + name: "ProductType", columns: table => new { Id = table.Column(maxLength: 32, nullable: false), - Name = table.Column(maxLength: 256, nullable: true), - Price = table.Column(nullable: false), - ProductId = table.Column(maxLength: 32, nullable: true) + Name = table.Column(maxLength: 256, nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Product", x => x.Id); + table.PrimaryKey("PK_ProductType", x => x.Id); }); - - migrationBuilder.CreateIndex( - name: "IX_Product_ProductId", - table: "Product", - column: "ProductId"); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "Product"); + name: "ProductType"); } } } diff --git a/samples/apis/Product.Api/Migrations/ProductDbContextModelSnapshot.cs b/samples/apis/Product.Api/Migrations/ProductDbContextModelSnapshot.cs index d38d707..ae37ed6 100644 --- a/samples/apis/Product.Api/Migrations/ProductDbContextModelSnapshot.cs +++ b/samples/apis/Product.Api/Migrations/ProductDbContextModelSnapshot.cs @@ -15,36 +15,10 @@ namespace Product.Api.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) - .HasAnnotation("ProductVersion", "3.1.0") + .HasAnnotation("ProductVersion", "3.1.1") .HasAnnotation("Relational:MaxIdentifierLength", 63); - modelBuilder.Entity("Product.Api.Domain.ProductAggregate.Product", b => - { - b.Property("Id") - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.Property("Name") - .IsRequired() - .HasColumnType("character varying(256)") - .HasMaxLength(256); - - b.Property("Price") - .HasColumnType("bigint"); - - b.Property("ProductTypeId") - .IsRequired() - .HasColumnType("character varying(32)") - .HasMaxLength(32); - - b.HasKey("Id"); - - b.HasIndex("ProductTypeId"); - - b.ToTable("Product"); - }); - - modelBuilder.Entity("Product.Api.Domain.ProductTypeAggregate.ProductType", b => + modelBuilder.Entity("Product.Api.Domain.AggregatesModel.ProductTypeAggregate.ProductType", b => { b.Property("Id") .HasColumnType("character varying(32)") diff --git a/samples/apis/Product.Api/Product.Api.csproj b/samples/apis/Product.Api/Product.Api.csproj index 1354fc2..2d1222b 100644 --- a/samples/apis/Product.Api/Product.Api.csproj +++ b/samples/apis/Product.Api/Product.Api.csproj @@ -3,30 +3,29 @@ netcoreapp3.1 - - - - - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + + + + + + + + - - - - - - - - + + \ No newline at end of file diff --git a/samples/apis/Product.Api/ProductDbContextDesignFactory.cs b/samples/apis/Product.Api/ProductDbContextDesignFactory.cs index 1aa4bc2..a948564 100644 --- a/samples/apis/Product.Api/ProductDbContextDesignFactory.cs +++ b/samples/apis/Product.Api/ProductDbContextDesignFactory.cs @@ -1,8 +1,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; -using Pole.Domain.EntityframeworkCore.MediatR; -using Product.Api.Infrastructure; +//using Pole.Domain.EntityframeworkCore.MediatR; +//using Product.Api.Infrastructure; using System; using System.Collections.Generic; using System.IO; @@ -11,18 +11,18 @@ using System.Threading.Tasks; namespace Product.Api { - public class ProductDbContextDesignFactory : IDesignTimeDbContextFactory - { - public ProductDbContext CreateDbContext(string[] args) - { - IConfigurationRoot configuration = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.Development.json") - .Build(); - var optionsBuilder = new DbContextOptionsBuilder() - .UseNpgsql(configuration["postgres:main"]); + //public class ProductDbContextDesignFactory : IDesignTimeDbContextFactory + //{ + // public ProductDbContext CreateDbContext(string[] args) + // { + // IConfigurationRoot configuration = new ConfigurationBuilder() + // .SetBasePath(Directory.GetCurrentDirectory()) + // .AddJsonFile("appsettings.Development.json") + // .Build(); + // var optionsBuilder = new DbContextOptionsBuilder() + // .UseNpgsql(configuration["postgres:main"]); - return new ProductDbContext(optionsBuilder.Options); - } - } + // return new ProductDbContext(optionsBuilder.Options); + // } + //} } diff --git a/samples/apis/Product.Api/Program.cs b/samples/apis/Product.Api/Program.cs index 77cdfbb..b00cca8 100644 --- a/samples/apis/Product.Api/Program.cs +++ b/samples/apis/Product.Api/Program.cs @@ -1,11 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Orleans.Hosting; +using Product.Api.Infrastructure; +using Pole.Orleans.Provider.EntityframeworkCore; +using Orleans; +using Product.Api.Grains; namespace Product.Api { @@ -21,6 +22,12 @@ namespace Product.Api .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); + }) + .UseOrleans(siloBuilder => + { + siloBuilder.UseLocalhostClustering(); + siloBuilder.AddEfGrainStorageAsDefault(); }); + } } diff --git a/samples/apis/Product.Api/Startup.cs b/samples/apis/Product.Api/Startup.cs index cd8b8be..4181f7f 100644 --- a/samples/apis/Product.Api/Startup.cs +++ b/samples/apis/Product.Api/Startup.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Data.SqlClient; -using System.Linq; -using System.Threading.Tasks; -using GreenPipes; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -11,9 +5,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Npgsql; -using Pole.ReliableMessage.Storage.Mongodb; -using Product.Api.Grpc; using Product.Api.Infrastructure; namespace Product.Api @@ -31,68 +22,68 @@ namespace Product.Api // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { - services.AddDbContext(options => - options.UseNpgsql(Configuration["postgres:main"])); + services.AddDbContextPool(options => + options.UseNpgsql(Configuration["postgres:write"])); + services.AddControllers(); + //services.AddGrpc(option => + //{ + // if (Environment.IsDevelopment()) + // { + // option.EnableDetailedErrors = true; + // } + //}); - services.AddGrpc(option => - { - if (Environment.IsDevelopment()) - { - option.EnableDetailedErrors = true; - } - }); - - services.AddGrpcValidation(); - services.AddGrpcRequestValidator(this.GetType().Assembly); + //services.AddGrpcValidation(); + //services.AddGrpcRequestValidator(this.GetType().Assembly); - services.AddPole(option => - { - option.AddManageredAssemblies(this.GetType().Assembly); - option.AutoInjectionDependency(); - option.AutoInjectionCommandHandlersAndDomainEventHandlers(); - option.AddPoleEntityFrameworkCoreDomain(); + //services.AddPole(option => + //{ + // option.AddManageredAssemblies(this.GetType().Assembly); + // option.AutoInjectionDependency(); + // option.AutoInjectionCommandHandlersAndDomainEventHandlers(); + // option.AddPoleEntityFrameworkCoreDomain(); - option.AddPoleReliableMessage(messageOption => - { - messageOption.AddMasstransitRabbitmq(rabbitoption => - { - rabbitoption.RabbitMqHostAddress = Configuration["RabbitmqConfig:HostAddress"]; - rabbitoption.RabbitMqHostUserName = Configuration["RabbitmqConfig:HostUserName"]; - rabbitoption.RabbitMqHostPassword = Configuration["RabbitmqConfig:HostPassword"]; - rabbitoption.QueueNamePrefix = Configuration["ServiceName"]; - rabbitoption.EventHandlerNameSuffix = "IntegrationEventHandler"; - rabbitoption.RetryConfigure = - r => - { - r.Intervals(TimeSpan.FromSeconds(0.1) - , TimeSpan.FromSeconds(1) - , TimeSpan.FromSeconds(4) - , TimeSpan.FromSeconds(16) - , TimeSpan.FromSeconds(64) - ); - r.Ignore(exception => - { - var sqlException = exception.InnerException as PostgresException; - return sqlException != null && sqlException.SqlState == "23505"; - }); - }; - }); - messageOption.AddMongodb(mongodbOption => - { - mongodbOption.ServiceCollectionName = Configuration["ServiceName"]; - mongodbOption.Servers = Configuration.GetSection("MongoConfig:Servers").Get(); - }); - messageOption.AddEventAssemblies(typeof(Startup).Assembly) - .AddEventHandlerAssemblies(typeof(Startup).Assembly); - messageOption.NetworkInterfaceGatewayAddress = Configuration["ReliableMessageOption:NetworkInterfaceGatewayAddress"]; - }); - }); + // option.AddPoleReliableMessage(messageOption => + // { + // messageOption.AddMasstransitRabbitmq(rabbitoption => + // { + // rabbitoption.RabbitMqHostAddress = Configuration["RabbitmqConfig:HostAddress"]; + // rabbitoption.RabbitMqHostUserName = Configuration["RabbitmqConfig:HostUserName"]; + // rabbitoption.RabbitMqHostPassword = Configuration["RabbitmqConfig:HostPassword"]; + // rabbitoption.QueueNamePrefix = Configuration["ServiceName"]; + // rabbitoption.EventHandlerNameSuffix = "IntegrationEventHandler"; + // rabbitoption.RetryConfigure = + // r => + // { + // r.Intervals(TimeSpan.FromSeconds(0.1) + // , TimeSpan.FromSeconds(1) + // , TimeSpan.FromSeconds(4) + // , TimeSpan.FromSeconds(16) + // , TimeSpan.FromSeconds(64) + // ); + // r.Ignore(exception => + // { + // var sqlException = exception.InnerException as PostgresException; + // return sqlException != null && sqlException.SqlState == "23505"; + // }); + // }; + // }); + // messageOption.AddMongodb(mongodbOption => + // { + // mongodbOption.ServiceCollectionName = Configuration["ServiceName"]; + // mongodbOption.Servers = Configuration.GetSection("MongoConfig:Servers").Get(); + // }); + // messageOption.AddEventAssemblies(typeof(Startup).Assembly) + // .AddEventHandlerAssemblies(typeof(Startup).Assembly); + // messageOption.NetworkInterfaceGatewayAddress = Configuration["ReliableMessageOption:NetworkInterfaceGatewayAddress"]; + // }); + //}); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - app.UsePoleReliableMessage(); + //app.UsePoleReliableMessage(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -102,7 +93,8 @@ namespace Product.Api app.UseEndpoints(endpoints => { - endpoints.MapGrpcService(); + endpoints.MapDefaultControllerRoute(); + //endpoints.MapGrpcService(); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); diff --git a/samples/apis/Product.Api/appsettings.Development.json b/samples/apis/Product.Api/appsettings.Development.json index 2585d46..d1b496a 100644 --- a/samples/apis/Product.Api/appsettings.Development.json +++ b/samples/apis/Product.Api/appsettings.Development.json @@ -8,23 +8,12 @@ } }, "postgres": { - "main": "Server=192.168.0.248;Port=5432;Username=postgres;Password=comteck2020!@#;Database=Pole_samples_Product;Enlist=True;Timeout=0;Command Timeout=600;Pooling=false;MinPoolSize=20;MaxPoolSize=500;" + "write": "Server=115.238.140.58;Port=5432;Username=postgres;Password=comteck2020!@#;Database=Pole-Product;Enlist=True;Timeout=0;Command Timeout=600;Pooling=false;MinPoolSize=20;MaxPoolSize=500;" }, "ServiceName": "Product", "RabbitmqConfig": { "HostAddress": "rabbitmq://192.168.0.248/", "HostUserName": "comteck", "HostPassword": "comteck3030" - }, - "MongoConfig": { - "Servers": [ - { - "Host": "192.168.0.248", - "Port": "27017" - } - ] - }, - "ReliableMessageOption": { - "NetworkInterfaceGatewayAddress": "192.168.0.1" } } diff --git a/samples/apis/Product.Api/appsettings.json b/samples/apis/Product.Api/appsettings.json index 1e979d9..695417e 100644 --- a/samples/apis/Product.Api/appsettings.json +++ b/samples/apis/Product.Api/appsettings.json @@ -8,28 +8,17 @@ } }, "postgres": { - "main": "Server=192.168.0.248;Port=5432;Username=postgres;Password=comteck2020!@#;Database=Pole_samples_Product;Enlist=True;Timeout=0;Command Timeout=600;Pooling=false;MinPoolSize=20;MaxPoolSize=500;" + "write": "Server=192.168.0.248;Port=5432;Username=postgres;Password=comteck2020!@#;Database=Pole_samples_Product;Enlist=True;Timeout=0;Command Timeout=600;Pooling=false;MinPoolSize=20;MaxPoolSize=500;" }, "ServiceName": "Product", "RabbitmqConfig": { "HostAddress": "rabbitmq://192.168.0.248/", "HostUserName": "comteck", "HostPassword": "comteck3030" - }, - "MongoConfig": { - "Servers": [ - { - "Host": "192.168.0.248", - "Port": "27017" - } - ] - }, - "ReliableMessageOption": { - "NetworkInterfaceGatewayAddress": "192.168.0.1" - }, - "Kestrel": { - "EndpointDefaults": { - "Protocols": "Http2" - } } + //"Kestrel": { + // "EndpointDefaults": { + // "Protocols": "Http2" + // } + //} } diff --git a/src/Pole.Core/Domain/Entity.cs b/src/Pole.Core/Domain/Entity.cs new file mode 100644 index 0000000..f57c527 --- /dev/null +++ b/src/Pole.Core/Domain/Entity.cs @@ -0,0 +1,75 @@ +using Pole.Core.EventBus.Event; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Pole.Core.Domain +{ + public abstract class Entity + { + string _id; + public virtual string Id + { + get + { + return _id; + } + set + { + _id = value; + } + } + public List DomainEvents { get; private set; } + public bool IsTransient() + { + return string.IsNullOrEmpty(this._id); + } + public override bool Equals(object obj) + { + if (obj == null || !(obj is Entity)) + return false; + + if (Object.ReferenceEquals(this, obj)) + return true; + + if (this.GetType() != obj.GetType()) + return false; + + Entity item = (Entity)obj; + + if (item.IsTransient() || this.IsTransient()) + return false; + else + return item.Id == this.Id; + } + public override int GetHashCode() + { + return this.Id.GetHashCode(); + } + public static bool operator ==(Entity left, Entity right) + { + if (Object.Equals(left, null)) + return (Object.Equals(right, null)) ? true : false; + else + return left.Equals(right); + } + public static bool operator !=(Entity left, Entity right) + { + return !(left == right); + } + public void AddDomainEvent(IEvent eventItem) + { + DomainEvents = DomainEvents ?? new List(); + DomainEvents.Add(eventItem); + } + public void RemoveDomainEvent(IEvent eventItem) + { + if (DomainEvents is null) return; + DomainEvents.Remove(eventItem); + } + public void ClearDomainEvents() + { + DomainEvents?.Clear(); + } + } +} diff --git a/src/Pole.Core/Domain/IAggregateRoot.cs b/src/Pole.Core/Domain/IAggregateRoot.cs new file mode 100644 index 0000000..988e813 --- /dev/null +++ b/src/Pole.Core/Domain/IAggregateRoot.cs @@ -0,0 +1,8 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Pole.Core.Domain +{ + public interface IAggregateRoot { } +} diff --git a/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageServiceCollectionExtensions.cs b/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageServiceCollectionExtensions.cs new file mode 100644 index 0000000..ed16723 --- /dev/null +++ b/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageServiceCollectionExtensions.cs @@ -0,0 +1,72 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; +using Orleans; +using Orleans.Providers; +using Orleans.Runtime; +using Orleans.Storage; +using Pole.Orleans.Provider.EntityframeworkCore.Conventions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Pole.Orleans.Provider.EntityframeworkCore +{ + public static class GrainStorageServiceCollectionExtensions + { + public static IServiceCollection ConfigureGrainStorageOptions( + this IServiceCollection services, + Action> configureOptions = null) + where TContext : DbContext + where TGrain : Grain + where TEntity : class, new() + { + return services + .AddSingleton>, + GrainStoragePostConfigureOptions>() + .Configure>(typeof(TGrain).FullName, options => + { + configureOptions?.Invoke(options); + }); + } + + public static IServiceCollection ConfigureGrainStorageOptions( + this IServiceCollection services, + Action> configureOptions = null) + where TContext : DbContext + where TGrain : Grain + where TGrainState : new() + where TEntity : class + { + return services + .AddSingleton>, + GrainStoragePostConfigureOptions>() + .Configure>(typeof(TGrain).FullName, options => + { + configureOptions?.Invoke(options); + }); + } + + public static IServiceCollection AddEfGrainStorage( + this IServiceCollection services, + string providerName = ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME) + where TContext : DbContext + { + services.TryAddSingleton(typeof(IEntityTypeResolver), typeof(EntityTypeResolver)); + services.TryAddSingleton(typeof(IGrainStorageConvention), typeof(GrainStorageConvention)); + services.TryAddSingleton(typeof(IGrainStateEntryConfigurator<,,>), + typeof(DefaultGrainStateEntryConfigurator<,,>)); + services.AddSingleton(typeof(EntityFrameworkGrainStorage)); + + services.TryAddSingleton(sp => + sp.GetServiceByName(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); + + services.AddSingletonNamedService(providerName, + (sp, name) => sp.GetRequiredService>()); + + return services; + } + + } +} diff --git a/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageSiloHostBuilderExtensions.cs b/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageSiloHostBuilderExtensions.cs new file mode 100644 index 0000000..d1c195a --- /dev/null +++ b/src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageSiloHostBuilderExtensions.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore; +using Orleans.Hosting; +using Orleans.Providers; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Pole.Orleans.Provider.EntityframeworkCore +{ + public static class GrainStorageSiloHostBuilderExtensions + { + public static ISiloHostBuilder AddEfGrainStorageAsDefault(this ISiloHostBuilder builder) + where TContext : DbContext + { + return builder.AddEfGrainStorage(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME); + } + + public static ISiloHostBuilder AddEfGrainStorage(this ISiloHostBuilder builder, + string providerName) + where TContext : DbContext + { + return builder + .ConfigureServices(services => { services.AddEfGrainStorage(providerName); }); + } + + public static ISiloBuilder AddEfGrainStorageAsDefault(this ISiloBuilder builder) + where TContext : DbContext + { + return builder.AddEfGrainStorage(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME); + } + + public static ISiloBuilder AddEfGrainStorage(this ISiloBuilder builder, + string providerName) + where TContext : DbContext + { + return builder + .ConfigureServices(services => { services.AddEfGrainStorage(providerName); }); + } + } +}