EFTriggerHelper 1.0.0

dotnet add package EFTriggerHelper --version 1.0.0                
NuGet\Install-Package EFTriggerHelper -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFTriggerHelper" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFTriggerHelper --version 1.0.0                
#r "nuget: EFTriggerHelper, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFTriggerHelper as a Cake Addin
#addin nuget:?package=EFTriggerHelper&version=1.0.0

// Install EFTriggerHelper as a Cake Tool
#tool nuget:?package=EFTriggerHelper&version=1.0.0                

EFTriggerHelper

This is a simple trigger implementation in efcore that you can create for your entities.

Table of Contents

Installation

dotnet add package EFTriggerHelper --version 1.0.0

Usage

  • copy and paste in your db context the override of savechanges in the following example.
    public class DummyContext : DbContext
    {
        public DbSet<PersonTbl> Person { get; set; }
       

        private DbContextTriggerHelper helper =  helper = new DbContextTriggerHelper(typeof(PersonTrigger).Assembly);

       
        public override int SaveChanges()
        {
            Func<int> handler = ()  => base.SaveChanges();
            return this.EFTriggerHelperSaveChanges(handler,  helper);
        }

        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            Func<int> handler = () => base.SaveChanges(acceptAllChangesOnSuccess);
            return this.EFTriggerHelperSaveChanges(handler, helper);
        }

        public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
        {
            Func<Task<int>> handler =  async () => await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
            return await this.EFTriggerHelperSaveChangesAsync(handler, helper);
        }

        public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            Func<Task<int>> handler =  async () => await base.SaveChangesAsync(cancellationToken);
            return await this.EFTriggerHelperSaveChangesAsync(handler, helper);
        }

    
    }

  • Define a class that implements the following interfaces. (Every sync interface has a async interface. The IBeforeCreate is sync and the async interface is IBeforeCreateAsync, it will be the same for the rest of the interfaces)
 public class PersonTrigger :
            IBeforeCreateAsync<PersonTbl>,
            IBeforeCreate<PersonTbl>,
            IAfterCreate<PersonTbl>,
            IBeforeUpdate<PersonTbl>,
            IAfterUpdate<PersonTbl>,
            IBeforeDelete<PersonTbl>,
            IAfterDelete<PersonTbl>
    {
        public async Task BeforeCreateAsync(DbContext context,  IEnumerable<PersonTbl> entities) => Console.WriteLine("before create async"); 

        public void BeforeCreate(DbContext context,  IEnumerable<PersonTbl> entities)
        {
             Console.WriteLine("before create executed");
            entities.First().CreatedDate = DateTime.Now;
        }
        
        public void AfterCreate(DbContext context,  IEnumerable<PersonTbl> entities) =>  Console.WriteLine("after create executed");


        public void BeforeUpdate(DbContext context,  IEnumerable<PersonTbl> entities)
        {
            Console.WriteLine("before update executed");
            entities.First().ModifiedDate = DateTime.Now;
            entities.First().ModifiyBy = "TEST";
        }

      
        public void AfterUpdate(DbContext context,  IEnumerable<PersonTbl> entities) => Console.WriteLine("after update executed");
      
        public void BeforeDelete(DbContext context,  IEnumerable<PersonTbl> entities) =>  Console.WriteLine("before delete executed");

        public void AfterDelete(DbContext context,  IEnumerable<PersonTbl> entities) =>  Console.WriteLine("after delete executed");
    }

  • Make sure to pass as parameter the assembly of one concrete trigger class.
  • Make sure to organize your trigger classes in one assembly.
        private DbContextTriggerHelper helper =  helper = new DbContextTriggerHelper(typeof(PersonTrigger).Assembly);

Execute triggers by calling SaveChanges or SaveChangesAsync() in the derived DbContext

public class PersonController : Controller

public IActionResult CreatePerson(PersonTbl person)
{ 
    yourDbContext.Person.Add(person);
    YourDbContext.SaveChanges() // or await yourDbContext.SaveChangesAsync() 
}

Support

Please open an issue for support.

License

MIT License

Copyright (c) [2019] [Hector Mota]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 519 12/24/2019