TanvirArjel / EFCore.GenericRepository

This repository contains Generic Repository implementation for Entity Framework Core
MIT License
600 stars 89 forks source link

Returning types should be changed? #19

Closed HybridSolutions closed 2 years ago

HybridSolutions commented 2 years ago

HI once again,

I decided to open a new Issue since this is another matter.

The InsertAsync method returns an object[] type but I wonder why you decided to hide the default EntityEntry return type. If the goal is to simplify and just return the primary key, then this could be done without hiding EntityEntry by using the following definition:

public async Task<(EntityEntry entity, object[] primaryKey)> InsertAsync<T>(T entity, CancellationToken cancellationToken = default)
           where T : class
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            EntityEntry<T> entityEntry = await _dbContext.Set<T>().AddAsync(entity, cancellationToken).ConfigureAwait(false);
            await _dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

            object[] primaryKeyValue = entityEntry.Metadata.FindPrimaryKey().Properties.
                Select(p => entityEntry.Property(p.Name).CurrentValue).ToArray();

            return (entityEntry, primaryKeyValue);
        }

What do you think of this?

TanvirArjel commented 2 years ago

EntityEntry is nothing but the object they are passing for the insert operation. So returning to the same tracked entity does not make sense.