EntityCollection AddNew without databinding

Posts   
 
    
Puser
User
Posts: 228
Joined: 20-Sep-2012
# Posted on: 24-Dec-2021 12:01:58   

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

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 24-Dec-2021 12:31:04   

It's better to use the factory if you have an entity type that's abstract (e.g. a supertype in an inheritance hierarchy).

So

public static TEntity AddNewEntity<TEntity>(this EntityCollectionBase2<TEntity> collection) where TEntity : EntityBase2, IEntity2
{
    if (collection == null) throw new ArgumentNullException(nameof(collection));
    var item = collection.EntityFactoryToUse == null ? Activator.CreateInstance<TEntity>() : collection.EntityFactoryToUse.Create();
    collection.Add(item);
    return item;
}

should do it

Frans Bouma | Lead developer LLBLGen Pro
Puser
User
Posts: 228
Joined: 20-Sep-2012
# Posted on: 24-Dec-2021 12:38:46   

ah, so close... didn't notice the public EntityFactoryToUse.

Thank you Frans!

Wishing you nice days

Puser
User
Posts: 228
Joined: 20-Sep-2012
# Posted on: 24-Dec-2021 12:41:45   

Small adjustment, cast IEntity2 to TEntity from: collection.EntityFactoryToUse.Create()

    public static TEntity AddNewEntity<TEntity>(this EntityCollectionBase2<TEntity> collection) where TEntity : EntityBase2, IEntity2
        {
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            var item = collection.EntityFactoryToUse == null ? Activator.CreateInstance<TEntity>() : (TEntity)collection.EntityFactoryToUse.Create();
            collection.Add(item);
            return item;
        }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 24-Dec-2021 15:12:23   

Ah yes, I missed that flushed simple_smile Happy holidays to you too!

Frans Bouma | Lead developer LLBLGen Pro