Skip to content

Commit

Permalink
EntityCollection should implement ICollection<> (#10)
Browse files Browse the repository at this point in the history
* Have IEntityCollection implement ICollection<> 
* Document some public members
* Do som additional code cleanup (?.Invoke)
* Make some codegen methods public
* remove m2m version of IEntityCollection and use version from OpenRiaServices.Client 4.6.1
  • Loading branch information
Daniel-Svensson authored Jun 28, 2019
1 parent 098821a commit d58065c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 108 deletions.
87 changes: 64 additions & 23 deletions OpenRiaServices.M2M.Silverlight/EntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,84 +55,125 @@ public EntityCollection(

private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(PropertyChanged != null)
{
PropertyChanged(this, e);
}
PropertyChanged?.Invoke(this, e);
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(CollectionChanged == null) return;
if (CollectionChanged == null) return;
// Checkout http://m2m4ria.codeplex.com/wikipage?title=CreatingAnM2MAssociationBetweenTwoNewEntities
if(!(e.Action == NotifyCollectionChangedAction.Reset && _collection.Select(_getEntity).Any(x => x == null)))
if (!(e.Action == NotifyCollectionChangedAction.Reset && _collection.Select(_getEntity).Any(x => x == null)))
{
CollectionChanged(this, MakeNotifyCollectionChangedEventArgs(e));
}
}

private void OnEntityRemoved(object sender, EntityCollectionChangedEventArgs<TLinkTable> e)
{
if(EntityRemoved != null)
{
EntityRemoved(this, new EntityCollectionChangedEventArgs<TEntity>(_getEntity(e.Entity)));
}
EntityRemoved?.Invoke(this, new EntityCollectionChangedEventArgs<TEntity>(_getEntity(e.Entity)));
}

private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<TLinkTable> e)
{
if(EntityAdded != null)
{
EntityAdded(this, new EntityCollectionChangedEventArgs<TEntity>(_getEntity(e.Entity)));
}
EntityAdded?.Invoke(this, new EntityCollectionChangedEventArgs<TEntity>(_getEntity(e.Entity)));
}

#endregion

#region Public Events

/// <summary>
/// Occurs when the items list of the collection has changed, or the collection is reset.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;

/// <summary>
/// Event raised whenever an <see cref="Entity"/>
/// is added to this collection
/// </summary>
public event EventHandler<EntityCollectionChangedEventArgs<TEntity>> EntityAdded;

/// <summary>
/// Event raised whenever an <see cref="Entity"/>
/// is removed from this collection
/// </summary>
public event EventHandler<EntityCollectionChangedEventArgs<TEntity>> EntityRemoved;

/// <summary>
/// Event raised when a property such as the <see cref="Count"/> has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

#endregion

#region Public Properties

public int Count
{
get { return _collection.Count; }
}
/// <summary>
/// Gets the current count of entities in this collection
/// </summary>
public int Count => _collection.Count;

bool ICollection<TEntity>.IsReadOnly => ((ICollection<TLinkTable>)_collection).IsReadOnly;

#endregion

#region Public Methods and Operators

/// <summary>
/// Add the specified entity to this collection. If the entity is unattached,
/// it will be added to its System.ServiceModel.DomainServices.Client.EntitySet
/// automatically.
/// </summary>
/// <param name="entity"> The entity to add </param>
public void Add(TEntity entity)
{
_addAction(entity);
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>A <see cref="IEnumerator{TEntity}" /> that can be used to iterate through the collection.</returns>
public IEnumerator<TEntity> GetEnumerator()
{
return _collection.Select(_getEntity).GetEnumerator();
}

/// <summary>
/// Removes an m2m relation with the given entity.
/// Remove the specified entity from this collection.
/// </summary>
/// <param name="entity"> </param>
public void Remove(TEntity entity)
/// <param name="entity"> The entity to remove </param>
/// <returns><c>true</c> if an item was removed</returns>
public bool Remove(TEntity entity)
{
var linkTableEntityToRemove = _collection.SingleOrDefault(jt => _getEntity(jt) == entity);
if(linkTableEntityToRemove != null)
if (linkTableEntityToRemove != null)
{
_removeAction(linkTableEntityToRemove);
return true;
}
return false;
}

/// <summary>
/// Removes all items from the collection
/// </summary>
public void Clear()
{
((ICollection<TLinkTable>)_collection).Clear();
}

bool ICollection<TEntity>.Contains(TEntity item)
{
return ((IEnumerable<TEntity>)this).Contains(item);
}

void ICollection<TEntity>.CopyTo(TEntity[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException(nameof(array));

foreach (TEntity entity in this)
array[arrayIndex++] = entity;
}

#endregion
Expand All @@ -156,7 +197,7 @@ IEnumerator IEnumerable.GetEnumerator()
private NotifyCollectionChangedEventArgs MakeNotifyCollectionChangedEventArgs(
NotifyCollectionChangedEventArgs e)
{
switch(e.Action)
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
Expand Down
58 changes: 0 additions & 58 deletions OpenRiaServices.M2M.Silverlight/IEntityCollection.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenRiaServices.Client.Core" Version="4.6.0" />
<PackageReference Include="OpenRiaServices.Client.Core" Version="4.6.1" />
</ItemGroup>
</Project>

This file was deleted.

21 changes: 11 additions & 10 deletions OpenRiaServices.M2M/EntityGenerator/M2M4RiaEntityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static IEnumerable<PropertyDescriptor> GetNavigationProperties(Type type
return props.Where(p => p.Attributes.OfType<AssociationAttribute>().Any());
}

private static bool IsLinkTableEntity(Type type)
protected static bool IsLinkTableEntity(Type type)
{
// Find generic base type
while(type != null && type.IsGenericType == false)
Expand All @@ -79,7 +79,7 @@ private static string ToLowerInitial(string s)
return s[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant() + s.Substring(1);
}

private void AddAttachMethod(Type linkTableType, AssociationAttribute assocAttrThisEnd, AssociationAttribute assocAttrOtherEnd, PropertyDescriptor prop1, PropertyDescriptor prop2)
public void AddAttachMethod(Type linkTableType, AssociationAttribute assocAttrThisEnd, AssociationAttribute assocAttrOtherEnd, PropertyDescriptor prop1, PropertyDescriptor prop2)
{
var linkTableFullTypeName = linkTableType.FullName;
var navProp1TypeFullName = prop1.PropertyType.FullName;
Expand Down Expand Up @@ -136,7 +136,7 @@ private void AddAttachMethod(Type linkTableType, AssociationAttribute assocAttrT
WriteLine(@"#endregion");
}

private void AddAttachMethodsToLinkTableEntity(Type type)
public void AddAttachMethodsToLinkTableEntity(Type type)
{
if(!IsLinkTableEntity(type)) return;
var propertyDescriptors = GetNavigationProperties(type).ToList();
Expand All @@ -153,7 +153,7 @@ private void AddAttachMethodsToLinkTableEntity(Type type)
AddAttachMethod(type, assocAttr0, assocAttr1, propertyDescriptors[1], propertyDescriptors[0]);
}

private void AddIExtendedEntityImplementation(Type type)
public void AddIExtendedEntityImplementation(Type type)
{
if(!IsLinkTableEntity(type)) return;
WriteLine(@"#region Lines added by m2m4ria code generator");
Expand All @@ -171,7 +171,7 @@ private void AddIExtendedEntityImplementation(Type type)
WriteLine(@"#endregion");
}

private void AddInsertEntityMethod(Type type)
public void AddInsertEntityMethod(Type type)
{
foreach(var propertyDescriptor in GetLinkTableViewProperties(type))
{
Expand Down Expand Up @@ -201,7 +201,7 @@ private void AddInsertEntityMethod(Type type)
}
}

private void AddM2MProperties(Type type)
public void AddM2MProperties(Type type)
{
foreach(var propertyDescriptor in GetLinkTableViewProperties(type))
{
Expand All @@ -227,13 +227,14 @@ private void AddM2MProperties(Type type)
linkTableTypeFullName,
type.FullName,
elementTypeFullName);

WriteLine(@"//");
WriteLine(
@"private OpenRiaServices.M2M.IEntityCollection<{0}> _{1};", elementTypeFullName, m2mPropertyNameLower);
@"private OpenRiaServices.DomainServices.Client.IEntityCollection<{0}> _{1};", elementTypeFullName, m2mPropertyNameLower);
WriteLine(@"/// <summary>");
WriteLine(@"/// Gets the collection of associated <see cref=""{0}]""/> entities.", elementTypeFullName);
WriteLine(@"/// </summary>");
WriteLine(@"public OpenRiaServices.M2M.IEntityCollection<{0}> {1}", elementTypeFullName, m2mPropertyName);
WriteLine(@"public OpenRiaServices.DomainServices.Client.IEntityCollection<{0}> {1}", elementTypeFullName, m2mPropertyName);
WriteLine(@"{");
WriteLine(@" get");
WriteLine(@" {");
Expand All @@ -257,7 +258,7 @@ private void AddM2MProperties(Type type)
}
}

private void AddRemoveEntityMethod(Type type)
public void AddRemoveEntityMethod(Type type)
{
foreach (var propertyDescriptor in GetLinkTableViewProperties(type))
{
Expand All @@ -282,7 +283,7 @@ private void AddRemoveEntityMethod(Type type)
}
}

private void MakeLinkTableEntityAnIExtendedEntity(Type type)
public void MakeLinkTableEntityAnIExtendedEntity(Type type)
{
if(!IsLinkTableEntity(type)) return;
WriteLine(@"#region Lines added by m2m4ria code generator");
Expand Down

0 comments on commit d58065c

Please sign in to comment.