Exemplo n.º 1
0
 public static Data.Models.Song ToEntity(this Domain.Song domain)
 {
     return(new Data.Models.Song()
     {
         Id = domain.Id,
         Title = domain.Title,
         CategoryId = domain.CategoryId,
         OriginalArtist = domain.OriginalArtist
     });
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var playlist = Init();
            #region Exceptions
            //            try
            //            {
            //                Debug.WriteLine("Throwing exception");
            //                Trace.WriteLine("Tracing...");
            //                //playlist.Move(2, 99);
            //                //playlist.Next();
            //                //throw new Exception();
            //            }
            //            catch (IndexOutOfRangeException ex)
            //            {
            //                Console.WriteLine(ex.Message);
            //                Debug.WriteLine(ex.StackTrace);
            //                Trace.WriteLine("Tracing 2...");
            //            }
            //            catch (SomethingWrongException ex)
            //            {
            //                Console.WriteLine(ex.Message);
            //#if MY_CONDITION
            //                throw;
            //#endif
            //            }
            //            catch (Exception ex)
            //            {
            //#if MY_CONDITION
            //                throw new SomethingWrongException("Woops... Try again later", ex);
            //#endif
            //            }
            //            finally
            //            {
            //                Debug.WriteLine("Finally.");
            //                Console.WriteLine("Do something regardless of was there exception or not.");
            //            }
            #endregion

            #region Modile10

            Console.WriteLine("============ Initial playlist");
            playlist.AllSongs.ToList().ForEach(Console.WriteLine);

            Console.WriteLine("============ Using delegate");
            MutatePlaylist(playlist, Shuffle);
            playlist.AllSongs.ToList().ForEach(Console.WriteLine);

            Console.WriteLine("============ Using anonymous function");
            MutatePlaylist(playlist, delegate(Playlist p)
            {
                int n = p.SongCount;
                RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
                while (n > 1)
                {
                    byte[] box = new byte[1];

                    do provider.GetBytes(box); while (!(box[0] < n*(Byte.MaxValue/n)));

                    int k = box[0]%n;
                    n--;
                    p.Move(n, k);
                }
            });
            playlist.AllSongs.ToList().ForEach(Console.WriteLine);

            Console.WriteLine("============ Using lambda expression");
            MutatePlaylist(playlist, p =>
            {
                int n = p.SongCount;
                RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
                while (n > 1)
                {
                    byte[] box = new byte[1];

                    do provider.GetBytes(box); while (!(box[0] < n * (Byte.MaxValue / n)));

                    int k = box[0] % n;
                    n--;
                    p.Move(n, k);
                }
            });
            playlist.AllSongs.ToList().ForEach(Console.WriteLine);

            Console.WriteLine("============ Using Extension Method");
            playlist.Shuffle();
            playlist.AllSongs.ToList().ForEach(Console.WriteLine);

            var sss = playlist.AllSongs.Select(s => new {s.Album.Artist.Name, s.Title});
            var jjj = playlist.AllSongs.Where(s => s.Length > 400);
            #endregion

            #region LINQ

            var songs = Init().AllSongs.ToList();

            var where = songs.Where(s => s.Length > 400);
            var take = songs.Take(5);
            var skip = songs.Skip(songs.Count/3);
            var takeWhile = songs.TakeWhile(s => s.Length < 300);
            var skipWhile = songs.SkipWhile(s => s.Length < 300);
            var distinct = songs.Distinct(new SongEqualityComparer());

            var select = songs.Select(s => new {s.Album.Artist.Name, s.Title});
            var selectMany = songs.SelectMany(s => s.Album.Genres);

            var orderBy = songs.OrderBy(s => s.Length).ThenBy(s => s.Title);
            var orderByDesc = songs.OrderByDescending(s => s.Length).ThenByDescending(s => s.Title);
            var groupBy = songs.GroupBy(s => s.Album.Artist.Name);
            var concat = Enumerable.Concat(takeWhile, skipWhile);
            var concat2 = takeWhile.Concat(skipWhile);

            //var first = songs.Single(s => s.Length > 300);
            var elementAt = songs.ElementAt(5);

            var count = songs.Count;

            var agv = songs.Average(s => s.Length);

            var arr1 = Enumerable.Range(1, 6);
            var arr2 = new int[] {4, 5, 6, 7, 8, 9, 10};
            var repeat = Enumerable.Repeat(5, 10);

            var firstOrDef = arr2.FirstOrDefault(x => x > 100);

            var union = arr1.Union(arr2);
            var intersect = arr1.Intersect(arr2);
            var except = arr1.Except(arr2);

            var nightwish = new Artist(name: "Nightwish", activeTo: null, activeFrom: new DateTime(1996, 1, 1), country: "Finland");
            var dope = new Artist(name: "Dope", activeTo: new DateTime(2013, 1, 1), activeFrom: new DateTime(1997, 1, 1), country: "US");
            var groupTherapyAlbum = new Album("Group Therapy", null, new List<string>() { "Nu metal", "Industrial metal", "Alternative metal" }, null,
                "Simon Dope", DateTime.Now, dope, new Label(DateTime.Today, "Sony"));
            var onceAlbum = new Album("Once", null, new List<string>() { "Symphonic metal", "Power metal" }, null,
                "Tuomas Holopainen", DateTime.Now, nightwish, new Label(DateTime.Today, "Nuclear Blast"));

            var song1 = new Song(@"C:\path\to\songs\Nightwish - Nemo", 189, AudioFormat.Flac, "Nemo", onceAlbum);
            var song2 = new Song(@"C:\path\to\songs\Dope - Motivation", 300, AudioFormat.Mp3, "Motivation",
                groupTherapyAlbum);

            var mixed = new List<Object>() {1, 2, 3, nightwish, dope, groupTherapyAlbum, onceAlbum, song1, song2 };

            var sgs = mixed.OfType<Song>();
            var artists = mixed.OfType<Artist>();

            try
            {
                mixed.Cast<Album>();
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("Some of arguments are null");
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine("Can't convert to song");
            }

            int it = 1;
            var dict = songs.ToDictionary(song => it++, song => song.Title);
            var lookup = songs.ToLookup(song => song.Album.Name, song => song.Title);
            songs.Reverse(0, songs.Count);

            //closure
            string hello = "Hello";

            Action<String> tmp = (username) => Console.WriteLine(hello + ", " + username);

            var greet = new Action<string>(tmp);

            greet("Alex");

            #endregion

            var factory = SongFactory.Instance;

            var paradiseLost = new List<Song>
            {
                factory.CreateSong(@"C:\Users\alexandr.racovschi\Downloads\Paradise Lost - Say Just Words.mp3"),
                factory.CreateSong(@"C:\Users\alexandr.racovschi\Downloads\Paradise Lost - Unreachable.mp3"),
                factory.CreateSong(@"C:\Users\alexandr.racovschi\Downloads\Paradise Lost - The Last Fallen Saviour.mp3")
            };

            paradiseLost.ForEach(Console.WriteLine);
        }