江苏省建设工人考勤网站,成都最好玩的地方排名,网页设计与制作教程第二版刘瑞新,以前做视频的网站吗框架搭建2聚合服务这里我们将聚合服务命名为Domain.Core和基础服务层一致#xff0c;我们先通过命令创建单层模板项目Domain.Core#xff0c;这里我们删除wwwroot、Data、Entities、Localization、ObjectMapping文件夹及其所有子文件#xff0c;并删除package.json文件和Ser…框架搭建2聚合服务这里我们将聚合服务命名为Domain.Core和基础服务层一致我们先通过命令创建单层模板项目Domain.Core这里我们删除wwwroot、Data、Entities、Localization、ObjectMapping文件夹及其所有子文件并删除package.json文件和Services文件夹下的Dtos文件夹在同级目录添加类库Domain.Core.Constracts并删除其中的Class1.cs文件这里我们在聚合服务中暂时不需要提供动态客户端代理暂时不创建.Client项目如果需要创建方式和基础服务层相同。修改Demo.Core.csproj文件如下Project SdkMicrosoft.NET.Sdk.WebPropertyGroupTargetFrameworknet6.0/TargetFrameworkImplicitUsingsenable/ImplicitUsingsGenerateEmbeddedFilesManifesttrue/GenerateEmbeddedFilesManifest/PropertyGroupItemGroupPackageReference IncludeSerilog.AspNetCore Version5.0.0 /PackageReference IncludeSerilog.Sinks.Async Version1.5.0 //ItemGroupItemGroupPackageReference IncludeVolo.Abp.AspNetCore.Mvc Version5.3.4 /PackageReference IncludeVolo.Abp.Autofac Version5.3.4 /PackageReference IncludeVolo.Abp.Swashbuckle Version5.3.4 /PackageReference IncludeVolo.Abp.AspNetCore.Serilog Version5.3.4 //ItemGroupItemGroupPackageReference IncludeMicrosoft.Extensions.FileProviders.Embedded Version6.0.5 //ItemGroupItemGroupContent RemoveLocalization\Core\*.json /EmbeddedResource IncludeLocalization\Core\*.json /EmbeddedResource Removewwwroot\** /Content Removewwwroot\** /Content Removepackage.json //ItemGroupItemGroupCompile RemoveLogs\** /Content RemoveLogs\** /EmbeddedResource RemoveLogs\** /None RemoveLogs\** /Compile Removewwwroot\** /None Removewwwroot\** //ItemGroupItemGroupProjectReference Include..\..\..\service\notificationmanager\Demo.NotificationManager.Client\Demo.NotificationManager.Client.csproj /ProjectReference Include..\Demo.Core.Contracts\Demo.Core.Contracts.csproj //ItemGroup
/Project修改CoreModule.cs类代码如下using Demo.Core.Contracts;
using Demo.NotificationManager.Client;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;namespace Demo.Core;[DependsOn(// ABP Framework packagestypeof(AbpAspNetCoreMvcModule),typeof(AbpAutofacModule),typeof(AbpSwashbuckleModule),typeof(AbpAspNetCoreSerilogModule),typeof(CoreContractsModule),typeof(NotificationManagerClientModule)
)]
public class CoreModule : AbpModule
{#region 私有方法#region 配置动态WebApiprivate void ConfigureAutoApiControllers(){ConfigureAbpAspNetCoreMvcOptions(options {options.ConventionalControllers.Create(typeof(CoreModule).Assembly);});}#endregion#region 配置swaggerprivate void ConfigureSwagger(IServiceCollection services){services.AddAbpSwaggerGen(options {options.SwaggerDoc(v1, new OpenApiInfo { Title Core API, Version v1 });options.DocInclusionPredicate((docName, description) true);options.CustomSchemaIds(type type.FullName);});}#endregion#endregionpublic override void ConfigureServices(ServiceConfigurationContext context){ConfigureSwagger(context.Services);ConfigureAutoApiControllers();}public override void OnApplicationInitialization(ApplicationInitializationContext context){var app context.GetApplicationBuilder();app.UseRouting();app.UseUnitOfWork();app.UseSwagger();app.UseSwaggerUI(options {options.SwaggerEndpoint(/swagger/v1/swagger.json, Core API);});app.UseAbpSerilogEnrichers();app.UseConfiguredEndpoints();}
}修改Program.cs文件内容如下using Demo.Core;
using Serilog;
using Serilog.Events;Log.Logger new LoggerConfiguration()
#if DEBUG.MinimumLevel.Debug()
#else.MinimumLevel.Information()
#endif.MinimumLevel.Override(Microsoft, LogEventLevel.Information).Enrich.FromLogContext().WriteTo.Async(c c.File(Logs/logs.txt))
#if DEBUG.WriteTo.Async(c c.Console())
#endif.CreateLogger();var builder WebApplication.CreateBuilder(args);builder.Host.UseAutofac().UseSerilog();
builder.Services.ReplaceConfiguration(builder.Configuration);
builder.Services.AddApplicationCoreModule();var app builder.Build();
app.InitializeApplication();
app.Run();修改appsettings.json文件如下{urls: http://*:6003,RemoteServices: {NitificationManager: {BaseUrl: http://localhost:5003/}}
}修改Demo.Core.Contracts.csproj文件Project SdkMicrosoft.NET.SdkPropertyGroupTargetFrameworknet6.0/TargetFrameworkImplicitUsingsenable/ImplicitUsingsNullableenable/Nullable/PropertyGroupItemGroupPackageReference IncludeVolo.Abp.Ddd.Application.Contracts Version5.3.4 //ItemGroup
/Project在Demo.Core.Contracts项目中创建CoreContractsModule.cs文件内容如下using Volo.Abp.Application;
using Volo.Abp.Modularity;namespace Demo.Core.Contracts;[DependsOn(typeof(AbpDddApplicationContractsModule)
)]
public class CoreContractsModule : AbpModule
{}启动Demo.Core项目可正常运行并显示swagger页面则聚合服务创建成功。业务代码1基础服务在Demo.NotificationManager项目Entities文件夹中按领域划分子文件夹这里创建Notifications领域文件夹并添加实体类Notification如下using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;namespace Demo.NotificationManager.Entities.Notifications;/// summary
/// 通知
/// /summary
public class Notification : CreationAuditedEntityGuid
{/// summary/// 接受着用户ID/// /summarypublic Guid ReceiverId { get; set; }/// summary/// 标题/// /summary[StringLength(128)]public string Title { get; set; }/// summary/// 内容/// /summarypublic string Content { get; set; }/// summary/// 是否已读/// /summarypublic bool IsRead { get; set; }
}在Demo.NotificationManager项目Data文件夹下的NotificationManagerDbContext类中添加如下属性并引入相应依赖public DbSetNotification Notifications { get; set; }通过 dotnet-ef 的 migrations add 命令和 database update 命令创建数据库结构。这里我们未用到领域服务如果需要可在Demo.NotificationManager项目中添加Managers文件夹并按领域存放对应代码。在Demo.NotificationManager.Contracts项目中添加Services文件夹DTO和应用服务接口的定义。这里添加子文件夹Notifications并在其中添加Dtos文件夹用于存放DTO。我们添加以下两个DTO分别用于添加和查询通知using System.ComponentModel.DataAnnotations;
using Volo.Abp.Application.Dtos;namespace Demo.NotificationManager.Contracts.Services.Notifications.Dtos;public class NotificationCreateDto:EntityDtoGuid
{/// summary/// 接受着用户ID/// /summarypublic Guid ReceiverId { get; set; }/// summary/// 标题/// /summary[StringLength(128)]public string Title { get; set; }/// summary/// 内容/// /summarypublic string Content { get; set; }/// summary/// 是否已读/// /summarypublic bool IsRead { get; set; }
}using System.ComponentModel.DataAnnotations;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Auditing;namespace Demo.NotificationManager.Contracts.Services.Notifications.Dtos;public class NotificationDto:NotificationCreateDto,ICreationAuditedObject
{/// summary/// 创建时间/// /summarypublic DateTime CreationTime { get; set; }/// summary/// 创建者/// /summarypublic Guid? CreatorId { get; set; }
}添加INotificationAppService应用服务接口内容如下using Demo.NotificationManager.Contracts.Services.Notifications.Dtos;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;namespace Demo.NotificationManager.Contracts.Services.Notifications;public interfaceINotificationAppService : ICrudAppServiceNotificationDto, Guid, PagedResultRequestDto, NotificationCreateDto
{}在Demo.NotificationManger中Services文件夹中添加应用服务实现类这里添加Notifications子文件夹并创建NotificationAppService类如下using Demo.Abp.Extension;
using Demo.NotificationManager.Contracts.Services.Notifications;
using Demo.NotificationManager.Contracts.Services.Notifications.Dtos;
using Demo.NotificationManager.Entities.Notifications;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;namespace Demo.NotificationManager.Services.Notifications;public class NotificationAppService : CustomCrudAppServiceNotification, NotificationDto, Guid,PagedResultRequestDto,NotificationCreateDto, INotificationAppService
{public NotificationAppService(IRepositoryNotification, Guid repository) : base(repository){}
}此处我引入了Mapster框架替代原来的AutoMapper框架所以我们看到CustomCrudAppService类替代了默认增删改查类CrudAppService。CrudAppService类被存放在Demo.Abp.Extension类库中作为通用基类使用代码如下using Mapster;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;namespace Demo.Abp.Extension;#region 重载
public abstract class CustomCrudAppServiceTEntity, TEntityDto, TKey: CustomCrudAppServiceTEntity, TEntityDto, TKey, PagedAndSortedResultRequestDtowhere TEntity : class, IEntityTKeywhere TEntityDto : IEntityDtoTKey
{protected CustomCrudAppService(IRepositoryTEntity, TKey repository): base(repository){}
}public abstract class CustomCrudAppServiceTEntity, TEntityDto, TKey, TGetListInput: CustomCrudAppServiceTEntity, TEntityDto, TKey, TGetListInput, TEntityDtowhere TEntity : class, IEntityTKeywhere TEntityDto : IEntityDtoTKey
{protected CustomCrudAppService(IRepositoryTEntity, TKey repository): base(repository){}
}public abstract class CustomCrudAppServiceTEntity, TEntityDto, TKey, TGetListInput, TCreateInput: CustomCrudAppServiceTEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInputwhere TEntity : class, IEntityTKeywhere TEntityDto : IEntityDtoTKey
{protected CustomCrudAppService(IRepositoryTEntity, TKey repository): base(repository){}
}public abstract class CustomCrudAppServiceTEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput: CustomCrudAppServiceTEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInputwhere TEntity : class, IEntityTKeywhere TEntityDto : IEntityDtoTKey
{protected CustomCrudAppService(IRepositoryTEntity, TKey repository): base(repository){}protected override TaskTEntityDto MapToGetListOutputDtoAsync(TEntity entity){return MapToGetOutputDtoAsync(entity);}protected override TEntityDto MapToGetListOutputDto(TEntity entity){return MapToGetOutputDto(entity);}
}
#endregionpublic class CustomCrudAppServiceTEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput,TUpdateInput: CrudAppServiceTEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInputwhere TEntity : class, IEntityTKeywhere TGetOutputDto : IEntityDtoTKeywhere TGetListOutputDto : IEntityDtoTKey
{public CustomCrudAppService(IRepositoryTEntity, TKey repository) : base(repository){}#region Mapsterprotected override TEntity MapToEntity(TCreateInput createInput){var entity createInput.AdaptTEntity();SetIdForGuids(entity);return entity;}protected override void MapToEntity(TUpdateInput updateInput, TEntity entity){if (updateInput is IEntityDtoTKey entityDto){entityDto.Id entity.Id;}entity updateInput.Adapt(entity);}protected override TGetOutputDto MapToGetOutputDto(TEntity entity){return entity.AdaptTGetOutputDto(); //ObjectMapper.MapTEntity, TGetOutputDto(entity);}protected override TGetListOutputDto MapToGetListOutputDto(TEntity entity){return entity.AdaptTGetListOutputDto(); //ObjectMapper.MapTEntity, TGetListOutputDto(entity);}#endregion
}此类我继承自原有的CrudAppService类并重写了对象转换方法。使用Mapster比AutoMapper效率更高而且不需要提前声明默认的对象映射关系如果需要额外定义映射关系或操作可通过添加MapConfig类实现。具体用法参考其官方文档https://github.com/rivenfx/Mapster-docs如果我们需要定义领域通用配置、枚举等原属于Domain.Share项目中的内容可创建并存放在Demo.NotificationManager.Contracts项目下Share文件夹中。完成以上步骤并可成功运行Demo.NotificationManager项目则基础服务代码运行成功。未完待续关注我获得更多精彩