Platform Independent Full Stack Development New Frontiers Platform Independent Full Stack Development
Thanks to our GOLD Sponsor
Thanks to our SILVER Sponsors
What’s On Deck? Quick Introduction to Our Toolset Creating the Database Server Defining Our Database Shape Creating the Service Layer Testing It! VS Code vs. Visual Studio Community/Professional/Enterprise Azure Data Studio vs. SQL Server Management Studio Question Time
Our Toolset for Today 1. iTerm2 2. Docker w/ SQL Server 2017 Linux https://www.iterm2.com 2. Docker w/ SQL Server 2017 Linux https://www.docker.com/community-edition https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-linux-2017 3. SQL Ops Studio Azure Data Studio https://docs.microsoft.com/en-us/sql/azure-data-studio/download?view=sql-server-2017 4. dotnet Command Line https://docs.microsoft.com/en-us/dotnet/core/tools/?tabs=netcore2x 5. Visual Studio Code https://code.visualstudio.com 6. Postman https://www.getpostman.com
Creating the Database Server
Create a New Docker Volume docker volume create --name sql_saturday_madison
Create a SQL Server Instance sudo docker pull microsoft/mssql-server-linux:2017-latest docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=password123!' -p 1433:1433 -v sql_saturday_madison:/var/opt/mssql --name sql_saturday_madison -d microsoft/mssql-server-linux:2017-latest
Azure Data Studio
Creating the Database Shape
Create User and Database CREATE LOGIN [Sql_Saturday_User] WITH PASSWORD = 'password123!'; GO CREATE DATABASE [Sql_Saturday]; USE [Sql_Saturday]; CREATE USER [Sql_Saturday_User] FOR LOGIN [Sql_Saturday_User]; EXEC sp_addrolemember 'db_owner’, Sql_Saturday_User';
Create the Song Table CREATE TABLE dbo.Song ( SongId UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_Song PRIMARY KEY CLUSTERED, SongName VARCHAR(150) NOT NULL, Composer VARCHAR(150) NOT NULL ) ON [PRIMARY]; GO
Fill It INSERT INTO dbo.Song (SongId, SongName, Composer) SELECT NEWID(), 'Two Step', 'Dave Matthews Band' UNION SELECT NEWID(), 'Crush', 'Dave Matthews Band' SELECT NEWID(), 'One Sweet World', 'Dave Matthews Band' SELECT NEWID(), 'All Along the Watchtower', 'Bob Dylan' SELECT NEWID(), 'Minarets', 'Dave Matthews Band' SELECT NEWID(), 'Why I Am', 'Dave Matthews Band' SELECT NEWID(), 'Warehouse', 'Dave Matthews Band' SELECT NEWID(), 'Grey Street', 'Dave Matthews Band'; GO
Create the Concert Table CREATE TABLE dbo.Concert ( ConcertId UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_Concert PRIMARY KEY CLUSTERED, ConcertDate DATE NOT NULL, ConcertLocation VARCHAR(150) NOT NULL ) ON [PRIMARY]; GO
Fill It INSERT INTO dbo.Concert (ConcertId, ConcertLocation, ConcertDate) SELECT NEWID(), 'Keybank Pavilion, Burgettstown, PA', '20160709' UNION SELECT NEWID(), 'Alpine Valley Music Center, Elkhorn, WI', '20160701' SELECT NEWID(), 'Alpine Valley Music Center, Elkhorn, WI', '20160702' SELECT NEWID(), 'Riverbend Music Center, Cincinnati, OH', '20130712'; GO
dotnet Command Line
Create a New WebAPI Project cd ./code mkdir new-frontiers cd ./new-frontiers dotnet new webapi --name NewFrontiers -o ./new-frontiers code .
Visual Studio Code
Coding in VS Code
Install Entity Framework Core dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.CodeAnalysis
Update Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var connectionString = @"Server=localhost;Database=Sql_Saturday;User=Sql_Saturday_User;Password=password123!"; services.AddDbContext<ConcertDbContext>(options => options.UseSqlServer(connectionString)); }
Add a Song Model public class Song { public Guid SongId {get;set;} public string SongName {get;set;} public string Composer {get;set;} }
Add a Concert Model public class Concert { public Guid ConcertId {get;set;} public string ConcertLocation {get;set;} public DateTime ConcertDate {get;set;} }
Create Out DbContext public class ConcertDbContext : DbContext { public ConcertDbContext(DbContextOptions<ConcertDbContext> options) :base (options) } public DbSet<Song> Songs {get;set;} public DbSet<Concert> Concerts {get;set;} protected override void OnModelCreating(ModelBuilder builder) builder.Entity<Song>(s => { s.ToTable("Song", "dbo"); s.HasKey(c => c.SongId); s.Property(sid => sid.SongId).ValueGeneratedOnAdd(); s.Property(c => c.Composer).IsRequired().HasMaxLength(150); s.Property(sn => sn.SongName).IsRequired().HasMaxLength(150); }); builder.Entity<Concert>(c => { c.ToTable("Concert", "dbo"); c.HasKey(cid => cid.ConcertId); c.Property(cid => cid.ConcertId).ValueGeneratedOnAdd(); c.Property(cl => cl.ConcertLocation).IsRequired().HasMaxLength(150); c.Property(cd => cd.ConcertDate).IsRequired().HasColumnType("date");
Add a Song Controller [Route("api/[controller]")] [ApiController] public class SongController : Controller { private ConcertDbContext _context; public SongController(ConcertDbContext context) this._context = context; } [HttpGet("")] public async Task<ActionResult<IEnumerable<Song>>> GetAllSongs() var data = await this._context.Songs.ToListAsync(); return Ok(data); [HttpPost("")] public async Task<ActionResult<Song>> CreateSong(Song song) var result = await this._context.AddAsync(song); await this._context.SaveChangesAsync(); return song;
Add a Concert Controller [Route("api/[controller]")] [ApiController] public class ConcertController : Controller { private ConcertDbContext _context; public ConcertController(ConcertDbContext context) this._context = context; } [HttpGet("")] public async Task<ActionResult<IEnumerable<Concert>>> GetAllConcert() var data = await this._context.Concerts.ToListAsync(); return Ok(data); [HttpPost("")] public async Task<ActionResult<Concert>> CreateConcert(Concert concert) var result = await this._context.AddAsync(concert); await this._context.SaveChangesAsync(); return concert;
Test It Out dotnet run
Tool Comparisons
Visual Studio Code vs. Visual Studio Cross-platform – versions available for Windows, Mac, and several flavors of Linux Rich Plugin Environment Lightweight Installation and Run Requirements Limited Scaffolding Support Within IDE No Integrated Test Runner (dotnet test only) Visual Studio Windows-only (Visual Studio for Mac does not provide a full experience) Rich Plugin Environment Heavyweight Installation and Run Requirements Full Scaffolding Support Within IDE Full Integrated Test Runner
Azure Data Studio vs. SQL Server Management Studio Recently Out of Beta Cross Platform – versions available for Windows, Mac, and several flavors of Linux Limited Plugin Environment, but Install from Within the IDE Still May Encounter Areas of Limited Functionality Designed to be Cloud-first SQL Server Management Studio On Version 17 (Version 18 is currently in RC1) Windows-only Limited Plugin Environment (and most are 3rd Party Installations) Fully Functional Designed to be On-premise-first
Questions?
Who am I? Dan Mallott Twitter: @DanielMallott Github: https://github.com/danielmallott LinkedIn: http://lnked.in/danmallott Principal for West Monroe Partners In the industry since 2011 Primary experience with SQL Server, starting with SQL Server 2005 Also worked with Oracle, PostgreSQL, and Cassandra Been both a DBA and a developer Have a couple Microsoft certifications DataStax Certified Professional