Exemplo n.º 1
0
        static object InvokeWithNullCheck <T>(Expression <Func <Root, T> > exp, Func <Root> setup = null)
        {
            setup = setup ?? (() => new Root());
            var nullChecked = new NullCheckInjector().Visit(exp);

            return(((Expression <Func <Root, object> >)nullChecked).Compile()(setup()));
        }
Exemplo n.º 2
0
        static bool CanItBeTrustedToNeverBeNull <T>(Expression <Func <Root, T> > exp)
        {
            var nullCheckInjector = new NullCheckInjector();

            nullCheckInjector.Visit(exp);
            return(nullCheckInjector.CanBeTrustedToNeverReturnNull);
        }
 public void Fails()
 {
     Expression<Func<Case, long>> test = x => x.Submission.EnergyLabelSerialIdentifier;
     var expression = new NullCheckInjector().Visit(test);
     var nullsafe = (Expression<Func<Case, object>>)expression;
     Should.NotThrow(() => nullsafe.Compile()(new Case()));
 }
        public void Fails()
        {
            Expression <Func <Case, long> > test = x => x.Submission.EnergyLabelSerialIdentifier;
            var expression = new NullCheckInjector().Visit(test);
            var nullsafe   = (Expression <Func <Case, object> >)expression;

            Should.NotThrow(() => nullsafe.Compile()(new Case()));
        }
Exemplo n.º 5
0
        public DocumentDesigner <TEntity> With <TMember>(string name, Expression <Func <TEntity, TMember> > projector, params Option[] options)
        {
            if (typeof(TMember) == typeof(string))
            {
                options = options.Concat(new MaxLength(1024)).ToArray();
            }

            var column = design.Table[name];

            if (design.Table.IdColumn.Equals(column))
            {
                throw new ArgumentException("You can not make a projection for IdColumn. Use Document.Key() method instead.");
            }

            if (column == null)
            {
                var lengthOption = options
                                   .OfType <MaxLength>()
                                   .FirstOrDefault();

                column = new Column(name, typeof(TMember), lengthOption?.Length);
                design.Table.Register(column);
            }

            Func <object, object> compiledProjector;

            if (!options.OfType <DisableNullCheckInjection>().Any())
            {
                var nullCheckInjector    = new NullCheckInjector();
                var nullCheckedProjector = (Expression <Func <TEntity, object> >)nullCheckInjector.Visit(projector);

                if (!nullCheckInjector.CanBeTrustedToNeverReturnNull && !column.IsPrimaryKey)
                {
                    column.Nullable = true;
                }

                compiledProjector = Compile(name, nullCheckedProjector);
            }
            else
            {
                compiledProjector = Compile(name, projector);
            }

            var newProjection = Projection.From <TMember>(managedEntity => compiledProjector(managedEntity.Entity));

            if (!newProjection.ReturnType.IsCastableTo(column.Type))
            {
                throw new InvalidOperationException(
                          $"Can not override projection for {name} of type {column.Type} " +
                          $"with a projection that returns {newProjection.ReturnType} (on {typeof (TEntity)}).");
            }

            Projection existingProjection;

            if (!design.Projections.TryGetValue(name, out existingProjection))
            {
                if (design.Parent != null && !column.IsPrimaryKey)
                {
                    column.Nullable = true;
                }

                design.Projections.Add(column, newProjection);
            }
            else
            {
                design.Projections[name] = newProjection;
            }

            return(this);
        }