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

Made FluidityRepository disposable #50

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/Fluidity/Data/DefaultFluidityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,10 @@ public long GetTotalRecordCount(bool fireEvents = true)

return Db.ExecuteScalar<long>(query);
}

public void Dispose()
{
//No disposable resources
}
}
}
7 changes: 6 additions & 1 deletion src/Fluidity/Data/FluidityRepository`T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Fluidity.Data
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TId">The type of the identifier.</typeparam>
/// <seealso cref="Fluidity.Data.IFluidityRepository" />
public abstract class FluidityRepository<TEntity, TId> : IFluidityRepository
public abstract class FluidityRepository<TEntity, TId> : IFluidityRepository, IDisposable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is IDisposable necesarry here as IFluidityRepository implement IDisposable anyway?

Copy link
Contributor Author

@drpeck drpeck May 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not. It was only the ObsoleteAttribute on the interface that caused me to add it here, for fear that if the interface was removed without IDisposable being moved over to the abstract class. It sounds like this is unnecessary duplication though so I'll remove it. 2 secs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, IFluidityRepository whilst marked as obsolete, won't be removed. It's purely to stop it being used directly.

{
public virtual Type EntityType => typeof(TEntity);
public virtual Type IdType => typeof(TId);
Expand Down Expand Up @@ -206,5 +206,10 @@ long IFluidityRepository.GetTotalRecordCount(bool fireEvents)
}

#endregion

public virtual void Dispose()
{
//No resources to dispose of by default
}
}
}
2 changes: 1 addition & 1 deletion src/Fluidity/Data/IFluidityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Fluidity.Data
{
[Obsolete("Use the abstract class FluidityRepository<TEntity, TId> instead")]
public interface IFluidityRepository
public interface IFluidityRepository : IDisposable
{
Type EntityType { get; }

Expand Down
61 changes: 37 additions & 24 deletions src/Fluidity/Services/FluidityEntityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public FluidityCollectionDisplayModel GetCollectionDisplayModel(FluiditySectionC
}

public PagedResult<FluidityEntityDisplayModel> GetEntityDisplayModels(FluiditySectionConfig section, FluidityCollectionConfig collection, int pageNumber = 1, int pageSize = 10, string query = null, string orderBy = null, string orderDirection = null, string dataView = null)
{
var repo = RepositoryFactory.GetRepository(collection);

{
// Construct where clause
LambdaExpression whereClauseExp = null;

Expand Down Expand Up @@ -118,8 +116,12 @@ public PagedResult<FluidityEntityDisplayModel> GetEntityDisplayModels(FluiditySe
? orderDirection.InvariantEquals("asc") ? SortDirection.Ascending : SortDirection.Descending
: collection.SortDirection;

// Perform the query
var result = repo?.GetPaged(pageNumber, pageSize, whereClauseExp, orderByExp, orderDir);
PagedResult<object> result;
using (var repo = RepositoryFactory.GetRepository(collection))
{
// Perform the query
result = repo?.GetPaged(pageNumber, pageSize, whereClauseExp, orderByExp, orderDir);
}

// If we've got no results, return an empty result set
if (result == null)
Expand Down Expand Up @@ -147,8 +149,6 @@ public IEnumerable<FluidityEntityDisplayModel> GetEntityDisplayModelsByIds(Fluid

public IEnumerable<object> GetEntitiesByIds(FluiditySectionConfig section, FluidityCollectionConfig collection, object[] ids)
{
var repo = RepositoryFactory.GetRepository(collection);

// Construct where clause
LambdaExpression whereClauseExp = null;

Expand All @@ -172,20 +172,25 @@ public IEnumerable<object> GetEntitiesByIds(FluiditySectionConfig section, Fluid
}

// Perform the query
var result = repo?.GetPaged(1, ids.Length, whereClauseExp, null, SortDirection.Ascending);
PagedResult<object> result;
using (var repo = RepositoryFactory.GetRepository(collection))
{
result = repo?.GetPaged(1, ids.Length, whereClauseExp, null, SortDirection.Ascending);
}

// Return the results
return result?.Items;
}

public FluidityEntityEditModel GetEntityEditModel(FluiditySectionConfig section, FluidityCollectionConfig collection, object entityOrId = null)
{
var repo = RepositoryFactory.GetRepository(collection);

{
object entity = null;
if (entityOrId != null)
{
entity = (entityOrId.GetType() == collection.EntityType ? entityOrId : null) ?? repo.Get(entityOrId);
using (var repo = RepositoryFactory.GetRepository(collection))
{
entity = (entityOrId.GetType() == collection.EntityType ? entityOrId : null) ?? repo.Get(entityOrId);
}
}

var mapper = new FluidityEntityMapper();
Expand All @@ -201,21 +206,22 @@ public object NewEntity(FluidityCollectionConfig collection)

public object GetEntity(FluidityCollectionConfig collection, object id)
{
var repo = RepositoryFactory.GetRepository(collection);

return repo?.Get(id);
using (var repo = RepositoryFactory.GetRepository(collection))
{
return repo?.Get(id);
}
}

public IEnumerable<object> GetAllEntities(FluidityCollectionConfig collection)
{
var repo = RepositoryFactory.GetRepository(collection);

return repo?.GetAll();
using (var repo = RepositoryFactory.GetRepository(collection))
{
return repo?.GetAll();
}
}

public object SaveEntity(FluidityCollectionConfig collection, object entity)
{
var repo = RepositoryFactory.GetRepository(collection);
var isNew = entity.GetPropertyValue(collection.IdProperty).Equals(collection.IdProperty.Type.GetDefaultValue());

if (isNew && collection.DateCreatedProperty != null)
Expand All @@ -228,21 +234,28 @@ public object SaveEntity(FluidityCollectionConfig collection, object entity)
entity.SetPropertyValue(collection.DateModifiedProperty, DateTime.Now);
}

repo?.Save(entity);
using (var repo = RepositoryFactory.GetRepository(collection))
{
repo?.Save(entity);
}

return entity;
}

public void DeleteEntity(FluidityCollectionConfig collection, object id)
{
var repo = RepositoryFactory.GetRepository(collection);
repo?.Delete(id);
using (var repo = RepositoryFactory.GetRepository(collection))
{
repo?.Delete(id);
}
}

public long GetsEntityTotalRecordCount(FluidityCollectionConfig collection)
{
var repo = RepositoryFactory.GetRepository(collection);
return repo?.GetTotalRecordCount() ?? 0;
using (var repo = RepositoryFactory.GetRepository(collection))
{
return repo?.GetTotalRecordCount() ?? 0;
}
}
}
}