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.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Signum.Engine;
using Signum.Entities;
using System.Diagnostics;
using System.IO;
using Signum.Engine.Linq;
using Signum.Utilities;
using System.Linq.Expressions;
using System.Data.SqlTypes;
using System.Reflection;
using Signum.Utilities.ExpressionTrees;

namespace Signum.Test.LinqProvider
{
    /// <summary>
    /// Summary description for LinqProvider
    /// </summary>
    [TestClass]
    public class SelectTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Starter.StartAndLoad();
        }

        [TestInitialize]
        public void Initialize()
        {
            Connection.CurrentLog = new DebugTextWriter();
        }      
     
        [TestMethod]
        public void Select()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.Name).ToList();
        }

        [TestMethod]
        public void SelectIndex()
        {
            var list = Database.Query<AlbumDN>().Select((a, i) =>  a.Name + i).ToList();
        }

        [TestMethod]
        public void SelectExpansion()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.Label.Name).ToList();
        }

        [TestMethod]
        public void SelectLetExpansion()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        let l = a.Label
                        select l.Name).ToList();
        }

        [TestMethod]
        public void SelectLetExpansionRedundant()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        let label = a.Label
                        select new
                        {
                            Artist = label.Country.ToStr,
                            Author = a.Label.ToStr
                        }).ToList();

            Assert.AreEqual(Database.Query<AlbumDN>().Count(), list.Count);
        }

        [TestMethod]
        public void SelectWhereExpansion()
        {
            var list = Database.Query<AlbumDN>().Where(a=>a.Label != null).Select(a => a.Label.Name).ToList();
        }

        [TestMethod]
        public void SelectAnonymous()
        {
            var list = Database.Query<AlbumDN>().Select(a => new { a.Name, a.Year }).ToList();
        }

        [TestMethod]
        public void SelectNoColumns()
        {
            var list = Database.Query<AlbumDN>().Select(a => new { DateTime.Now, Album = (AlbumDN)null, Artist = (Lite<ArtistDN>)null }).ToList();
        }

        [TestMethod]
        public void SelectCount()
        {
            var list = Database.Query<AlbumDN>().Select(a => (int?)a.Songs.Count).ToList();
        }

        [TestMethod]
        public void SelectLite()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.ToLite()).ToList();
        }

        [TestMethod]
        public void SelectLiteToStr()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.ToLite(a.Label.Name)).ToList();
        }

        [TestMethod]
        public void SelectLiteIB()
        {
            var list = Database.Query<AlbumDN>()
                .Select(a => a.Author.ToLite()).ToList();
        }

        [TestMethod]
        public void SelectEntityWithLiteIb()
        {
            var list = Database.Query<AwardNominationDN>().Where(a => a.Award.Entity is GrammyAwardDN).ToList();
        }

        [TestMethod]
        public void SelectLiteUpcast()
        {
            var list = Database.Query<ArtistDN>()
                .Select(a => a.ToLite<IAuthorDN>()).ToList();
        }

        [TestMethod]
        public void SelectLiteCastUpcast()
        {
            var list = Database.Query<ArtistDN>()
                .SelectMany(a => a.Friends).Select(a=>a.ToLite<IAuthorDN>()).ToList();
        }

        [TestMethod]
        public void SelectLiteCastNocast()
        {
            var list = Database.Query<ArtistDN>()
                .SelectMany(a => a.Friends).Select(a => a.ToLite<ArtistDN>()).ToList();
        }

        [TestMethod]
        public void SelectLiteCastDowncast()
        {
            var list = Database.Query<AlbumDN>()
                .Select(a => a.Author.ToLite().ToLite<ArtistDN>()).ToList();
        }


        [TestMethod]
        public void SelectLiteGenericUpcast()
        {
            var list = SelectAuthorsLite<ArtistDN, IAuthorDN>();
        }

        public List<Lite<LT>> SelectAuthorsLite<T, LT>()
            where T : IdentifiableEntity, LT
            where LT : class, IIdentifiable
        {
            return Database.Query<T>().Select(a => a.ToLite<LT>()).ToList(); //an explicit convert is injected in this scenario
        }

        [TestMethod]
        public void SelectLiteIBRedundant()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        let band = (BandDN)a.Author
                        select new { Artist = band.LastAward.ToStr, Author = a.Author.ToStr }).ToList();

            Assert.AreEqual(Database.Query<AlbumDN>().Count(), list.Count);
        }

        [TestMethod]
        public void SelectLiteIBWhere()
        {
            var list = Database.Query<AlbumDN>()
                .Select(a => a.Author.ToLite())
                .Where(a => a.ToStr.StartsWith("Michael")).ToList();
        }

        [TestMethod]
        public void SelectLiteIBA()
        {
            var list = Database.Query<NoteWithDateDN>().Select(a => a.Target.ToLite()).ToList();
        }

        [TestMethod]
        public void SelectEntity()
        {
            var list = Database.Query<AlbumDN>().ToList();
        }

        [TestMethod]
        public void SelectEntitySelect()
        {
            var list = Database.Query<AlbumDN>().Select(a => a).ToList();
        }

        [TestMethod]
        public void SelectEntityIB()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.Author).ToList();
        }

        [TestMethod]
        public void SelectEntityIBRedundant()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        let aut = a.Author
                        select new { aut, a.Author }).ToList();
        }

        [TestMethod]
        public void SelectEntityIBA()
        {
            var list = Database.Query<NoteWithDateDN>().Select(a => a.Target).ToList();
        }

        [TestMethod]
        public void SelectBool()
        {
            var list = Database.Query<ArtistDN>().Select(a => a.Dead).ToList();
        }

        [TestMethod]
        public void SelectConditionToBool()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.Year < 1990).ToList();
        }

        [TestMethod]
        public void SelectCastIB()
        {
            var list = (from a in Database.Query<AlbumDN>()
                       select ((ArtistDN)a.Author).Name ?? 
                              ((BandDN)a.Author).Name).ToList();
        }

        [TestMethod]
        public void SelectCastIBPolymorphic()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        select a.Author.Name).ToList();
        }

        [TestMethod]
        public void SelectCastIBPolymorphicIB()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        select a.Author.LastAward.ToLite()).ToList();
        }

        [TestMethod]
        public void SelectCastIBA()
        {
            var list = (from n in Database.Query<NoteWithDateDN>()
                        select 
                        ((ArtistDN)n.Target).Name ??
                        ((AlbumDN)n.Target).Name ??
                        ((BandDN)n.Target).Name).ToList();

        }

        [TestMethod]
        public void SelectCastIsIB()
        {
            var list = (from a in Database.Query<AlbumDN>()
                        select (a.Author is ArtistDN ? ((ArtistDN)a.Author).Name :
                                                        ((BandDN)a.Author).Name)).ToList(); //Just to full-nominate
        }

        [TestMethod]
        public void SelectCastIsIBA()
        {
            var list = (from n in Database.Query<NoteWithDateDN>()
                        select n.Target is ArtistDN ?
                        ((ArtistDN)n.Target).Name :
                        ((BandDN)n.Target).Name).ToList(); //Just to full-nominate
        }

        [TestMethod]
        public void SelectUpCast()
        {
            var list = (from n in Database.Query<ArtistDN>()
                        select (IAuthorDN)n).ToList(); //Just to full-nominate
        }

        [TestMethod]
        public void SelectEntityEquals()
        {
            ArtistDN michael = Database.Query<ArtistDN>().Single(a => a.Dead);

            var list = Database.Query<AlbumDN>().Select(a => a.Author == michael).ToList(); 
        }

        [TestMethod]
        public void SelectBoolExpression()
        {
            ArtistDN michael = Database.Query<ArtistDN>().Single(a => a.Dead);

            var list = Database.Query<AlbumDN>().Select(a => a.Author == michael).ToList();
        }

        [TestMethod]
        public void SelectExpressionProperty()
        {
            var list = Database.Query<ArtistDN>().Where(a => a.IsMale).ToArray();
        }

        [TestMethod]
        public void SelectExpressionMethod()
        {
            var list = Database.Query<ArtistDN>().Select(a => new { a.Name, Count = a.AlbumCount() }).ToArray();
        }

        [TestMethod]
        public void SelectPolyExpressionProperty()
        {
            var list = Database.Query<AlbumDN>().Select(a => a.Author.FullName).ToArray();
        }

        [TestMethod]
        public void SelectPolyExpressionMethod()
        {
            var list2 = Database.Query<AlbumDN>().Select(a => a.Author is BandDN ? ((BandDN)a.Author).Members.Count : ((ArtistDN)a.Author).Friends.Count).ToArray();
            var list = Database.Query<AlbumDN>().Select(a => a.Author.Lonely()).ToArray();
        }

        [TestMethod]
        public void SelectThrowIntNullable()
        {
            Assert2.Throws<FieldReaderException>(() =>
                Database.Query<AlbumDN>().Select(a => ((ArtistDN)a.Author).Id).ToArray());
        }

        [TestMethod]
        public void SelectThrowBoolNullable()
        {
            Assert2.Throws<FieldReaderException>(() =>
                Database.Query<AlbumDN>().Select(a => ((ArtistDN)a.Author).Dead).ToArray());
        }
        
        [TestMethod]
        public void SelectThrowEnumNullable()
        {
            Assert2.Throws<FieldReaderException>(() =>
                Database.Query<AlbumDN>().Select(a => ((ArtistDN)a.Author).Sex).ToArray());
        }

        [TestMethod]
        public void SelectIntNullable()
        {
            var list = Database.Query<AlbumDN>().Select(a => (int?)((ArtistDN)a.Author).Id).ToArray();
        }

        [TestMethod]
        public void SelectBoolNullable()
        {
            var list = Database.Query<AlbumDN>().Select(a => (bool?)((ArtistDN)a.Author).Dead).ToArray();
        }

        [TestMethod]
        public void SelectEnumNullable()
        {
            var list = Database.Query<ArtistDN>().Select(a => a.Status).ToArray();
        }

        [TestMethod]
        public void SelectEnumNullableNullable()
        {
            var list = Database.Query<AlbumDN>().Select(a => ((ArtistDN)a.Author).Status).ToArray();
        }


        [TestMethod]
        public void SelectEnumNullableBullableCast()
        {
            var list = Database.Query<AlbumDN>().Select(a => (Sex?)((ArtistDN)a.Author).Sex).ToArray();
        }

        [TestMethod]
        public void SelectEnumNullableValue()
        {
            var list = Database.Query<AlbumDN>().Where(a => a.Author is ArtistDN)
                .Select(a => ((Sex?)((ArtistDN)a.Author).Sex).Value).ToArray();
        }

        [TestMethod]
        public void SelectEmbeddedNullable()
        {
            var bonusTracks = Database.Query<AlbumDN>().Select(a => a.BonusTrack).ToArray();
        }

        [TestMethod]
        public void SelectNullable()
        {
            var durations = (from a in Database.Query<AlbumDN>()
                             from s in a.Songs
                             where s.Duration.HasValue
                             select s.Duration.Value).ToArray();
        }

        [TestMethod]
        public void SelectIsNull()
        {
            var durations = (from a in Database.Query<AlbumDN>()
                             from s in a.Songs
                             where s.Duration.HasValue
                             select s.Duration == null).ToArray();
        }

        [TestMethod]
        public void SelectAvoidNominate()
        {
            var durations =
                (from a in Database.Query<AlbumDN>()
                 select new
                {
                    a.Name,
                    Value = 3,
                }).ToList();
        }

        [TestMethod]
        public void SelectAvoidNominateEntity()
        {
            var durations =
                (from a in Database.Query<AlbumDN>()
                 select new
                 {
                     a.Name,
                     Friend = (Lite<BandDN>)null
                 }).ToList();
        }


        [TestMethod]
        public void SelectSingleCellAggregate()
        {
            var list = Database.Query<BandDN>()
                .Select(b => new
                {
                    Count = b.Members.Count,
                    AnyDead = b.Members.Any(m => m.Dead),
                    DeadCount = b.Members.Count(m => m.Dead),
                    MinId = b.Members.Min(m => m.Id),
                    MaxId = b.Members.Max(m => m.Id),
                    AvgId = b.Members.Average(m => m.Id),
                    SumId = b.Members.Sum(m => m.Id),
                }).ToList();
        }

        [TestMethod]
        public void SelectSingleCellWhere()
        {
            var list = Database.Query<BandDN>().Where(b => b.Members.OrderBy(a => a.Sex).Select(a => a.Sex).First() == Sex.Male).Select(a => a.Name).ToList();
        }

        [TestMethod]
        public void SelectSingleCellSingle()
        {
            var list = Database.Query<BandDN>().Select(b => new
            {
                FirstName = b.Members.Select(m => m.Name).First(),
                FirstOrDefaultName = b.Members.Select(m => m.Name).FirstOrDefault(),
                SingleName = b.Members.Take(1).Select(m => m.Name).Single(),
                SingleOrDefaultName = b.Members.Take(1).Select(m => m.Name).SingleOrDefault(),
            }).ToList();
        }

        [TestMethod]
        public void SelectMemoryEntity()
        {
            var artist = Database.Query<ArtistDN>().First();

            var songs = Database.Query<AlbumDN>().Select(a => new
            {
                Lite = a.ToLite(),
                Memory = artist,
            }).ToList(); 
        }

        [TestMethod]
        public void SelectMemoryLite()
        {
            var artist = Database.Query<ArtistDN>().Select(a=>a.ToLite()).First();

            var songs = Database.Query<AlbumDN>().Select(a => new
            {
                Lite = a.ToLite(),
                MemoryLite = artist,
            }).ToList();
        }

        [TestMethod]
        public void SelectOutsideStringNull()
        {
            var awards = Database.Query<GrammyAwardDN>().Select(a => ((AmericanMusicAwardDN)(AwardDN)a).Category).ToList();
        }

        [TestMethod]
        public void SelectOutsideLiteNull()
        {
            var awards = Database.Query<GrammyAwardDN>().Select(a => ((AmericanMusicAwardDN)(AwardDN)a).ToLite()).ToList();
        }

        [TestMethod]
        public void SelectMListLite()
        {
            var lists = (from mle in Database.MListQuery((ArtistDN a) => a.Friends)
                         select new { Artis = mle.Parent.Name, Friend = mle.Element.Entity.Name }).ToList();
        }

        [TestMethod]
        public void SelectMListEntity()
        {
            var lists = (from mle in Database.MListQuery((BandDN a) => a.Members)
                         select new { Band = mle.Parent.Name, Artis = mle.Element.Name }).ToList();
        }

        [TestMethod]
        public void SelectMListEmbedded()
        {
            var lists = (from mle in Database.MListQuery((AlbumDN a) => a.Songs)
                         select mle).ToList();
        }
    }

    public static class AuthorExtensions
    {
        static Expression<Func<IAuthorDN, int>> AlbumCountExpression = auth => Database.Query<AlbumDN>().Count(a => a.Author == auth);
        public static int AlbumCount(this IAuthorDN author)
        {
            return Database.Query<AlbumDN>().Count(a => a.Author == author);
        }
    }
}

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 | Mobile
Web04 | 2.8.141015.1 | Last Updated 15 Nov 2012
Article Copyright 2011 by Olmo del Corral
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid