Spring.NET – LINQ to SQL transactions support

There is no built-in way to enlist LINQ to SQL query in Spring.NET managed transaction. Long story short, these snippets might be helpful:

Our commonly used LINQ support:

    public delegate T DataContextDelegate<T>(DataContext command);

    public class LinqAdoTemplate : AdoTemplate
    {
        public virtual T Execute<T>(DataContextDelegate<T> del)
        {
            ConnectionTxPair connTxPair = ConnectionUtils.GetConnectionTxPair(DbProvider);

            using (var context = new DataContext(connTxPair.Connection))
            {
                //downcast is a bit smelly here
                //one can throw exception though when connTxPair.Transaction is of invalid type
                context.Transaction = connTxPair.Transaction as DbTransaction;

                return del(context);
            }
        }
    }

    public class LinqAdoDaoSupport : AdoDaoSupport
    {
        //maybe there is some better solution out there
        public LinqAdoTemplate LinqAdoTemplate { get { return AdoTemplate as LinqAdoTemplate; } }
    }

Spring.NET IoC configuration:

<!-- standard ado template -->
  <object id="adoTemplate" type="Spring.Data.Generic.AdoTemplate, Spring.Data">
    <property name="DbProvider" ref="dbProvider"/>
    <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/>
  </object>

  <!-- linq-enabled ado template -->
  <object id="linqAdoTemplate" type="SpringTest1.Services.LinqAdoTemplate, SpringTest1.Services">
    <property name="DbProvider" ref="dbProvider"/>
    <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/>
  </object>

  <object id="SQLEventSink" type="SpringTest1.Services.SQLEventSink, SpringTest1.Services">

    <!--<property name="AdoTemplate" ref="adoTemplate"/>-->
    <property name="AdoTemplate" ref="linqAdoTemplate" />
  </object>

  <tx:attribute-driven/>

And usage…. With no dead-lock!

[Table(Name = "Event")]
    public class TestView
    {
        [Column]
        public DateTime TimeStamp { get; set; }
        [Column]
        public string Description { get; set; }
    }

    public class SQLEventSink : LinqAdoDaoSupport, IDomainEventSink
    {
        [Transaction]
        public void Push(string @event)
        {
            LinqAdoTemplate.ExecuteNonQuery(CommandType.Text, "INSERT INTO EVENT(TimeStamp, Description) VALUES (GETDATE(), N'" + @event + "')");

            var all = LinqAdoTemplate.Execute(delegate(DataContext ctx)
            {
                return ctx.GetTable<TestView>().Where(x => x.Description.Equals(@event)).ToList();
            });
        }
    }

That’s it. Not tested heavily, seems to work though.

Advertisements

One Response to Spring.NET – LINQ to SQL transactions support

  1. Pingback: Spring.NET- Dapper micro ORM support « WellDesignedSoftwareException

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: