Skip to content

pacificIT/ef-enterpriseextensions

 
 

Repository files navigation

ef-enterpriseextensions

Build status

Unosquare Labs EntityFramework.EnterpriseExtensions Library contains a set of useful helpers and classes to common tasks related to process and data manipulation in enterprise applications.

NuGet Installation:

NuGet version

PM> Install-Package Unosquare.EntityFramework.EnterpriseExtensions

or

NuGet version

PM> Install-Package Unosquare.Identity.EntityFramework.EnterpriseExtensions

if you are using Identity Entity Framework.

Usage

First you need to change your DbContext or IdentityDbContext to BusinessDbContext or IdentityBusinessDbContext respectly and you can attach Business Controllers to your DbContext in the constructor. They will execute before anytime you call SaveChanges method. The controllers require to specified a BusinessRuleAttribute to identify what CRUD action and which Entity types will be processed.

EF Enterprise Extensions includes a JobBase abstract class (with a singleton extension named SingletonJobBase), so you can build business jobs easily. Check the Sample app.

Business Rules - Audit Trail

Audit Trails is a task to save the changes to any operation perform in a record. In other words, capture what change between any data saving. This operation is important in many system and you can accomplish with these extensions easily. The AuditTrailController can be attached to your BusinessDbContextand setup which Entities will be recorded in the three CRUD actions supported, create, update and delete.

To start using the AuditTrailController you need to specify a table where the Audit Trail data will be, you should implement the IAuditTrailEntry interface in your entity, for example:

public class AuditTrailEntry : IAuditTrailEntry
    {
        [Key]
        public int AuditId { get; set; }

        public string UserId { get; set; }
        public string TableName { get; set; }
        public int Action { get; set; }
        public string JsonBody { get; set; }
        public DateTime DateCreated { get; set; }
    }

Since you need to know who change the data, you need to construct your DataContext with the user relation. For example in Web API you can send the UserId from the request in the DataContext constructor. The AuditTrailController requires two parameters, the DbContext instance and a string to identify the user, and two type parameters, the DbContext type and the AudiTrailEntry type. You can call the extension method UseAuditTrail to add the Business Controller to your Business DataContext.

public class SampleDb : BusinessDbContext
    {
        public string UserId { get; set; }

        public SampleDb(DbConnection connection, string userid) : base(connection, true)
        {
            UserId = userid;
            this.UseAuditTrail<SampleDb, AuditTrailEntry>(userid);
        }

        public DbSet<AuditTrailEntry> AuditTrailEntrys { get; set; }

        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Product> Products { get; set; }
    }

By default, all the entities will be audited, you can access to the Audit Trail controller and set up the action-types relation with the method AddTypes. All the data is stored as a JSON string. You can use this Business Controller or you can create your own and probably change to serialize the data change in XML or every property in one database record.

Jobs

The Jobs are wrapper to your tasks. They can run in single instance (singletons with SingletonJobBase) or with multiple executions using JobBase. You can execute them in your ASP.NET applications (WebApi too) using the HostingEnvironment.QueueBackgroundWorkItem method in .NET 4.6 and setup your execution condition with a datetime or a flag.

To begin using this Jobs, you need to create a class and inherent from SingletonJobBase or JobBase and fill the implementation methods. The jobs exposes three executions modes:

  • Run - The simple non-async way to execute the task. You can provide your own ThreadPool or mechanism to execute them. Returns true if the task was executed.
  • RunAsync - An awaitable method to run your task.
  • RunBackgroundWork - The ideal method to keep a scheduled task, with an optional idle timespan. You can use this method with HostingEnvironment.QueueBackgroundWorkItem and your BackgroundCondition method to generate a cron-like system in your OWIN application.

About

EntityFramework.EnterpriseExtensions Library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%