Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Signum Framework Tutorials Part 2 – Southwind Logic

, 15 Nov 2012 LGPL3
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
Northwind.zip
Northwind.bak
SignumFramework2.0-src.zip
signumframework-signumframework-c6436e1
.gitignore
Local.testsettings
README.md
Signum.Engine
ClassDiagram1.cd
Connection
DynamicQuery
Engine
Linq
ExpressionVisitor
Meta
Properties
Schema
Schema.cd
SchemaBuilder
SignumFramework.pfx
Signum.Entities
Attributes.cd
Basics
ClassDiagram1.cd
Diagram.cd
DynamicQuery
Tokens
Patterns
Properties
Reflection
Services
SignumFramework.pfx
Signum.Framework.vsmdi
Signum.Test
ClassDiagram1.cd
LinqProvider
Properties
Settings.settings
Signum.Utilities
DataStructures
IntervalDictionaries
ExpressionTrees
Properties
Reflection
SignumFramework.pfx
Synchronization
Signum.Web
ButtonBar
ControllerAttributes
Facades
HtmlHelpers
JSRenderer
LineHelpers
Lines
Mappings
PortableAreas
Repositories
Properties
Signum
Content
Controllers
Images
asc.gif
calendar.png
desc.gif
lineButtons.gif
sign_error.gif
Models
Scripts
Views
Widgets
SignumFramework.pfx
TypeContext
Widgets
Signum.Windows
Basics
Controls
ColorUtils
Converters
DynamicQuery
EntityControls
Facades
Fluent
Images
admin.png
alert.png
calendar.png
cancel.png
close.png
entity.png
find.png
forbidden.png
newColumn.png
newFilter.png
note.png
ok.png
pin.png
reload.png
save.png
star.png
Localization
MarkupExtensions
Properties
SignumFramework.pfx
Themes
Utilities
Widgets
SignumFrameworkSetup
SignumFrameworkSetup.vdproj
logo.ico
signumFramework2.0.jpg
Snippets
DroidSansMono.ttf
RazorSingleFileGenerator.vsix
Signum.Fonts.vssettings
T4template
entity.snippet
entityWithName.snippet
expressionMethod.snippet
expressionMethodQuery.snippet
expressionProperty.snippet
field.snippet
fieldCreacion.snippet
fieldEuro.snippet
fieldInicioFin.snippet
fieldString.snippet
fiff.snippet
propiedad.snippet
tran.snippet
Template
EntityControlTemplate
MyTemplate.vstemplate
SignumEntityControl.zip
__TemplateIcon.ico
Template
MyTemplate.vstemplate
ProjectName.Entities
MyTemplate.vstemplate
Properties
__TemplateIcon.ico
ProjectName.Load
MyTemplate.vstemplate
Properties
Settings.settings
__TemplateIcon.ico
ProjectName.Logic
MyTemplate.vstemplate
Properties
__TemplateIcon.ico
ProjectName.Web
Code
Content
smoothness
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
MyTemplate.vstemplate
Properties
Settings.settings
Scripts
ServerCapri.svc
Views
Capri
Home
Shared
__TemplateIcon.ico
favicon.ico
ProjectName.Windows
Controls
MyTemplate.vstemplate
Properties
Settings.settings
__TemplateIcon.ico
SignumFramework2ClientServer.zip
__TemplateIcon.ico
WizardProjectName
Test
Properties
Settings.settings
WizardProjectName
Isolated
Properties
SignumFramework.pfx
TraceAndTestImpact.testsettings
SignumFramework2Setup.zip
SignumFramework2Setup.msi
Southwind-Part2-Logic.zip
.gitignore
README.md
Southwind.vssscc
Southwind
Southwind.Entities
Properties
Southwind.Load
Properties
Settings.settings
Southwind.Logic
Properties
Southwind.Web
Code
Content
smoothness
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
Properties
Settings.settings
Scripts
ServerSouthwind.svc
Views
Home
Shared
Southwind
favicon.ico
Southwind.Windows
Controls
Properties
Settings.settings
Southwind.zip
Southwind.bak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Signum.Utilities.Reflection;
using System.Linq.Expressions;
using System.Reflection;
using Signum.Utilities;
using Signum.Engine.Maps;
using Signum.Entities;
using System.Data.SqlTypes;

namespace Signum.Engine
{
    public class FieldReader
    {
        SqlDataReader reader;
        TypeCode[] typeCodes;

        private const TypeCode tcGuid = (TypeCode)20;
        private const TypeCode tcTimeSpan = (TypeCode)21;
        private const TypeCode tcDateTimeOffset = (TypeCode)22;

        public int LastOrdinal;

