Skip to content

Commit

Permalink
Refined the implementation of the Disposable objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
daxnet committed Dec 3, 2017
1 parent 82efe5b commit e4b3878
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class DataServiceController<TKey, TAggregateRoot> : Controller
private readonly IQueryConditionParser queryConditionParser;
private readonly ISortSpecificationParser sortSpecificationParser;
private readonly IKeyGenerator<TKey, TAggregateRoot> keyGenerator;
private bool disposed = false;
#endregion

#region Ctor
Expand Down Expand Up @@ -358,12 +359,16 @@ public virtual async Task<IActionResult> Patch(TKey id, [FromBody] JsonPatchDocu
/// otherwise <c>false</c>.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
this.repositoryContext.Dispose();
}
if (disposing)
{
this.repositoryContext.Dispose();
}

base.Dispose(disposing);
disposed = true;
base.Dispose(disposing);
}
}
}
}
8 changes: 5 additions & 3 deletions src/Apworks.Messaging.RabbitMQ/MessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ public void Subscribe(string route = null)
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
if (!disposed)
if (disposing)
{
this.channel.Dispose();
this.connection.Dispose();
disposed = true;
}

disposed = true;
base.Dispose(disposing);
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/Apworks.Messaging.Simple/MessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ public void Subscribe(string route = null)
}
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing) { }

private void OnMessagePublished(MessagePublishedEventArgs e)
{
this.MessagePublished?.Invoke(this, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Apworks.Repositories.EntityFramework
{
public sealed class EntityFrameworkRepositoryContext : RepositoryContext<DbContext>
{
private bool disposed = false;

public EntityFrameworkRepositoryContext(DbContext session) : base(session)
{
}
Expand All @@ -26,9 +28,15 @@ public override async Task CommitAsync(CancellationToken cancellationToken = def

protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
this.Session.Dispose();
if (disposing)
{
this.Session.Dispose();
}

disposed = true;
base.Dispose(disposing);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/Apworks/DisposableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace Apworks
/// </remarks>
public abstract class DisposableObject : IDisposable
{
/// <summary>
/// Occurs when the current object has been disposed.
/// </summary>
public event EventHandler Disposed;

/// <summary>
/// Finalizes an instance of the <see cref="DisposableObject"/> class.
/// </summary>
Expand All @@ -57,6 +62,9 @@ public void Dispose()
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected abstract void Dispose(bool disposing);
protected virtual void Dispose(bool disposing)
{
this.Disposed?.Invoke(this, EventArgs.Empty);
}
}
}
11 changes: 6 additions & 5 deletions src/Apworks/Messaging/MessageConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class MessageConsumer<TMessageSubscriber, TMessageHandler> : Dis
private readonly TMessageSubscriber subscriber;
private readonly IEnumerable<TMessageHandler> handlers;
private readonly string route;
private volatile bool disposed;
private bool disposed;

protected MessageConsumer(TMessageSubscriber subscriber, IEnumerable<TMessageHandler> handlers, string route = null)
{
Expand Down Expand Up @@ -52,19 +52,20 @@ protected virtual async void OnMessageAcknowledged(object sender, MessageAcknowl

protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
if (!disposed)
if (disposing)
{
if (this.subscriber != null)
{
this.subscriber.MessageReceived -= this.OnMessageReceived;
this.subscriber.MessageAcknowledged -= this.OnMessageAcknowledged;
this.subscriber.Dispose();
}

disposed = true;
}

disposed = true;
base.Dispose(disposing);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/Apworks/Repositories/EventPublishingDomainRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class EventPublishingDomainRepository : DomainRepository
{
#region Private Fields
private readonly IEventPublisher publisher;
private bool disposed = false;
#endregion

#region Protected Properties
Expand Down Expand Up @@ -75,9 +76,15 @@ protected EventPublishingDomainRepository(IEventPublisher publisher)
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
this.publisher?.Dispose();
if (disposing)
{
this.publisher.Dispose();
}

disposed = true;
base.Dispose(disposing);
}
}
#endregion
Expand Down
15 changes: 15 additions & 0 deletions src/Apworks/Repositories/EventSourcingDomainRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class EventSourcingDomainRepository : EventPublishingDomainReposit
private readonly IEventStore eventStore;
private readonly ISnapshotProvider snapshotProvider;
private readonly string route;
private bool disposed = false;

public EventSourcingDomainRepository(IEventStore eventStore,
IEventPublisher publisher,
Expand Down Expand Up @@ -130,5 +131,19 @@ public sealed class EventSourcingDomainRepository : EventPublishingDomainReposit
}
}
}

protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
this.eventStore.Dispose();
}

disposed = true;
base.Dispose(disposing);
}
}
}
}
6 changes: 0 additions & 6 deletions src/Apworks/Repositories/RepositoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ public virtual Task CommitAsync(CancellationToken cancellationToken = default(Ca
protected abstract IRepository<TKey, TAggregateRoot> CreateRepository<TKey, TAggregateRoot>()
where TKey : IEquatable<TKey>
where TAggregateRoot : class, IAggregateRoot<TKey>;

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing) { }
#endregion

}
Expand Down
11 changes: 9 additions & 2 deletions src/Apworks/Repositories/SimpleDomainRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Apworks.Repositories
public sealed class SimpleDomainRepository : DomainRepository
{
private readonly IRepositoryContext repositoryContext;
private bool disposed = false;

public SimpleDomainRepository(IRepositoryContext repositoryContext)
{
Expand Down Expand Up @@ -50,9 +51,15 @@ public SimpleDomainRepository(IRepositoryContext repositoryContext)

protected override void Dispose(bool disposing)
{
if (disposing)
if (!disposed)
{
this.repositoryContext?.Dispose();
if (disposing)
{
this.repositoryContext?.Dispose();
}

disposed = true;
base.Dispose(disposing);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PostgreSQLFixture
{
public static readonly object locker = new object();

public const string ConnectionString = "User ID=test;Password=oe9jaacZLbR9pN;Host=localhost;Port=5432;Database=test;";
public const string ConnectionString = "User ID=test;Password=test;Host=localhost;Port=5432;Database=test;";

private const string CreateAddressTableSql = @"CREATE TABLE ""Addresses"" (
""Id"" serial NOT NULL,
Expand Down
26 changes: 16 additions & 10 deletions tests/Apworks.Tests.Integration/Fixtures/SQLServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace Apworks.Tests.Integration.Fixtures
{
public class SQLServerFixture
{
private const string EventStoreDatabaseName = "SQLServerEventStoreTest";

public static readonly object locker = new object();
public const string ConnectionString = "Server=localhost;Database=SQLServerEventStoreTest;User Id=sa;Password=G1veMeP@ss";
public const string ConnectionStringWithoutDatabase = "Server=localhost;User Id=sa;Password=G1veMeP@ss";
public static readonly string ConnectionString = $@"Server=localhost\sqlexpress;Database={EventStoreDatabaseName};Integrated Security=SSPI;";
public const string ConnectionStringWithoutDatabase = @"Server=localhost\sqlexpress;Integrated Security=SSPI;";

private static readonly string MDF_FileName = Path.Combine(Path.GetTempPath(), EventStoreDatabaseName + ".mdf");
private static readonly string LDF_FileName = Path.Combine(Path.GetTempPath(), EventStoreDatabaseName + ".ldf");

public SQLServerFixture()
{
Expand All @@ -34,7 +40,7 @@ private static void DropDatabase()
{
using (var tmpConn = new SqlConnection(ConnectionStringWithoutDatabase))
{
var sqlCreateDBQuery = "DROP DATABASE SQLServerEventStoreTest";
var sqlCreateDBQuery = $"DROP DATABASE {EventStoreDatabaseName}";
tmpConn.Open();
tmpConn.ChangeDatabase("master");
using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
Expand All @@ -50,7 +56,7 @@ private static bool CheckDatabase()
{
using (var tmpConn = new SqlConnection(ConnectionStringWithoutDatabase))
{
var sqlCreateDBQuery = "SELECT COUNT(*) FROM sys.databases where name = 'SQLServerEventStoreTest'";
var sqlCreateDBQuery = $"SELECT COUNT(*) FROM sys.databases where name = '{EventStoreDatabaseName}'";
tmpConn.Open();
tmpConn.ChangeDatabase("master");
using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
Expand All @@ -74,7 +80,7 @@ private static bool CheckTableExists(string tableName)
using (var con = new SqlConnection(ConnectionString))
{
var sqlCheck = $@"SELECT COUNT(*)
FROM SQLServerEventStoreTest.INFORMATION_SCHEMA.TABLES
FROM {EventStoreDatabaseName}.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = '{tableName}'";
con.Open();
Expand All @@ -95,12 +101,12 @@ private static void CreateDatabase()
String str;
SqlConnection myConn = new SqlConnection(ConnectionStringWithoutDatabase);

str = "CREATE DATABASE SQLServerEventStoreTest ON PRIMARY " +
"(NAME = 'SQLServerEventStoreTest_Data', " +
"FILENAME = '/var/opt/mssql/data/SQLServerEventStoreTest.mdf', " +
str = $@"CREATE DATABASE {EventStoreDatabaseName} ON PRIMARY " +
$"(NAME = '{EventStoreDatabaseName}_Data', " +
$"FILENAME = '{MDF_FileName}', " +
"SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = 'SQLServerEventStoreTest_Log', " +
"FILENAME = '/var/opt/mssql/data/SQLServerEventStoreTest.ldf', " +
$"LOG ON (NAME = '{EventStoreDatabaseName}_Log', " +
$"FILENAME = '{LDF_FileName}', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
Expand Down

0 comments on commit e4b3878

Please sign in to comment.