Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7 #10

Merged
merged 2 commits into from
Jul 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ReliableDbProvider.Tests/DbProviderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public void Insert_and_select_entity()
Assert.That(dbUser.Name, Is.EqualTo(user.Name));
}
}

[Test]
public void Insert_and_select_entity_with_custom_sql()
{
using (var context = GetContext())
using (var context2 = GetContext())
{
var user = new User { Name = "Name" };
context.Users.Add(user);
context.SaveChanges();

var dbUser = context2.Database.SqlQuery<User>("select * from [user] where Id = '" + user.Id + "'").FirstOrDefault();

Assert.That(dbUser.Name, Is.EqualTo(user.Name));
}
}

[Test]
public void Insert_and_select_multiple_entities()
Expand Down
40 changes: 27 additions & 13 deletions ReliableDbProvider/ReliableSqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;

namespace ReliableDbProvider
Expand All @@ -18,21 +19,30 @@ public class ReliableSqlCommand : DbCommand, ICloneable
/// <summary>
/// The underlying <see cref="SqlCommand"/> being proxied.
/// </summary>
public SqlCommand Current { get; private set; }
private SqlCommand Current { get; set; }

/// <summary>
/// The <see cref="ReliableSqlConnection"/> that has been assigned to the command via the Connection property.
/// The <see cref="ReliableSqlDbConnection"/> wrapper that has been assigned to the command via the Connection property or the ctor.
/// </summary>
public ReliableSqlConnection ReliableConnection { get; set; }
private ReliableSqlDbConnection ReliableConnection { get; set; }

/// <summary>
/// Constructs a <see cref="ReliableSqlCommand"/>.
/// Constructs a <see cref="ReliableSqlCommand"/>. with no associated connection
/// </summary>
public ReliableSqlCommand(SqlCommand commandToWrap)
internal ReliableSqlCommand(SqlCommand commandToWrap)
{
Debug.Assert(commandToWrap.Connection == null, "Expected Command connection to be uninitialised. This constructor creates a new command with no associated connection.");
Current = commandToWrap;
}

public ReliableSqlCommand(ReliableSqlDbConnection connection, SqlCommand commandToWrap)
{
Current = commandToWrap;
ReliableConnection = connection;
if (connection != null)
Current.Connection = ReliableConnection.ReliableConnection.Current;
}

/// <summary>
/// Explicit type-casting between a <see cref="ReliableSqlCommand"/> and a <see cref="SqlCommand"/>.
/// </summary>
Expand All @@ -48,19 +58,23 @@ public static explicit operator SqlCommand(ReliableSqlCommand command)
/// </summary>
protected override DbConnection DbConnection
{
get { return Current.Connection; }
get
{
return ReliableConnection;
}
set
{
if (value == null)
return;
ReliableConnection = ((ReliableSqlDbConnection)value).ReliableConnection;
Current.Connection = ReliableConnection.Current;

ReliableConnection = ((ReliableSqlDbConnection) value);
Current.Connection = ReliableConnection.ReliableConnection.Current;
}
}

public object Clone()
{
return new ReliableSqlCommand(Current.Clone());
return new ReliableSqlCommand(ReliableConnection, Current.Clone());
}

#region Wrapping code
Expand Down Expand Up @@ -93,9 +107,9 @@ protected override DbParameter CreateDbParameter()

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
return ReliableConnection.CommandRetryPolicy.ExecuteAction(() => {
return ReliableConnection.ReliableConnection.CommandRetryPolicy.ExecuteAction(() => {
if (Connection == null)
Connection = ReliableConnection.Open();
Connection = ReliableConnection.ReliableConnection.Open();
if (Connection.State != ConnectionState.Open)
Connection.Open();
return Current.ExecuteReader(behavior);
Expand All @@ -104,12 +118,12 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

public override int ExecuteNonQuery()
{
return ReliableConnection.ExecuteCommand(Current);
return ReliableConnection.ReliableConnection.ExecuteCommand(Current);
}

public override object ExecuteScalar()
{
return ReliableConnection.ExecuteCommand<int>(Current);
return ReliableConnection.ReliableConnection.ExecuteCommand<int>(Current);
}

protected override DbTransaction DbTransaction
Expand Down
2 changes: 1 addition & 1 deletion ReliableDbProvider/ReliableSqlDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override void ChangeDatabase(string databaseName)

protected override DbCommand CreateDbCommand()
{
return new ReliableSqlCommand(ReliableConnection.CreateCommand());
return new ReliableSqlCommand(this, ReliableConnection.CreateCommand());
}

public override void Open()
Expand Down