Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Writing a custom audit plugin


In the ServiceStack framework, plugins are a great way to encapsulate completely independent functionality that can be reused between projects. ServiceStack itself bundles loads of useful packages this way, including authentication, validation, and others.

In this recipe, we will build a plugin to add an audit to our database's create and update methods—you might find this useful if your project requires auditing for changes to a data source. We'll make use of OrmLite features to do so.

How to do It...

Create a new class library project that will contain your plugin and all its required components—AuditFeaturePlugin.

Add ServiceStack and its dependencies as references to the new project.

Create an interface for objects you want to audit called IAuditable:

public interface IAuditable
{
    DateTime CreatedDate { get; set; }
    DateTime ModifiedDate { get; set; }
    string ModifiedByUserId { get; set; }
}
Create a class that implements IPlugin.
public class AuditFeature : IPlugin
{
  public IDbConnectionFactory DbConnectionFactory { get; set; }

  public void Register(IAppHost appHost)
  {
    if (OrmLiteConfig.DialectProvider == null)
    {
      throw new Exception("AuditFeature requires the use of OrmLite and a DialectProvider must be first initialized.");
    }

    OrmLiteConfig.InsertFilter = AuditInsert;
    OrmLiteConfig.UpdateFilter = AuditUpdate;
  }

  private void AuditInsert(IDbCommand command, object rowObj)
  {
    var auditObject = rowObj as IAuditable;
    if (auditObject != null)
    {
      var now = DateTime.UtcNow;
      auditObject.CreatedDate = now;
      auditObject.ModifiedDate = now;
      // Modified by user running the process of the AppPool. Note: only works in Windows.
      auditObject.ModifiedByUserId =System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    }
  }

  private void AuditUpdate(IDbCommand command, object rowObj)
  {
    var auditObject = rowObj as IAuditable;
    if (auditObject != null)
    {
      var now = DateTime.UtcNow;
      auditObject.ModifiedDate = now;
      // Modified by user running the process of the AppPool. Note: only works in Windows.
      auditObject.ModifiedByUserId =System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  }
  }
}

Now that we have created a separate class library that contains our AuditFeature plugin, we can share it with the main project that is hosting the ServiceStack web services. Remember to add a reference to the main project so that both the IAuditable interface and the AuditFeature plugin itself are able to be used.

Register the plugin from within your ApplicationHost class:

public class ApplicationHost : AppHostBase
{
  public ApplicationHost(): base("Reidson Messenger", typeof(MessageRequest).Assembly)
  { }

  public override void Configure(Container container)
  {
    Plugins.Add(new AuditFeature());
  }
}
Add the IAuditable interface to a model class, like the Message class:
public class Message : IAuditable
{
  //...
  public DateTime CreatedDate { get;set; }
  public DateTime ModfiedDate { get;set; }
  public string ModfiedByUserId { get;set; }
}

How it works...

IPlugin that ServiceStack provides has one single method that ServiceStack calls when the framework is initializing.

In the case of the AuditFeature class, a check is made to make sure that OrmLite is being used in the project that is running the AuditFeature function by checking whether DialectProvider is currently being used with OrmLite.

Once this is done, it binds an action to both InsertFilter and UpdateFilter provided by OrmLite. These actions are fired whenever an insert or an update is processed using the OrmLite framework.

See also

  • Using Ormlite filtering for custom actions on insert/update