Click here to Skip to main content
Click here to Skip to main content

Signum Framework Tutorials Part 2 – Southwind Logic

By , 15 Nov 2012
 
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
logo.ico
signumFramework2.0.jpg
SignumFrameworkSetup.vdproj
Snippets
DroidSansMono.ttf
entity.snippet
entityWithName.snippet
expressionMethod.snippet
expressionMethodQuery.snippet
expressionProperty.snippet
field.snippet
fieldCreacion.snippet
fieldEuro.snippet
fieldInicioFin.snippet
fieldString.snippet
fiff.snippet
propiedad.snippet
RazorSingleFileGenerator.vsix
Signum.Fonts.vssettings
T4template
tran.snippet
Template
EntityControlTemplate
__TemplateIcon.ico
MyTemplate.vstemplate
SignumEntityControl.zip
Template
__TemplateIcon.ico
MyTemplate.vstemplate
ProjectName.Entities
__TemplateIcon.ico
MyTemplate.vstemplate
Properties
ProjectName.Load
__TemplateIcon.ico
MyTemplate.vstemplate
Properties
Settings.settings
ProjectName.Logic
__TemplateIcon.ico
MyTemplate.vstemplate
Properties
ProjectName.Web
__TemplateIcon.ico
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
favicon.ico
Global.asax
MyTemplate.vstemplate
Properties
Settings.settings
Scripts
ServerCapri.svc
Views
Capri
Home
Shared
ProjectName.Windows
__TemplateIcon.ico
Controls
MyTemplate.vstemplate
Properties
Settings.settings
SignumFramework2ClientServer.zip
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
favicon.ico
Global.asax
Properties
Settings.settings
Scripts
ServerSouthwind.svc
Views
Home
Shared
Southwind
Southwind.Windows
Controls
Properties
Settings.settings
Southwind.zip
Southwind.bak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities;
using Signum.Engine.Maps;
using Signum.Engine;
using Signum.Test.Properties;
using System.IO;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Signum.Entities.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities;

namespace Signum.Test
{
    public static class Starter
    {
        static bool started = false; 
        public static void StartAndLoad()
        {
            if(!started)
            {
                Start(Settings.Default.SignumTest); 

                Administrator.TotalGeneration();

                Schema.Current.Initialize();

                Load();

                started = true;
            }
        }

        internal static void Dirty()
        {
            started = false;
        }

        public static void Start(string connectionString)
        {
            SchemaBuilder sb = new SchemaBuilder();
            DynamicQueryManager dqm = new DynamicQueryManager();
            ConnectionScope.Default = new Connection(connectionString, sb.Schema, dqm);

            StartMusic(sb, dqm);

        }

        public static void StartMusic(SchemaBuilder sb, DynamicQueryManager dqm)
        {
            sb.Include<AlbumDN>();
            sb.Include<NoteDN>();
            sb.Include<NoteWithDateDN>();
            sb.Include<AlertDN>();
            sb.Include<PersonalAwardDN>();
            sb.Include<AwardNominationDN>();

            dqm[typeof(AlbumDN)] = (from a in Database.Query<AlbumDN>()
                                    select new
                                    {
                                        Entity = a.ToLite(),
                                        a.Id,
                                        Author = a.Author.ToLite(),
                                        Label = a.Label.ToLite(),
                                        a.Name,
                                        a.Year
                                    }).ToDynamic();

            dqm[typeof(NoteDN)] = (from a in Database.Query<NoteDN>()
                                           select new
                                           {
                                               Entity = a.ToLite(),
                                               a.Id,
                                               a.Text,
                                               a.Target
                                           }).ToDynamic();

            dqm[typeof(NoteWithDateDN)] = (from a in Database.Query<NoteWithDateDN>()
                                    select new
                                    {
                                        Entity = a.ToLite(),
                                        a.Id,
                                        a.Text,
                                        Target = a.Target.ToLite(),
                                        a.CreationTime,
                                    }).ToDynamic();

            dqm[typeof(ArtistDN)] = (from a in Database.Query<ArtistDN>()
                                     select new
                                     {
                                         Entity = a.ToLite(),
                                         a.Id,
                                         a.Name,
                                         a.IsMale,
                                         a.Sex,
                                         a.Dead,
                                         LastAward = a.LastAward.ToLite(),
                                     }).ToDynamic();

            dqm.RegisterExpression((IAuthorDN au) => Database.Query<AlbumDN>().Where(a => a.Author == au), () => "Albums", "Albums"); 

            dqm[typeof(BandDN)] = (from a in Database.Query<BandDN>()
                                   select new
                                   {
                                       Entity = a.ToLite(),
                                       a.Id,
                                       a.Name,
                                       LastAward = a.LastAward.ToLite(),
                                   }).ToDynamic();


            dqm[typeof(LabelDN)] = (from a in Database.Query<LabelDN>()
                                    select new
                                    {
                                        Entity = a.ToLite(),
                                        a.Id,
                                        a.Name,
                                    }).ToDynamic();


            dqm[typeof(AmericanMusicAwardDN)] = (from a in Database.Query<AmericanMusicAwardDN>()
                                                 select new
                                                 {
                                                     Entity = a.ToLite(),
                                                     a.Id,
                                                     a.Year,
                                                     a.Category,
                                                     a.Result,
                                                 }).ToDynamic();

            dqm[typeof(GrammyAwardDN)] = (from a in Database.Query<GrammyAwardDN>()
                                          select new
                                          {
                                              Entity = a.ToLite(),
                                              a.Id,
                                              a.Year,
                                              a.Category,
                                              a.Result
                                          }).ToDynamic();

            dqm[typeof(PersonalAwardDN)] = (from a in Database.Query<PersonalAwardDN>()
                                            select new
                                            {
                                                Entity = a.ToLite(),
                                                a.Id,
                                                a.Year,
                                                a.Category,
                                                a.Result
                                            }).ToDynamic();

            dqm[typeof(AwardNominationDN)] = (from a in Database.Query<AwardNominationDN>()
                                            select new
                                            {
                                                Entity = a.ToLite(),
                                                a.Id,
                                                a.Award,
                                                a.Author
                                            }).ToDynamic();
            
            var alertExpr = Linq.Expr((AlertDN a) => new
            {
                Entity = a.ToLite(),
                a.Id,
                a.AlertDate,
                Text = a.Text.Etc(100),
                a.CheckDate,
                Target = a.Entity
            });

            dqm[typeof(AlertDN)] = Database.Query<AlertDN>().Select(alertExpr).ToDynamic();
            dqm[AlertQueries.NotAttended] = Database.Query<AlertDN>().Where(a => a.NotAttended).Select(alertExpr).ToDynamic();
            dqm[AlertQueries.Attended] = Database.Query<AlertDN>().Where(a => a.Attended).Select(alertExpr).ToDynamic();
            dqm[AlertQueries.Future] = Database.Query<AlertDN>().Where(a => a.Future).Select(alertExpr).ToDynamic();
            
            dqm[typeof(IAuthorDN)] = DynamicQuery.Manual((request, descriptions) =>
                                    {
                                        var one = (from a in Database.Query<ArtistDN>()
                                                   select new
                                                   {
                                                       Entity = a.ToLite<IAuthorDN>(),
                                                       a.Id,
                                                       Type = "Artist",
                                                       a.Name,
                                                       Lonely = a.Lonely(),
                                                       LastAward = a.LastAward.ToLite()
                                                   }).ToDQueryable(descriptions)
                                                    .SelectMany(request.Multiplications)
                                                    .Where(request.Filters)
                                                    .OrderBy(request.Orders)
                                                    .Select(request.Columns)
                                                    .TryTake(request.Limit).ToArray();


                                        var two = (from a in Database.Query<BandDN>()
                                                   select new
                                                   {
                                                       Entity = a.ToLite<IAuthorDN>(),
                                                       a.Id,
                                                       Type = "Band",
                                                       a.Name,
                                                       Lonely = a.Lonely(),
                                                       LastAward = a.LastAward.ToLite()
                                                   }).ToDQueryable(descriptions)
                                                    .SelectMany(request.Multiplications)
                                                    .Where(request.Filters)
                                                    .OrderBy(request.Orders)
                                                    .Select(request.Columns)
                                                    .TryTake(request.Limit).ToArray();

                                        return one.Concat(two).OrderBy(request.Orders).TryTake(request.Limit);
                                    })
                                    .Column(a => a.Entity, cl => cl.Implementations = new ImplementedByAttribute(typeof(ArtistDN), typeof(BandDN)))
                                    .Column(a => a.LastAward, cl => cl.Implementations = new ImplementedByAllAttribute());
        }

        public const string Japan = "Japan";
        
        public static void Load()
        {
            var ama = new AmericanMusicAwardDN { Category = "Indie Rock", Year = 1991, Result = AwardResult.Nominated }.Save();

            BandDN smashingPumpkins = new BandDN
            {
                Name = "Smashing Pumpkins",
                Members = "Billy Corgan, James Iha, D'arcy Wretzky, Jimmy Chamberlin"
                .Split(',').Select(s => new ArtistDN { Name = s.Trim(), Sex = s.Contains("Wretzky") ? Sex.Female : Sex.Male, Status = s.Contains("Wretzky") ? Status.Married: (Status?)null }).ToMList(),
                LastAward = ama,
            };

            CountryDN usa = new CountryDN { Name = "USA" };
            CountryDN japan = new CountryDN { Name = Japan };

            smashingPumpkins.Members.ForEach(m => m.Friends = smashingPumpkins.Members.Where(a => a.Sex != m.Sex).Select(a => a.ToLiteFat()).ToMList());

            new NoteWithDateDN { CreationTime = DateTime.Now.AddHours(+8), Text = "American alternative rock band", Target = smashingPumpkins }.Save();

            LabelDN virgin = new LabelDN { Name = "Virgin", Country = usa };

            new AlbumDN
            {
                Name = "Siamese Dream",
                Year = 1993,
                Author = smashingPumpkins,
                Songs = new MList<SongDN> { new SongDN { Name = "Disarm" } },
                Label = virgin
            }.Save();

            AlbumDN mellon = new AlbumDN
            {
                Name = "Mellon Collie and the Infinite Sadness",
                Year = 1995,
                Author = smashingPumpkins,
                Songs = new MList<SongDN> 
                { 
                    new SongDN { Name = "Zero", Duration = 123 }, 
                    new SongDN { Name = "1976" }, 
                    new SongDN { Name = "Tonight, Tonight", Duration = 376 } 
                },
                BonusTrack = new SongDN { Name = "Jellybelly" },
                Label = virgin
            };

            mellon.Save();

            new NoteWithDateDN { CreationTime = DateTime.Now.AddDays(-100).AddHours(-8), Text = "The blue one with the angel", Target = mellon }.Save();

            LabelDN wea = new LabelDN { Name = "WEA International", Country = usa, Owner = virgin.ToLite() };

            new AlbumDN
            {
                Name = "Zeitgeist",
                Year = 2007,
                Author = smashingPumpkins,
                Songs = new MList<SongDN> { new SongDN { Name = "Tarantula" } },
                BonusTrack = new SongDN{Name = "1976"},
                Label = wea,
            }.Save();

            new AlbumDN
            {
                Name = "American Gothic", 
                Year = 2008,
                Author = smashingPumpkins,
                Songs = new MList<SongDN> { new SongDN { Name = "The Rose March", Duration = 276 } },
                Label = wea,
            }.Save();

            var pa  =new PersonalAwardDN { Category = "Best Artist", Year = 1983, Result = AwardResult.Won }.Save();

            ArtistDN michael = new ArtistDN
            {
                Name = "Michael Jackson",
                Dead = true,
                LastAward = pa,
                Status = Status.Single,
            };

            new NoteWithDateDN { CreationTime = new DateTime(2009, 6, 25, 0, 0, 0), Text = "Death on June, 25th", Target = michael }.Save();

            LabelDN universal = new LabelDN { Name = "UMG Recordings", Country = usa };

            new AlbumDN
            {
                Name = "Ben",
                Year = 1972,
                Author = michael,
                Songs = new MList<SongDN> { new SongDN { Name = "Ben" } },
                BonusTrack = new SongDN{Name = "Michael"},
                Label = universal,
            }.Save();

            LabelDN sony = new LabelDN { Name = "Sony", Country = japan };

            new AlbumDN
            {
                Name = "Thriller",
                Year = 1982,
                Author = michael,
                Songs = "Wanna Be Startin' Somethin', Thriller, Beat It"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                BonusTrack = new SongDN{Name = "Billie Jean"},
                Label = sony
            }.Save();

            LabelDN mjj = new LabelDN { Name = "MJJ", Country = usa, Owner = sony.ToLite() };

            new AlbumDN
            {
                Name = "Bad",
                Year = 1989,
                Author = michael,
                Songs = "Bad, Man in the Mirror, Dirty Diana, Smooth Criminal"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                Label = mjj
            }.Save();

            new AlbumDN
            {
                Name = "Dangerous",
                Year = 1991,
                Author = michael,
                Songs = "Black or White, Who Is It, Give it to Me"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                Label = mjj
            }.Save();

            new AlbumDN
            {
                Name = "HIStory",
                Year = 1995,
                Author = michael,
                Songs = "Billie Jean, Stranger In Moscow"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                BonusTrack = new SongDN{Name="Heal The World"},
                Label = mjj
            }.Save();

            new AlbumDN
            {
                Name = "Blood on the Dance Floor",
                Year = 1995,
                Author = michael,
                Songs = "Blood on the Dance Floor, Morphine"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                Label = mjj
            }.Save();

            var ga = new GrammyAwardDN { Category = "Foreing Band", Year = 2001, Result = AwardResult.Won };

            BandDN sigurRos = new BandDN
            {
                Name = "Sigur Ros",
                Members = "Jón Þór Birgisson, Georg Hólm, Orri Páll Dýrason"
                .Split(',').Select(s => new ArtistDN { Name = s.Trim() }).ToMList(),
                LastAward = ga,
            };

            LabelDN fatCat = new LabelDN { Name = "FatCat Records", Country = usa, Owner = universal.ToLite() }; 

            new AlbumDN
            {
                Name = "Ágaetis byrjun",
                Year = 1999,
                Author = sigurRos,
                Songs = "Scefn-g-englar"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                BonusTrack = new SongDN { Name = "Intro" },
                Label = fatCat
            }.Save();

            LabelDN emi = new LabelDN { Name = "EMI", Country = usa }; 

            new AlbumDN
            {
                Name = "Takk...",
                Year = 2005,
                Author = sigurRos,
                Songs = "Hoppípolla, Glósóli, Saeglópur"
                .Split(',').Select(s => new SongDN { Name = s.Trim() }).ToMList(),
                BonusTrack = new SongDN { Name = "Svo hljótt" },
                Label = emi
            }.Save();


            new AwardNominationDN { Author = sigurRos.ToLite<IAuthorDN>(), Award = ga.ToLite<AwardDN>() }.Save();
            new AwardNominationDN { Author = michael.ToLite<IAuthorDN>(), Award = ga.ToLite<AwardDN>() }.Save();
            new AwardNominationDN { Author = smashingPumpkins.ToLite<IAuthorDN>(), Award = ga.ToLite<AwardDN>() }.Save();

            new AwardNominationDN { Author = sigurRos.ToLite<IAuthorDN>(), Award = ama.ToLite<AwardDN>() }.Save();
            new AwardNominationDN { Author = michael.ToLite<IAuthorDN>(), Award = ama.ToLite<AwardDN>() }.Save();
            new AwardNominationDN { Author = smashingPumpkins.ToLite<IAuthorDN>(), Award = ama.ToLite<AwardDN>() }.Save();

            new AwardNominationDN { Author = michael.ToLite<IAuthorDN>(), Award = pa.ToLite<AwardDN>() }.Save();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use 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)

About the Author

Olmo del Corral
Software Developer (Senior) Signum Software
Spain Spain
Member
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...

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