I didn't work on the business logic addins that we support in the product, but they have a very interesting model to allow addins to be called before saving/deleting an account or document. The bad thing is we didn't ship a sample in our SDK and I just did a quick search and couldn't find any out there. I'm going to implement this by tranforming the UIAddIn sample in our SDK (download and install it here). I'm using SBA with Service Pack 3 and the sample product company. Let's start.

1. Open the UIAddIn project.
2. Add a new class that implements the ISdkAddInDriver interface. I got the basic code from the Concepts Manual of our SDK. I'm going to post the code here:

    public class BusinessLogic : ISdkAddInDriver
    {
        
public BusinessLogic(IBaseMasterEntity contextObject)
        {
            
this.contextObject contextObject;
            this
.instance contextObject.SmallBusinessInstance;
        
}

        
public  void  RunPreSave()
        {
            
// Do Pre-Save work here
        
}

        
public  void  RunPostSave()
        {
            
// Do Post-Save work here
        
}

        
public  void RunPreDelete()
        {
            
// Do Pre-Delete work here
        
}

        
public  void RunPostDelete()
        {
            
// Do Post-Delete work here
        
}

        
public IBaseMasterEntity ContextObject
        {
            
get
            
{
                
return this.contextObject;
            
}
        }

        
private IBaseMasterEntity contextObject;
        private 
ISmallBusinessInstance instance;
    
}

3. Now we need to hook the business logic addin so it can be registered and instantiated by SBA. Here's what I have to do:

In DriverRegistration.cs we need to return a new myDriverInfo for the business logic addin. I added this code in line 42.

   myDriverInfo = new MyDriverInfo(new Guid("6EF926D5-79B8-476e-8847-3AE0BCCBF28F"), DriverType.SdkAddIn, false"""Validate Customer Email Business Logic Addin""This is a test business logic to validate email on customers.", Guid.Empty, typeof(BusinessLogic).FullName);

   
driverInfo.Add(myDriverInfo);

I'm just creating a new guid because I will only have one business logic on this assembly. The last parameter uses BusinessLogic which is the name of my class implementing the ISdkAddInDriver.

In DriverRegistration.cs I need to instantiate my business logic addin. I change the LoadSdkAddInDriver to return a new instance of my BusinessLogic class. This is my new implementation:

        public ISdkAddInDriver LoadSdkAddInDriver(Guid driverGuid, IBaseMasterEntity ownerEntity)
        {
            
return new BusinessLogic(ownerEntity);
        
}

Finally, in MyDriverInfo.cs we need to support the AppliedTypes property. In this case, I'm simple going to change the property directly just to make this addin work.

        public Type[] AppliedTypes
        {
            
get
            
{
                
return new Type[] { typeof(ICustomerAccount) };
            
}
        }

4. I'm going to put a simple validation for customers so we can test this business logic addin. If the addin wants to cancel the save event (from the PreSave handler), it needs to throw an exception. The message shown to the user will be an ugly technical message, so I would personally suggest to show a friendly message before that. Here is my code:

   public  void  RunPreSave()
   {
       
// validate we have en email here
      
ICustomerAccount customer (ICustomerAccount)contextObject;

      
ICustomerVendorEmail email customer.CustomerVendorEmails.GetByType(CustomerVendorEmailType.Email1);

      if 
((email.EmailAddress == null) || (email.EmailAddress.Length == 0))
      {
         
throw new SmallBusinessException(ErrorCases.EmailTypeInvalid, "EMAIL MISSING");
      
}
   }

Gotchas
Each time you modify the code in the UIAddIn project, the assembly version will change so you would need to reinstall you addin again or change the AssemblyInfo.cs to have a fixed version number. This was the only issue I ran into. Then, I just have to copy the UIAddin.dll to the SBA location and install it. If you were to ship a business logic, I would suggest you add it to your GAC and install in a separate folder.