EasyRepository.EFCore icon indicating copy to clipboard operation
EasyRepository.EFCore copied to clipboard

✨ [FEATURE] Enable Nullable Reference Types, add null exceptions, add null checks

Open jeffward01 opened this issue 3 years ago • 2 comments

Summary

Basically this Feature request is to enable: Nullable Reference Types

If approved, I can implement this change

Advantage

In favor of being explicit, nullable reference types will highlight and throw errors when null is returned when it is not expected. Such as:

image

    private Expression<Func<TEntity, bool>> GenerateExpression<TEntity>(object id)
    {
        
        // Type might be null
        var type = this._context.Model.FindEntityType(typeof(TEntity));
        
        
        // Type might be null.. If this is not 'caught' then it will throw NullReference Error.
        // Instead... We can 'catch' the Null Reference Error and then throw an error such as:
        // EasyRepositoryTypeNotFoundException : Exception
        // Message: The Type TypeOf(TEntity).Name was not found, please ensure it exists within the DBContext model as a registered
        // DbSet<TEntity>.
        //---- This is a change in favor of being 'explicit'.
        var pk = type.FindPrimaryKey()
            .Properties.Select(s => s.Name)
            .FirstOrDefault();
        
        
        // Type might also be null here
        var pkType = type.FindPrimaryKey()
            .Properties.Select(p => p.ClrType)
            .FirstOrDefault();

        // PkType might be null here also
        var value = Convert.ChangeType(id, pkType, CultureInfo.InvariantCulture);

        var pe = Expression.Parameter(typeof(TEntity), "entity");
        var me = Expression.Property(pe, pk);
        var constant = Expression.Constant(value, pkType);
        var body = Expression.Equal(me, constant);
        var expression = Expression.Lambda<Func<TEntity, bool>>(body, pe);

        return expression;
    }

Further Reading of advantages:

Proposed Changes

  • Enable Nullable Reference Types
  • Add EasyRepositoryTypeNullException and other exception types for EasyRepository such as EasyRepositoryPrimaryKeyNotFoundException, and others
  • Add Null checks for when null can be returned, yet, EasyRepository does not expect null to be returned. Then we would throw an exception instead of .NET xxx throwing a NullReferenceTypeException

Additional context

Personally, I like to be explicit in code, it helps prevent and diagnose errors. I think Nullable Reference types will lower the likely-hood of future bugs, and also help the developer user (consumer of EasyRepository library) diagnose bugs.

I can add this feature if approved

jeffward01 avatar Oct 01 '22 05:10 jeffward01

Of course, exceptions should be clear and unambiguous. In Open Source projects, development should usually be done on request or problem. So far there has been no problem with exception handling. You mentioned this problem first. So let's add it. :D @jeffward01

furkandeveloper avatar Oct 01 '22 11:10 furkandeveloper

Open Source projects, development should usually be done on request or problem

That is great logic, I will remember this phrase, thank you!


Sounds good! I will get this added after we get https://github.com/furkandeveloper/EasyRepository.EFCore/pull/19 wrapped up

jeffward01 avatar Oct 01 '22 18:10 jeffward01