        TypeCode GetTypeCode(int ordinal)
        {
            Type type = reader.GetFieldType(ordinal);
            TypeCode tc = Type.GetTypeCode(type);
            if (tc == TypeCode.Object)
            {
                if (type == typeof(Guid))
                    tc = tcGuid;

                if (type == typeof(TimeSpan))
                    tc = tcTimeSpan;

                if (type == typeof(DateTimeOffset))
                    tc = tcDateTimeOffset;
            }
            return tc;
        }

        public FieldReader(SqlDataReader reader)
        {
            this.reader = reader;

            this.typeCodes = new TypeCode[reader.FieldCount];
            for (int i = 0; i < typeCodes.Length; i++)
                typeCodes[i] = GetTypeCode(i);
        }

        public bool IsNull(int ordinal)
        {
            LastOrdinal = ordinal;
            return reader.IsDBNull(ordinal);
        }

        public string GetString(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }

            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return reader.GetByte(ordinal).ToString();
                case TypeCode.Int16:
                    return reader.GetInt16(ordinal).ToString();
                case TypeCode.Int32:
                    return reader.GetInt32(ordinal).ToString();
                case TypeCode.Int64:
                    return reader.GetInt64(ordinal).ToString();
                case TypeCode.Double:
                    return reader.GetDouble(ordinal).ToString();
                case TypeCode.Single:
                    return reader.GetFloat(ordinal).ToString();
                case TypeCode.Decimal:
                    return reader.GetDecimal(ordinal).ToString();
                case TypeCode.DateTime:
                    return reader.GetDateTime(ordinal).ToString();
                case tcGuid:
                    return reader.GetGuid(ordinal).ToString();
                case TypeCode.String:
                    return reader.GetString(ordinal);
                default:
                    return reader.GetValue(ordinal).ToString();
            }
        }

        public byte[] GetByteArray(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }

            return (byte[])reader.GetValue(ordinal);
        }

        public bool GetBoolean(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Boolean:
                    return reader.GetBoolean(ordinal);
                case TypeCode.Byte:
                    return reader.GetByte(ordinal) != 0;
                case TypeCode.Int16:
                    return reader.GetInt16(ordinal) != 0;
                case TypeCode.Int32:
                    return reader.GetInt32(ordinal) != 0;
                case TypeCode.Int64:
                    return reader.GetInt64(ordinal) != 0;
                case TypeCode.String:
                    return bool.Parse(reader.GetString(ordinal));
                default:
                    return ReflectionTools.ChangeType<bool>(reader.GetValue(ordinal));
            }
        }

        public bool? GetNullableBoolean(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetBoolean(ordinal);
        }


        public Byte GetByte(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Byte)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Byte)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Byte)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Byte)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Byte)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Byte)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Byte>(reader.GetValue(ordinal));
            }
        }

        public Byte? GetNullableByte(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetByte(ordinal); 
        }


        public Char GetChar(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Char)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Char)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Char)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Char)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Char)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Char)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Char)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Char>(reader.GetValue(ordinal));
            }
        }

        public Char? GetNullableChar(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetChar(ordinal); 
        }


        public Single GetFloat(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Single)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Single)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Single)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Single)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Single)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Single)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Single>(reader.GetValue(ordinal));
            }
        }

        public Single? GetNullableFloat(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetFloat(ordinal);
        }


        public Double GetDouble(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Double)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Double)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Double)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Double)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Double)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Double)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Double>(reader.GetValue(ordinal));
            }
        }

        public Double? GetNullableDouble(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetDouble(ordinal);
        }


        public Decimal GetDecimal(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Decimal)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Decimal)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Decimal)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Decimal)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Decimal)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Decimal)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Decimal>(reader.GetValue(ordinal));
            }
        }

        public Decimal? GetNullableDecimal(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetDecimal(ordinal);
        }


        public Int16 GetInt16(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Int16)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Int16)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Int16)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Int16)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Int16)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Int16)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Int16>(reader.GetValue(ordinal));
            }
        }

        public Int16? GetNullableInt16(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetInt16(ordinal);
        }


        public Int32 GetInt32(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Int32)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Int32)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Int32)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Int32)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Int32)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Int32)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Int32>(reader.GetValue(ordinal));
            }
        }

        public Int32? GetNullableInt32(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetInt32(ordinal);
        }


        public Int64 GetInt64(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case TypeCode.Byte:
                    return (Int64)reader.GetByte(ordinal);
                case TypeCode.Int16:
                    return (Int64)reader.GetInt16(ordinal);
                case TypeCode.Int32:
                    return (Int64)reader.GetInt32(ordinal);
                case TypeCode.Int64:
                    return (Int64)reader.GetInt64(ordinal);
                case TypeCode.Double:
                    return (Int64)reader.GetDouble(ordinal);
                case TypeCode.Single:
                    return (Int64)reader.GetFloat(ordinal);
                case TypeCode.Decimal:
                    return (Int64)reader.GetDecimal(ordinal);
                default:
                    return ReflectionTools.ChangeType<Int64>(reader.GetValue(ordinal));
            }
        }

        public Int64? GetNullableInt64(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetInt64(ordinal);
        }


        public DateTime GetDateTime(int ordinal)
        {
            LastOrdinal = ordinal;
            DateTime dt;
            switch (typeCodes[ordinal])
            {
                case TypeCode.DateTime:
                    dt = reader.GetDateTime(ordinal);
                    break;
                default:
                    dt = ReflectionTools.ChangeType<DateTime>(reader.GetValue(ordinal));
                    break;
            }

            if (Schema.Current.TimeZoneMode == TimeZoneMode.Utc)
                return new DateTime(dt.Ticks, DateTimeKind.Utc);
            return dt; 
        }

        public DateTime? GetNullableDateTime(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetDateTime(ordinal);
        }


        public DateTimeOffset GetDateTimeOffset(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case tcDateTimeOffset:
                    return reader.GetDateTimeOffset(ordinal);
                default:
                    return ReflectionTools.ChangeType<DateTimeOffset>(reader.GetValue(ordinal));
            }
        }

        public DateTimeOffset? GetNullableDateTimeOffset(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetDateTimeOffset(ordinal);
        }


        public TimeSpan GetTimeSpan(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case tcTimeSpan:
                    return reader.GetTimeSpan(ordinal);
                default:
                    return ReflectionTools.ChangeType<TimeSpan>(reader.GetValue(ordinal));
            }
        }

        public TimeSpan? GetNullableTimeSpan(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetTimeSpan(ordinal);
        }


        public Guid GetGuid(int ordinal)
        {
            LastOrdinal = ordinal;
            switch (typeCodes[ordinal])
            {
                case tcGuid:
                    return reader.GetGuid(ordinal);
                default:
                    return ReflectionTools.ChangeType<Guid>(reader.GetValue(ordinal));
            }
        }

        public Guid? GetNullableGuid(int ordinal)
        {
            LastOrdinal = ordinal;
            if (reader.IsDBNull(ordinal))
            {
                return null;
            }
            return GetGuid(ordinal);
        }

        static Dictionary<Type, MethodInfo> methods =
            typeof(FieldReader).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
            .Where(m => m.Name != "GetExpression" && m.Name != "IsNull")
            .ToDictionary(a => a.ReturnType);


        public static Expression GetExpression(Expression reader, int ordinal, Type type)
        {
            MethodInfo mi = methods.GetOrThrow(type, "Type {0} not supported");

            return Expression.Call(reader, mi, Expression.Constant(ordinal));
        }

        static MethodInfo miIsNull = ReflectionTools.GetMethodInfo((FieldReader r) => r.IsNull(0)); 

        public static Expression GetIsNull(Expression reader, int ordinal)
        {
            return Expression.Call(reader, miIsNull, Expression.Constant(ordinal));
        }

        internal FieldReaderException CreateFieldReaderException(SqlTypeException ex)
        {
            return new FieldReaderException(ex)
            {
                Ordinal = LastOrdinal,
                ColumnName = reader.GetName(LastOrdinal),
                ColumnType = reader.GetFieldType(LastOrdinal),
                
            };
        }
    }

    [Serializable]
    public class FieldReaderException : SqlTypeException
    {
        public FieldReaderException(SqlTypeException inner) : base(null, inner) { }
        protected FieldReaderException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

        public override string Message
        {
            get
            {
                string text = "{0}\r\nOrdinal: {1}\r\nColumnName: {2}\r\nRow: {3}".Formato(
                    InnerException.Message, Ordinal, ColumnName, Row);

                if (Projector != null)
                {
                    text += "\r\nCalling: row.Reader.Get{0}({1})".Formato(ColumnType.Name, Ordinal);
                    text += "\r\nProjector:\r\n{0}".Formato(Projector.NiceToString().Indent(4));
                }

                if(Command != null)
                    text += "\r\nCommand:\r\n{0}".Formato(Command.PlainSql().Indent(4));

                 return text;
            }
        }

        public int Ordinal { get; internal set; }
        public string ColumnName { get; internal set; }
        public Type ColumnType { get; internal set; }
        public int Row { get; internal set; }
        public SqlPreCommand Command { get; internal set; }
        public LambdaExpression Projector { get; internal set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Olmo del Corral
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
 
www.signumframework.com
 
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Nov 2012
Article Copyright 2011 by Olmo del Corral
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid