Created
September 10, 2010 22:13
-
-
Save fboiton/574460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ListingTriggers | |
{ | |
[SqlTrigger(Name = "trig_listingmodified", | |
Target = "ppc_t_account_profile", | |
Event = "FOR INSERT, DELETE, UPDATE")] | |
public static void WhenListingModified() | |
{ | |
var context = SqlContext.TriggerContext; | |
var builder = new ListingCommandBuilder(); | |
if (context.TriggerAction != TriggerAction.Insert | |
|| context.TriggerAction != TriggerAction.Delete | |
|| context.TriggerAction != TriggerAction.Update) | |
return; | |
var id = TriggersDao.GetInsertedPrimaryKeyValue<int>("account_profile_id"); | |
object message = builder.BuildCommand(id, context.TriggerAction); | |
Bus.Instance.Send(message); | |
} | |
} | |
public class | |
public static class TriggersDao | |
{ | |
public static T GetInsertedPrimaryKeyValue<T>(string fieldName) | |
{ | |
var sql = string.Format("SELECT {0} FROM INSERTED", fieldName); | |
var conn = new SqlConnection("context connection=true"); | |
conn.Open(); | |
var cmd = new SqlCommand(sql, conn); | |
return (T)cmd.ExecuteScalar(); | |
} | |
} | |
public class ListingCommandBuilder | |
{ | |
public object BuildCommand(int id, TriggerAction action) { | |
var factory = new ListingCommandFactory(); | |
if (action == TriggerAction.Insert) | |
return factory.CreateInsertCommand(id); | |
if (action == TriggerAction.Update) | |
return factory.CreateUpdateCommand(id); | |
return factory.CreateDeleteCommand(id); | |
} | |
} | |
public class ListingCommandFactory : IListingCommandFactory { | |
public InsertListingCommand CreateInsertCommand(int id) { | |
return new InsertListingCommand { Id = id }; | |
} | |
public UpdateListingCommand CreateUpdateCommand(int id) { | |
return new UpdateListingCommand { Id = id }; | |
} | |
public DeleteListingCommand CreateDeleteCommand(int id) { | |
return new DeleteListingCommand { Id = id }; | |
} | |
} | |
public interface IListingCommandFactory | |
{ | |
InsertListingCommand CreateInsertCommand(int id); | |
UpdateListingCommand CreateUpdateCommand(int id); | |
DeleteListingCommand CreateDeleteCommand(int id); | |
} | |
public class DeleteListingCommand { | |
public int Id { get; set; } | |
} | |
public class UpdateListingCommand { | |
public int Id { get; set; } | |
} | |
public class InsertListingCommand { | |
public int Id { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment