-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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:
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 forEasyRepository
such asEasyRepositoryPrimaryKeyNotFoundException
, 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 aNullReferenceTypeException
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
Assignees
Labels
enhancementNew feature or requestNew feature or request