DNTFrameworkCore

2025-12-07 0 520

什么是DNTFrameworkCore ?

DNTFrameworkCore是一种基于ASP.NET Core的高质量Web应用程序的轻巧且可扩展的基础架构,并具有以下目标:

  • 在各种应用中的常见结构,例如横切问题等
  • 遵循干燥原则以关注主要业务逻辑
  • 减少开发时间
  • 较少的错误和停止错误传播
  • 减少对OOP和OOD了解的新开发人员的培训时间

基于Crud的思维

申请服务

 public interface IBlogService : IEntityService < int , BlogModel >
{
}

public class BlogService : EntityService < Blog , int , BlogModel > , IBlogService
{
    private readonly IMapper _mapper ;

    public BlogService (
        IDbContext dbContext ,
        IEventBus bus ,
        IMapper mapper ) : base ( dbContext , bus )
    {
        _mapper = mapper ?? throw new ArgumentNullException ( nameof ( mapper ) ) ;
    }

    public override Task < IPagedResult < BlogModel > > FetchPagedListAsync ( FilteredPagedRequest request ,
        CancellationToken cancellationToken = default )
    {
        return EntitySet . AsNoTracking ( )
            . Select ( b => new BlogModel
            {
                Id = b . Id ,
                Version = b . Version ,
                Url = b . Url ,
                Title = b . Title
            } ) . ToPagedListAsync ( request , cancellationToken ) ;
    }

    protected override void MapToEntity ( BlogModel model , Blog blog )
    {
        _mapper . Map ( model , blog ) ;
    }

    protected override BlogModel MapToModel ( Blog blog )
    {
        return _mapper . Map < BlogModel > ( blog ) ;
    }
}

ASP.NET Core WebAPI

 [ Route ( \"api/[controller]\" ) ]
public class BlogsController : EntityController < IBlogService , int , BlogModel >
{
    public BlogsController ( IBlogService service ) : base ( service )
    {
    }

    protected override string CreatePermissionName => PermissionNames . Blogs_Create ;
    protected override string EditPermissionName => PermissionNames . Blogs_Edit ;
    protected override string ViewPermissionName => PermissionNames . Blogs_View ;
    protected override string DeletePermissionName => PermissionNames . Blogs_Delete ;
}

ASP.NET核心MVC

 public class BlogsController : EntityController < IBlogService , int , BlogModel >
{
   public BlogsController ( IBlogService service ) : base ( service )
   {
   }

   protected override string CreatePermissionName => PermissionNames . Blogs_Create ;
   protected override string EditPermissionName => PermissionNames . Blogs_Edit ;
   protected override string ViewPermissionName => PermissionNames . Blogs_View ;
   protected override string DeletePermissionName => PermissionNames . Blogs_Delete ;
   protected override string ViewName => \"_BlogPartial\" ;
}

_blogpartial.cshtml

 @inherits EntityFormRazorPage < BlogModel >
@{
   Layout = \" _EntityFormLayout \" ;
   EntityName = \" Blog \" ;
   DeletePermission = PermissionNames . Blogs_Delete ;
   CreatePermission = PermissionNames . Blogs_Create ;
   EditPermission = PermissionNames . Blogs_Edit ;
   EntityDisplayName = \" Blog \" ;
}

< div class = \" form-group row \" >
   < div class = \" col col-md-8 \" >
       < label asp-for = \" Title \" class = \" col-form-label text-md-left \" ></ label >
       < input asp-for = \" Title \" autocomplete = \" off \" class = \" form-control \" />
       < span asp-validation-for = \" Title \" class = \" text-danger \" ></ span >
   </ div >
</ div >
< div class = \" form-group row \" >
   < div class = \" col \" >
       < label asp-for = \" Url \" class = \" col-form-label text-md-left \" ></ label >
       < input asp-for = \" Url \" class = \" form-control \" type = \" url \" />
       < span asp-validation-for = \" Url \" class = \" text-danger \" ></ span >
   </ div >
</ div >

安装

要根据DNTFrameworkCore创建您的第一个项目,您可以安装以下软件包:

DNTFrameworkCore
PM> Install-Package DNTFrameworkCore .EFCore
PM> Install-Package DNTFrameworkCore .EFCore.SqlServer
PM> Install-Package DNTFrameworkCore .Web
PM> Install-Package DNTFrameworkCore .Web.Tenancy
PM> Install-Package DNTFrameworkCore .Web.EFCore
PM> Install-Package DNTFrameworkCore .Licensing
PM> Install-Package DNTFrameworkCore .FluentValidation
\”>

 PM> Install-Package DNTFrameworkCore
PM> Install-Package DNTFrameworkCore .EFCore
PM> Install-Package DNTFrameworkCore .EFCore.SqlServer
PM> Install-Package DNTFrameworkCore .Web
PM> Install-Package DNTFrameworkCore .Web.Tenancy
PM> Install-Package DNTFrameworkCore .Web.EFCore
PM> Install-Package DNTFrameworkCore .Licensing
PM> Install-Package DNTFrameworkCore .FluentValidation

或者

1-运行以下命令以基于ASP.NET Core Web API和DNTFrameworkCore安装样板项目模板:

dotnet new --install DNTFrameworkCore TemplateAPI::*‌‌

2-使用安装模板创建新项目:

dotnet new dntcore-api

现在,您有了下面的解决方案,其中包含完整的身份管理功能包括用户,角色和动态许可管理,并与持续的JWT身份验证机制集成在一起:

有关模板的更多信息,您可以观看DNTFrameworkCore模板存储库

特征

  • 应用程序输入验证
  • 交易管理
  • 事件
  • EntityGraph Tracking(Master-detail)
  • 编号
  • 功能编程错误处理
  • 许可授权
  • 实体服务
  • EntityController(API和MVC)
  • 基于EFCORE的DBLOGGER提供商
  • protectionKey Efcore商店
  • 钩子
  • SoftDelete
  • 租赁
  • 跟踪机制(iCreationTracking,Imodification Tracking)
  • 荧光验证整合
  • BackowdTaskqueue
  • Rowintegrity
  • 起始仪机制
  • CQRS(即将推出)
  • 实体历史(即将推出)

用法

DNTFrameworkCore .testapi完成ASP.NET核心Web API

创建实体

 public class Task : Entity < int > , INumberedEntity , IHasRowVersion , IHasRowIntegrity , ICreationTracking , IModificationTracking
{
    public const int MaxTitleLength = 256 ;
    public const int MaxDescriptionLength = 1024 ;

    public string Title { get ; set ; }
    public string NormalizedTitle { get ; set ; }
    public string Number { get ; set ; }
    public string Description { get ; set ; }
    public TaskState State { get ; set ; } = TaskState . Todo ;
    public byte [ ] Version { get ; set ; }
}

实现从DBContextCore继承的ProjectDbContext

 public class ProjectDbContext : DbContextCore
{
    public ProjectDbContext ( DbContextOptions < ProjectDbContext > options , IEnumerable < IHook > hooks ) : base ( options , hooks )
    {
    }

    protected override void OnModelCreating ( ModelBuilder modelBuilder )
    {               
        modelBuilder . ApplyConfigurationsFromAssembly ( Assembly . GetExecutingAssembly ( ) ) ;

        modelBuilder . AddJsonFields ( ) ;
        modelBuilder . AddTrackingFields < long > ( ) ;
        modelBuilder . AddIsDeletedField ( ) ;
        modelBuilder . AddRowVersionField ( ) ;
        modelBuilder . AddRowIntegrityField ( ) ;
            
        modelBuilder . NormalizeDateTime ( ) ;
        modelBuilder . NormalizeDecimalPrecision ( ) ;
            
        base . OnModelCreating ( modelBuilder ) ;
    }
}

创建模型/DTO

DNTFrameworkCore.TestAPI")]
public class TaskModel : MasterModel<int>, IValidatableObject
{
public string Title { get; set; }

[MaxLength(50, ErrorMessage = "Validation from DataAnnotations")]
public string Number { get; set; }

public string Description { get; set; }
public TaskState State { get; set; } = TaskState.Todo;

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Title == "IValidatableObject")
{
yield return new ValidationResult("Validation from IValidatableObject");
}
}
}\”>

 [ LocalizationResource ( Name = \"SharedResource\" , Location = \" DNTFrameworkCore .TestAPI\" ) ]
public class TaskModel : MasterModel < int > , IValidatableObject
{
    public string Title { get ; set ; }

    [ MaxLength ( 50 , ErrorMessage = \"Validation from DataAnnotations\" ) ]
    public string Number { get ; set ; }

    public string Description { get ; set ; }
    public TaskState State { get ; set ; } = TaskState . Todo ;

    public IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
    {
        if ( Title == \"IValidatableObject\" )
        {
            yield return new ValidationResult ( \"Validation from IValidatableObject\" ) ;
        }
    }
}

注意:基于验证基础结构,您可以使用多种方法验证模型/DTO,使用DataAnnotation ValidateAttribute,实现IvalIdatableObject,或实现DNTFrameworkCore软件包中存在的Imodelvalidator。

 public class TaskValidator : ModelValidator < TaskModel >
{
    public override IEnumerable < ModelValidationResult > Validate ( TaskModel model )
    {
        if ( ! Enum . IsDefined ( typeof ( TaskState ) , model . State ) )
        {
            yield return new ModelValidationResult ( nameof ( TaskModel . State ) , \"Validation from IModelValidator\" ) ;
        }
    }
}

同样,在大多数情况下,一种模型/DTO可以满足您对创建/编辑/查看实体的要求。但是,您可以创建以下方式创建重新模型:

 public class TaskReadModel : ReadModel < int >
{
    public string Title { get ; set ; }
    public string Number { get ; set ; }
    public TaskState State { get ; set ; } = TaskState . Todo ;
}

实施服务

 public interface ITaskService : IEntityService < int , TaskReadModel , TaskModel , TaskFilteredPagedRequest >
{
}

public class TaskService : EntityService < Task , int , TaskReadModel , TaskModel , TaskFilteredPagedRequest > ,
    ITaskService
{
    private readonly IMapper _mapper ;
    public TaskService ( IDbContext dbContext , IEventBus bus , IMapper mapper ) : base ( dbContext , bus )
    {
        _mapper = mapper ?? throw new ArgumentNullException ( nameof ( mapper ) ;
    }

    public override Task < IPagedResult < TaskReadModel > > FetchPagedListAsync ( TaskFilteredPagedRequest request ,
        CancellationToken cancellationToken = default )
    {
        return EntitySet . AsNoTracking ( )
            . WhereIf ( model . State . HasValue , t => t . State == model . State )
            . Select ( t => new TaskReadModel
            {
                Id = t . Id ,
                Title = t . Title ,
                State = t . State ,
                Number = t . Number
            } ) . ToPagedListAsync ( request , cancellationToken ) ;
    }

    protected override void MapToEntity ( TaskModel model , Task task )
    {
        _mapper . Map ( model , task ) ;
    }

    protected override TaskModel MapToModel ( Task task )
    {
        return _mapper . Map < TaskModel > ( task ) ;
    }
}

在DNTFrameworkCore .efcore中,自动应用程序或其他映射器库没有依赖性,然后您可以通过实现maptomodel和maptoentity抽象方法来手动进行映射。

实现API控制器

 [ Route ( \"api/[controller]\" ) ]
public class
    TasksController : EntityController < ITaskService , int , TaskReadModel , TaskModel , TaskFilteredPagedRequest >
{
    public TasksController ( ITaskService service ) : base ( service )
    {
    }

    protected override string CreatePermissionName => PermissionNames . Tasks_Create ;
    protected override string EditPermissionName => PermissionNames . Tasks_Edit ;
    protected override string ViewPermissionName => PermissionNames . Tasks_View ;
    protected override string DeletePermissionName => PermissionNames . Tasks_Delete ;
} 
 [ Route ( \"api/[controller]\" ) ]
public class BlogsController : EntityController < IBlogService , int , BlogModel >
{
    public BlogsController ( IBlogService service ) : base ( service )
    {
    }

    protected override string CreatePermissionName => PermissionNames . Blogs_Create ;
    protected override string EditPermissionName => PermissionNames . Blogs_Edit ;
    protected override string ViewPermissionName => PermissionNames . Blogs_View ;
    protected override string DeletePermissionName => PermissionNames . Blogs_Delete ;
} 

基于任务的思维

丰富的领域模型

 public class PriceType : Entity < long > , IAggregateRoot
{
    private PriceType ( Title title )
    {
        Title = title ;
    }
    
    public PriceType ( Title title , IPriceTypePolicy policy )
    {
        if ( title == null ) throw new ArgumentNullException ( nameof ( title ) ) ;
        if ( policy == null ) throw new ArgumentNullException ( nameof ( policy ) ) ;

        Title = title ;

        if ( ! policy . IsUnique ( this ) ) ThrowDomainException ( \"PriceType Title Should Be Unique\" ) ;

        AddDomainEvent ( new PriceTypeCreatedDomainEvent ( this ) ) ;
    }

    public Title Title { get ; private set ; }

    // public static Result<PriceType> New(Title title, IPriceTypePolicy policy)
    // {
    //     if (title == null) throw new ArgumentNullException(nameof(title));
    //     if (policy == null) throw new ArgumentNullException(nameof(policy));
    //
    //     var priceType = new PriceType(title);
    //     if (!policy.IsUnique(priceType)) return Fail<PriceType>(\"PriceType Title Should Be Unique\");
    //
    //     priceType.AddDomainEvent(new PriceTypeCreatedDomainEvent(priceType));
    //
    //     return Ok(priceType);
    // }
}

ValueObject

 public class Title : ValueObject
{
private Title ( )
{
}

public Title ( string value )
{
value ??= string . Empty ;

switch ( value . Length )
{
case 0 :
ThrowDomainException ( \"title should not be empty\" ) ;
break ;
case > 100 :
ThrowDomainException ( \"title is too long\" ) ;
break ;
}
}

public string Value { get ; private set ; }

protected override IEnumerable < object > EqualityValues
{
get { <spa

下载源码

通过命令行克隆项目:

git clone https://github.com/rabbal/DNTFrameworkCore.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 DNTFrameworkCore https://www.zuozi.net/31954.html

php swiss qr bill
上一篇: php swiss qr bill
Angular Full Stack
下一篇: Angular Full Stack
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务