Skip to content

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

@jeffward01

Description

@jeffward01

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

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions