Created
September 9, 2010 20:31
-
-
Save fboiton/572499 to your computer and use it in GitHub Desktop.
refactoring option fboiton
This file contains hidden or 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
using Microsoft.SqlServer.Server; | |
using System.Data.SqlClient; | |
namespace BrokersWeb.Synchronization.SqlServer.Listings | |
{ | |
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; | |
if (context.TriggerAction != TriggerAction.Insert | |
|| context.TriggerAction != TriggerAction.Delete | |
|| context.TriggerAction != TriggerAction.Update) | |
return; | |
var id = TriggersDao.GetInsertedPrimaryKeyValue<int>("account_profile_id"); | |
object message = ListingCommandFactory.GetCommand(id, context.TriggerAction); | |
Bus.Instance.Send(message); | |
} | |
} | |
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 static class ListingCommandFactory | |
{ | |
public static object GetCommand(int id, TriggerAction action) | |
{ | |
if (action == TriggerAction.Insert) | |
return new InsertListingCommand { Id = id }; | |
if (action == TriggerAction.Update) | |
return new UpdateListingCommand { Id = id }; | |
return new DeleteListingCommand { Id = id }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment