Hi,
Runtime Framework 5.7 (5.7.1) RTM with adapter, under dot net 4.8.
I would like to have a oneliner for adding child entities to an EntityCollection without side effects.
I don't use databinding anywhere for my entities, they are used server side only.
I have this pattern where I create a new Entity, add it to an EntityCollection of an existing entity and return it for further use, like:
public Child CreateChild(Parent parent)
{
var child = new Child();
parent.Children.Add(child);
return child;
}
This seems very generic and I found a method on EntityCollectionBase2<TEntity> which does this in a way, but it is supposed to be used only for databinding purposes:
Do not call this method from your own code. This is a databinding ONLY method.
Looking at the code it shows among others:
TEntity entity = (TEntity) this._entityFactoryToUse.Create();
Is this the same as 'new Child()' but then in a generic way? Or does it have special behaviours?
I can't use this in an extension method because it is private to EntityCollectionBase2<TEntity>.
I could create a generic extension method like:
public static TEntity AddNewEntity<TEntity>(this EntityCollectionBase2<TEntity> collection) where TEntity : EntityBase2, IEntity2
{
if (collection == null) throw new ArgumentNullException(nameof(collection));
var item = Activator.CreateInstance<TEntity>();
collection.Add(item);
return item;
}
Is this the way, or do I have to account for the entityfactory?
Best Puser