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.Utilities;

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

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

        [TestMethod]
        public void WhereIndex()
        {
            var list = Database.Query<AlbumDN>().Where((a,i) => i % 2 == 0).ToList();
        }

        [TestMethod]
        public void WhereExplicitConvert()
        {
            var list = Database.Query<AlbumDN>().Where(a=>a.Id.ToString() == "1").ToList();
        }

        [TestMethod]
        public void WhereImplicitConvert()
        {
            Database.Query<AlbumDN>().Where(a => ("C" + a.Id) == "C1").Any();
        }


        [TestMethod]
        public void WhereSelect()
        {
            var list = Database.Query<AlbumDN>().Where(a => a.Year < 1995).Select(a => new { a.Year, Author = a.Author.ToLite(), a.Name }).ToList();
        }
        
        [TestMethod]
        public void WhereBool()
        {
            var list = Database.Query<ArtistDN>().Where(a => a.Dead).ToList();
        }

        [TestMethod]
        public void WhereNotNull()
        {
            var list = Database.Query<ArtistDN>().Where(a => a.LastAward != null).ToList();
        }

        [TestMethod]
        public void SingleFirstLast()
        {
            var artists = Database.Query<ArtistDN>();

            Assert2.Throws<InvalidOperationException>(() => artists.Single(a => a.Dead && !a.Dead));
            Assert.IsNotNull(artists.Single(a => a.Dead)); //michael
            Assert2.Throws<InvalidOperationException>(() => artists.Single(a => a.Sex == Sex.Male));

            Assert.IsNull(artists.SingleOrDefault(a => a.Dead && !a.Dead));
            Assert.IsNotNull(artists.SingleOrDefault(a => a.Dead)); //michael
            Assert2.Throws<InvalidOperationException>(() => artists.SingleOrDefault(a => a.Sex == Sex.Male));

            Assert2.Throws<InvalidOperationException>(() => artists.First(a => a.Dead && !a.Dead));
            Assert.IsNotNull(artists.First(a => a.Dead)); //michael
            Assert.IsNotNull(artists.First(a => a.Sex == Sex.Male));

            Assert.IsNull(artists.FirstOrDefault(a => a.Dead && !a.Dead));
            Assert.IsNotNull(artists.FirstOrDefault(a => a.Dead)); //michael
            Assert.IsNotNull(artists.FirstOrDefault(a => a.Sex == Sex.Male));

            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Dead && !a.Dead).SingleOrMany());
            Assert.IsNotNull(artists.Where(a => a.Dead).SingleOrMany()); //michael
            Assert.IsNull(artists.Where(a => a.Sex == Sex.Male).SingleOrMany());
        }

        [TestMethod]
        public void SingleFirstLastError()
        {
            var artists = Database.Query<ArtistDN>();

            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Dead && !a.Dead).Single("X"), "X");
            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Sex == Sex.Male).Single("X"), "X");
            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Sex == Sex.Male).SingleOrDefault("X"), "X");
            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Dead && !a.Dead).First("X"),"X");


            Assert2.Throws<InvalidOperationException>(() => artists.Where(a => a.Dead && !a.Dead).SingleOrMany("X"), "X");
        }


        [TestMethod]
        public void WhereEntityEquals()
        {
            ArtistDN wretzky = Database.Query<ArtistDN>().Single(a => a.Sex == Sex.Female);

            BandDN smashing = (from b in Database.Query<BandDN>()
                               from a in b.Members
                               where a == wretzky
                               select b).Single(); 
        }


        [TestMethod]
        public void WhereLiteEquals()
        {
            ArtistDN wretzky = Database.Query<ArtistDN>().Single(a => a.Sex == Sex.Female);

            BandDN smashing = (from b in Database.Query<BandDN>()
                               from a in b.Members
                               where a.ToLite() == wretzky.ToLite()
                               select b).Single();
        }


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

            var albums = (from a in Database.Query<AlbumDN>()
                          where a.Author == michael
                          select a.ToLite()).ToList();
        }

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

            var albums = (from n in Database.Query<NoteWithDateDN>()
                          where n.Target == michael
                          select n.ToLite()).ToList();
        }

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

            var albums = (from a in Database.Query<AlbumDN>()
                          where a.Author.ToLite() == michael.ToLite<IAuthorDN>()
                          select a.ToLite()).ToList();
        }

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

            var albums = (from n in Database.Query<NoteWithDateDN>()
                          where n.Target.ToLite() == michael.ToLite<IIdentifiable>()
                          select n.ToLite()).ToList();
        }

        [TestMethod]
        public void WhereIs()
        {
            var albums = (from a in Database.Query<AlbumDN>()
                          where a.Author is ArtistDN
                          select a.ToLite()).ToList();
        }

        [TestMethod]
        public void WhereCase()
        {
            var list = (from a in Database.Query<ArtistDN>()
                        where a.Dead ? a.Name.Contains("Michael") : a.Name.Contains("Billy")
                        select a).ToArray();
        }

        [TestMethod]
        public void WherePolyExpressionMethod()
        {
            var list = Database.Query<AlbumDN>().Where(a => a.Author.Lonely()).ToArray();
        }


        [TestMethod]
        public void WhereOptimize()
        {
            var list = Database.Query<ArtistDN>().Where(a => a.Dead && true).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead && false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead || true).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead || false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead == true).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead == false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead != true).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => a.Dead != false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => true ? a.Dead : false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => false ? false : a.Dead).Select(a => a.Name).ToList();

            list = Database.Query<ArtistDN>().Where(a => true).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => !false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => true ? true : false).Select(a => a.Name).ToList();
            list = Database.Query<ArtistDN>().Where(a => false ? false : true).Select(a => a.Name).ToList();
        }

        [TestMethod]
        public void WhereInnerQueryable()
        {
            var females = Database.Query<ArtistDN>().Where(a=>a.Sex == Sex.Female);
            string f = females.ToString();

            var female = Database.Query<ArtistDN>().Single(a=>females.Contains(a));
        }

        [TestMethod]
        public void WhereEmbeddedNull()
        {
            var albumsWithBonusTrack = Database.Query<AlbumDN>().Where(a => a.BonusTrack == null).ToList();
        }

        [TestMethod]
        public void WhereEmbeddedNotNull()
        {
            var albumsWithBonusTrack = Database.Query<AlbumDN>().Where(a => a.BonusTrack != null).ToList();
        }

        [TestMethod]
        public void WhereBindTuple()
        {
            var albums = Database.Query<AlbumDN>().Select(a => Tuple.Create(a.Name, a.Label)).Where(t => t.Item2 == null).ToList(); 
        }

        [TestMethod]
        public void WhereBindBigTuple()
        {
            var albums = Database.Query<AlbumDN>().Select(a => Tuple.Create(a.Name, a.Name, a.Name, a.Name, a.Name, a.Name, a.Name, a.Label)).Where(t => t.Rest.Item1 == null).ToList();
        }

        [TestMethod]
        public void WhereOutsideIs()
        {
            var albums = Database.Query<BandDN>().Where(a => a.LastAward is PersonalAwardDN).ToList();
        }

        [TestMethod]
        public void WhereOutsideCast()
        {
            var albums = Database.Query<BandDN>().Where(a => ((PersonalAwardDN)a.LastAward) != null).ToList();
        }

        [TestMethod]
        public void WhereOutsideEquals()
        {
            var pa = Database.Query<PersonalAwardDN>().First();

            var albums = Database.Query<BandDN>().Where(a => a.LastAward == pa).ToList();
        }

    }
}

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