Skip to content

Commit

Permalink
progress towards lazy loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
ToddThomson committed Aug 23, 2018
1 parent 9177640 commit 7a21ce8
Show file tree
Hide file tree
Showing 27 changed files with 1,819 additions and 1,648 deletions.
8 changes: 4 additions & 4 deletions Achilles.Entities.Sqlite/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class DataContext : IDisposable
private IDataContextService _contextService = null;
private IRelationalCommandBuilder _commandBuilder = null;

private List<IEntitySet> _entitySets = new List<IEntitySet>();
private Dictionary<Type, IEntitySet> _entitySets = new Dictionary<Type, IEntitySet>();

private bool _isConfiguring = false;
private bool _isModelBuilding = false;
Expand Down Expand Up @@ -325,12 +325,12 @@ private void InitializeServices( DataContextOptions options )

#region Internal Methods

internal void AddEntitySet( IEntitySet entitySet )
internal void AddEntitySet<TEntity>( EntitySet<TEntity> entitySet ) where TEntity : class
{
_entitySets.Add( entitySet );
_entitySets.Add( typeof(TEntity), entitySet );
}

internal List<IEntitySet> EntitySets => _entitySets;
internal Dictionary<Type, IEntitySet> EntitySets => _entitySets;

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Achilles.Entities
{
public sealed class EntityCollection<TEntity> : ICollection<TEntity>, IListSource, IEntityCollection
public sealed class EntityCollection<TEntity> : IEntityCollection<TEntity>, IEntityCollection, ICollection<TEntity>, IListSource
where TEntity : class
{
#region Private Fields
Expand All @@ -40,6 +40,15 @@ public EntityCollection()

#endregion

#region IEntityCollection Implementation

void IEntityCollection<TEntity>.AttachSource( IEnumerable<TEntity> source )
{
_source = source;
}

#endregion

#region ICollection<TEntity> Implementation

public int Count => ((ICollection<TEntity>)_entities).Count;
Expand Down Expand Up @@ -92,6 +101,8 @@ IList IListSource.GetList()
throw new NotImplementedException();
}



#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public TEntity Entity
}
}

public void AttachSource( IEnumerable<TEntity> source )
{
_source = source;
}

#endregion

#region Private Methods
Expand Down
7 changes: 6 additions & 1 deletion Achilles.Entities.Sqlite/EntitySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public EntitySet( DataContext context )

#region Public CRUD Methods

// TODO: Add documented user interface
// TODO: Add documented user interface on IRepository

public int Add( TEntity entity )
=> _context.Add( entity );
Expand Down Expand Up @@ -113,6 +113,11 @@ private EntityQueryable<TEntity> EntityQueryable

IEnumerator IEnumerable.GetEnumerator() => EntityQueryable.GetEnumerator();

public IEnumerable<TSource> GetSource<TSource>( TSource t ) where TSource : class
{
return (IEnumerable<TSource>)EntityQueryable.Cast<TSource>();
}

#endregion
}
}
5 changes: 4 additions & 1 deletion Achilles.Entities.Sqlite/IEntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

namespace Achilles.Entities
{
public interface IEntityCollection
/// <summary>
/// Marker interface
/// </summary>
internal interface IEntityCollection
{
}
}
23 changes: 23 additions & 0 deletions Achilles.Entities.Sqlite/IEntityCollectionOfT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#region Copyright Notice

// Copyright (c) by Achilles Software, All rights reserved.
//
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// Send questions regarding this copyright notice to: mailto:[email protected]

#endregion

#region Namespaces

using System.Collections.Generic;

#endregion

namespace Achilles.Entities
{
internal interface IEntityCollection<TEntity>
{
void AttachSource( IEnumerable<TEntity> source );
}
}
3 changes: 3 additions & 0 deletions Achilles.Entities.Sqlite/IEntityReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

#endregion

using System.Collections.Generic;

namespace Achilles.Entities
{
/// <summary>
/// Marker interface
/// </summary>
internal interface IEntityReference
{
//void AttachSource<TEntityRef>( IEnumerable<TEntityRef> source ) where TEntityRef: class;
}
}
5 changes: 4 additions & 1 deletion Achilles.Entities.Sqlite/IEntitySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#region Namespaces

using System;
using System.Collections.Generic;

#endregion

Expand All @@ -20,12 +21,14 @@ namespace Achilles.Entities
/// Internal API for <see cref="EntitySet{TEntity}"/>
/// </summary>
/// <remarks>An internal interface for now.</remarks>
internal interface IEntitySet
public interface IEntitySet
{
/// <summary>
/// Returns the entity set generic type.
/// </summary>
/// <returns></returns>
Type EntityType { get; }

IEnumerable<TSource> GetSource<TSource>( TSource source ) where TSource : class;
}
}
2 changes: 1 addition & 1 deletion Achilles.Entities.Sqlite/Linq/EntityQueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public EntityQueryExecutor( DataContext context )
_context = context;
_connection = context.Database.Connection;
//_transaction = transaction;
_materializer = new EntityMaterializer( context.Model );
_materializer = new EntityMaterializer( context );
}

#endregion
Expand Down
24 changes: 12 additions & 12 deletions Achilles.Entities.Sqlite/Modelling/EntityModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ public EntityModel( EntityMappingCollection entityMappings )

public IReadOnlyCollection<IEntityMapping> EntityMappings => _entityMappings.Values as IReadOnlyCollection<IEntityMapping>;

public IEntityMapping GetOrAddEntityMapping( Type entityType )
{
IEntityMapping mapping;
//public IEntityMapping GetOrAddEntityMapping( Type entityType )
//{
// IEntityMapping mapping;

if ( !_entityMappings.TryGetValue( entityType, out mapping ) )
{
var EntityMappingType = typeof( EntityMapping<> );
var mapType = EntityMappingType.MakeGenericType( entityType );
// if ( !_entityMappings.TryGetValue( entityType, out mapping ) )
// {
// var EntityMappingType = typeof( EntityMapping<> );
// var mapType = EntityMappingType.MakeGenericType( entityType );

mapping = Activator.CreateInstance( mapType ) as IEntityMapping;
// mapping = Activator.CreateInstance( mapType ) as IEntityMapping;

_entityMappings[ entityType ] = mapping;
}
// _entityMappings[ entityType ] = mapping;
// }

return mapping;
}
// return mapping;
//}

public IEntityMapping GetEntityMapping<TEntity>() where TEntity : class
{
Expand Down
4 changes: 2 additions & 2 deletions Achilles.Entities.Sqlite/Modelling/EntityModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public IEntityModel Build( DataContext context )
// What should happen if an entity is referenced during model building that is
// not in the context?

foreach ( var entitySet in context.EntitySets )
foreach ( var entitySetType in context.EntitySets.Keys )
{
EntityMappings.GetOrAddEntityMapping( entitySet.EntityType );
EntityMappings.GetOrAddEntityMapping( entitySetType );
}

context.OnModelBuilding( this );
Expand Down
Loading

0 comments on commit 7a21ce8

Please sign in to comment.