Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ServiceStack 4 Cookbook

You're reading from   ServiceStack 4 Cookbook Over 70 recipes to create web services, build message-based apps, and work with object-relational mapping

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783986569
Length 444 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Configuration and Routing FREE CHAPTER 2. Services and Data Transfer Objects 3. Testing and Logging 4. Object Relational Mapping (OrmLite) 5. HTML and Form Data 6. Filters and Validators 7. Security and Authentication 8. Working with Redis 9. Integrating with Other Technologies A. Getting Started B. Testing Locally 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
You have been reading a chapter from
ServiceStack 4 Cookbook
Published in: Jan 2015
Publisher:
ISBN-13: 9781783986569
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image