View
9
Download
0
Category
Preview:
Citation preview
Copyright 2017 Code Career Academy, LLC
Cosmos DB and ASP.NET CoreCreating Your First Project!
Tampa Code Camp 2017
Jeff AmmonsChief Instructor
Code Career Academy
Copyright 2017 Code Career Academy, LLC
Who am I?
Jeff Ammons
Microsoft MVP
Pluralsight Author
CEO/Chief Instructor at Code Career
Academy
25 Years Professional Experience in Software
Copyright 2017 Code Career Academy, LLC
Hey Jeff, Could You Please Set My Expectation Levels?
Copyright 2017 Code Career Academy, LLC
What is the focus of THIS Cosmos DB talk?
• Think Average…• How can Cosmos DB help the…
• Average developer working for the• Average company building the• Average web app?
Copyright 2017 Code Career Academy, LLC
What is the focus of THIS Cosmos DB talk?
• Think Average…• How can Cosmos DB help the…
• Average developer working for the• Average company building the• Average web app?
Copyright 2017 Code Career Academy, LLC
What is the focus of THIS Cosmos DB talk?
• Think Average…• How can Cosmos DB help the…
• Average developer working for the• Average company building the• Average web app?
Copyright 2017 Code Career Academy, LLC
What is the focus of THIS Cosmos DB talk?
• Think Average…• How can Cosmos DB help the…
• Average developer working for the• Average company building the• Average web app?
Copyright 2017 Code Career Academy, LLC
What is Cosmos DB?
Copyright 2017 Code Career Academy, LLC
It is a…
• Globally replicated• Multi-modal database• Offering
• Rich querying over schema-free data
• Configurable and reliable performance
• Faster development
Copyright 2017 Code Career Academy, LLC
It is a…
• Globally replicated• Multi-modal database• Offering
• Rich querying over schema-free data
• Configurable and reliable performance
• Faster development
Be all the things!
Copyright 2017 Code Career Academy, LLC
Globally Replicated
• Click a hexagon• Replicate to that datacenter• Low latency SLA
• 10 ms reads and 15 ms writes
Copyright 2017 Code Career Academy, LLC
Multi-Modal
• Key/Value Store
Copyright 2017 Code Career Academy, LLC
Multi-Modal
• Key/Value Store• Document Database
Copyright 2017 Code Career Academy, LLC
Multi-Modal
• Key/Value Store• Document Database• Graph Database
Copyright 2017 Code Career Academy, LLC
Multi-Modal
• Key/Value Store• Document Database• Graph Database• More to come!
Copyright 2017 Code Career Academy, LLC
Multi-Modal
• Key/Value Store• Document Database• Graph Database• More to come!
• Pick the right tool for the job
Copyright 2017 Code Career Academy, LLC
Rich Querying Over Schema-Free Data
• SQL Interface• Mongo DB Interface• Table Interface• Graph Interface
Copyright 2017 Code Career Academy, LLC
Rich Querying Over Schema-Free Data
• SQL Interface• Mongo DB Interface• Table Interface• Graph Interface
• Dump your data in a bucket• We'll index everything• No matter what it is• SuperCaliMagicData-
ExpediteYourQueries
Copyright 2017 Code Career Academy, LLC
Configurable and Reliable Performance
• Just slide the sliders• and click the hexagons!
Copyright 2017 Code Career Academy, LLC
Faster Development
• Avoid object/relational mapping
• Worry less about schema changes
Copyright 2017 Code Career Academy, LLC
So…
• Is it really magic?
Copyright 2017 Code Career Academy, LLC
Of course not, but…
• It's pretty darned good!
Copyright 2017 Code Career Academy, LLC
So What Can I Use Today?
• Average Company, Developer, App is likely to want• DocumentDB
• Lose the schema• Save some time• Gain some speed• Keep SQL ad hoc queries
Copyright 2017 Code Career Academy, LLC
Document DB Concepts• Your objects are stored as
Documents (JSON)
• Documents are stored in Collections
• You need to partition your data
• HINT: This is crucial
• Your data is denormalized
DocumentDBAccount
Database/dbs/{id}
Collections/colls/{id}
Documents/docs/{id}
Attachments/attachments/{id}
Users/users/{id}
Permissions/permissions/{id}
Stored Procedures/sprocs/{id}
Triggers/triggers/{id}
User Defined Functions/functions/{id}
Copyright 2017 Code Career Academy, LLC
What's the Big Deal About Schemaless?
Pros• No need to define tables• No need to map objects to
tables• Store objects as objects• Changing code is faster since
you don't have to also change tables
Cons• Nothing enforces your structure• Nothing enforces relationships• Data can be stored in multiple
places
Copyright 2017 Code Career Academy, LLC
You Have to Pick the Right ToolNeither approach is perfect
Copyright 2017 Code Career Academy, LLC
What is Partitioning?
Copyright 2017 Code Career Academy, LLC
Subdivide Your Data
• Pick a partition key• Partitions can be moved around• You don't manage partitions, Azure does• Picking your key is… well… key to performance and/or price!
https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data
Copyright 2017 Code Career Academy, LLC
So What's the Biggest Pain Point?
Copyright 2017 Code Career Academy, LLC
Danger!
• You can go broke!• Either a
• great value or • eat you alive!
• Danger Zones• Collections
• You pay for each!• Request Units
• $5.92/ 100 RUs/sec/month• MINIMUM 400!• BEWARE "OfferThroughput"!
• Examples show 3,000 & 5,000
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
NO! NO! NO!
• OfferThroughput = 20000!!!• 20000/100 = 200• 200 * $5.592 = $1,190.40• PER MONTH!
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-partition-data
Copyright 2017 Code Career Academy, LLC
But wait!
• How about the "Azure Document DB Overview and Offline Development" video?
Copyright 2017 Code Career Academy, LLC
But wait!
• How about the "Azure Document DB Overview and Offline Development" video?
Copyright 2017 Code Career Academy, LLC
But wait!
• How about the "Azure Document DB Overview and Offline Development" video?
Copyright 2017 Code Career Academy, LLC
But wait!
• How about the "Azure Document DB Overview and Offline Development" video?
• $15,000 a month!• Great Googly Moogly!
Copyright 2017 Code Career Academy, LLC
Copyright 2017 Code Career Academy, LLC
s Your Boss
Copyright 2017 Code Career Academy, LLC
To Be Clear…
• I'm not upset the high priced options exist…
• Just that they casually use those values in the basic documentation!
Copyright 2017 Code Career Academy, LLC
Burn This Into Your Brain RIGHT NOW
OfferThroughput = 400;
Copyright 2017 Code Career Academy, LLC
Burn This Into Your Brain RIGHT NOW
OfferThroughput = 400; • Today, Tomorrow, and Always
Copyright 2017 Code Career Academy, LLC
How Can I Minimize Costs?
Copyright 2017 Code Career Academy, LLC
Take the Big Bucket Approach
• DON'T think of collections as tables!• You can put all kinds of different data into the bucket• The Key is partitioning!
Copyright 2017 Code Career Academy, LLC
ExampleCar
ID: 123Year: 2012Make: Ford
Model: Fusion
CarID: 125
Year: 2017Make: HondaModel: Accord
CarID: 124
Year: 2015Make: Toyota
Model: Tundra
CustomerID: 456
FirstName: BobLastName: Smith
CustomerID: 458
Year: 2017FirstName: SamLastName: Jones
CustomerID: 457
Year: 2015FirstName: JoeLastName: Wills
Copyright 2017 Code Career Academy, LLC
Example: Option 1Car
ID: 123Year: 2012Make: Ford
Model: Fusion
CarID: 125
Year: 2017Make: HondaModel: Accord
CarID: 124
Year: 2015Make: Toyota
Model: Tundra
CustomerID: 456
FirstName: BobLastName: Smith
CustomerID: 458
Year: 2017FirstName: SamLastName: Jones
CustomerID: 457
Year: 2015FirstName: JoeLastName: Wills
Collection 1
Collection 2
Copyright 2017 Code Career Academy, LLC
Car Collection
Customer Collection
Example: Option 1Car
ID: 125Year: 2017
Make: HondaModel: Accord
CarID: 124
Year: 2015Make: Toyota
Model: Tundra
CustomerID: 458
Year: 2017FirstName: SamLastName: Jones
CustomerID: 457
Year: 2015FirstName: JoeLastName: Wills
CarID: 123
Year: 2012Make: Ford
Model: Fusion
CustomerID: 456
FirstName: BobLastName: Smith
~$25/mo
~$25/mo
Copyright 2017 Code Career Academy, LLC
Collection 1
Customer Partition
Car Partition
Example: Option 2
CarID: 125
Year: 2017Make: HondaModel: Accord
Type: Car
CarID: 124
Year: 2015Make: Toyota
Model: TundraType: Car
CustomerID: 458
Year: 2017FirstName: SamLastName: JonesType: Customer
CustomerID: 457
Year: 2015FirstName: JoeLastName: WillsType: Customer
CarID: 123
Year: 2012Make: Ford
Model: FusionType: Car
CustomerID: 456
FirstName: BobLastName: SmithType: Customer
~$25/mo
Copyright 2017 Code Career Academy, LLC
Choose Carefully
• Option 2 partitions for the best price, not best performance• Depending on your needs you may need to partition on some other
attribute• Sometimes paying more is the right choice!
Copyright 2017 Code Career Academy, LLC
So Schemaless Means Not Thinking About Structure?• No!• How you break up your objects is very important.• Remember in general:
• Normalized data makes for faster writes• Denormalized data makes for faster reads/fewer trips to the source
Copyright 2017 Code Career Academy, LLC
Modeling Your Data
Concept mapping:Embedded Data == DenormalizedReferenced Data == Normalized (more or less…)
Copyright 2017 Code Career Academy, LLC
Referenced
• Fast writes• Two queries to get data
CarID: 123
Year: 2012Make: Ford
Model: FusionOwnerId: 456
CustomerID: 456
FirstName: BobLastName: Smith
Cars: [CarId: 123
]
Copyright 2017 Code Career Academy, LLC
Embedded
• Fast reads (one trip)• Data may be duplicated
CustomerID: 456
FirstName: BobLastName: Smith
Cars: [
]
CarID: 123
Year: 2012Make: Ford
Model: Fusion
Copyright 2017 Code Career Academy, LLC
Difference from Relational DB
Document• Weak references
• Nothing enforces them
Relational• Strong references
• Enforced by DB Engine
Copyright 2017 Code Career Academy, LLC
How About Another Pain Point?
Copyright 2017 Code Career Academy, LLC
Auto Incrementing IDNope.
Sorry.
Gotta getta GUID.
Or roll your own in stored procedure.
Copyright 2017 Code Career Academy, LLC
Paging DataNope.
Sorry.
We have TAKE, but not SKIP.
It's coming…
Copyright 2017 Code Career Academy, LLC
So How Do We Get Started?
Copyright 2017 Code Career Academy, LLC
Where Are the Docs?
https://docs.microsoft.com/en-us/azure/cosmos-db/
Copyright 2017 Code Career Academy, LLC
Be Careful With Docs!
This isn't Core!
https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-documentdb-dotnet
Copyright 2017 Code Career Academy, LLC
Here's the Core Walk Through!
Here's the Core one!
But… it's not ASP
https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-dotnetcore-get-started
Copyright 2017 Code Career Academy, LLC
Grab the Emulator!
• Run locally• https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator
• When you run it you will get a little web server running locally• Browser will pop up
https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator
Copyright 2017 Code Career Academy, LLC
Create Your Base App
• Whatever kind of app you want, but I like• ASP.NET Core MVC• You know the drill: File, New, Project…
• If you are doing Core, be sure you have the Preview 2 or better of VS 2017
Copyright 2017 Code Career Academy, LLC
NuGet Yourself Some Cosmos DB Love
• Microsoft.Azure.DocumentDB OR• Microsoft.Azure.DocumentDB.Core
• Depending on whether you're doing Core yet or not
Copyright 2017 Code Career Academy, LLC
Create Your Collection
• You can do so manually in the Portal OR• do it via code
Copyright 2017 Code Career Academy, LLC
Create Your Domain Classes
• Use JsonProperties to prep the JSON documents
public class Item
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
Copyright 2017 Code Career Academy, LLC
Create Your Domain Classes
• Use JsonProperties to prep the JSON documents
public class Item
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
• You must have an ID column that is a string
• Even if you use a GUID or Integer
Copyright 2017 Code Career Academy, LLC
Gut Check Time
Copyright 2017 Code Career Academy, LLC
Which is More Important?
• Super pure OOP? • Easier/faster development?
Copyright 2017 Code Career Academy, LLC
Options
Super Pure OOP• Create a domain object that is
pure• Create a document object for
each domain object• Keep the document objects
inside your repository
Down and Dirty• Create a domain object that
has:• ID as a string• Type as a string
• Pass this object around
Copyright 2017 Code Career Academy, LLC
Basically EF's DilemmaBut not as much extra junk
Copyright 2017 Code Career Academy, LLC
Either Way…
• I'd encapsulate the Cosmos Code in a repository
Copyright 2017 Code Career Academy, LLC
Startup.csConfigureServices Method
If You Haven't Worked with Core Yet…• The biggest difference is:
• Use Dependency Injection to get your repo into your controller
services.AddScoped(); services.AddScoped();
services.AddScoped();
services.AddScoped();
Copyright 2017 Code Career Academy, LLC
Startup.csConfigureServices Method
If You Haven't Worked with Core Yet…• The biggest difference is:
• Use Dependency Injection to get your repo into your controller
services.AddScoped(); services.AddScoped(); For Down & Dirty You Only
Need the Second line here
services.AddScoped();
services.AddScoped();
Copyright 2017 Code Career Academy, LLC
If You Haven't Worked with Core Yet…• The biggest difference is:
• Use Dependency Injection to get your repo into your controller
In Your Controller
public class HolidayManagerController : Controller { private readonly IHolidayRepository _holidayRepo; public HolidayManagerController(IHolidayRepository holidayRepo) { _holidayRepo = holidayRepo; }
public class HolidayManagerController : Controller
{
private readonly IHolidayRepository _holidayRepo;
public HolidayManagerController(IHolidayRepository holidayRepo)
{
_holidayRepo = holidayRepo;
}
Copyright 2017 Code Career Academy, LLC
Configure Your Project for the Emulator
• Right Click the project and select Manage User Secrets• This opens secrets.json (shh!)• This does NOT get checked in: stays on the developer's machine
• Paste into the secrets file the following (get it from the PDF…)• These values are the same for everyone• Put the real values on your server (App Settings Env Vars for App Svcs)
"cosmosdb:studentportal:authKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"cosmosdb:studentportal:endpoint": "https://localhost:8081"
Copyright 2017 Code Career Academy, LLC
Questions?
Copyright 2017 Code Career Academy, LLC
Follow Me on The Web
• Blog: jeffa.tech• CCA: CodeCareerAcademy.com• Facebook: https://www.facebook.com/CodeCareerAcademy/• Twitter: jeffa00• Twitter: codecareeracad
Copyright 2017 Code Career Academy, LLC
Intro to Web Development
•First 3 weeks of all certificate based programs.•Study at your own pace•Price deducted from any future class you take.
•Can’t commute to our campus? Just opt in to our fully remote program!•Take our part-time classes from the comfort of your own home!
$600
Fully Remote Classes
CodeCareerAcademy.cominfo@codecareeracademy.com
Copyright 2017 Code Career Academy, LLC
Part-Time, Front-End Web DevelopmentJanuary 23, 2018 - April 30, 2018
•Become Front-End Certified•Create web sites using HTML, CSS + JavaScript•Study with Sam Luedke, Lead Front-End Developer•Build Projects for your Portfolio!
Part-Time, Full-Stack Web DevelopmentMarch 12, 2018 - October 1, 2018
•Become Full-Stack Certified•Evenings + Weekends•Remote in via WebEx or attend In-Person•Learn Front-End, C#, ASP.Net, and SQL•Study Under Jeff Ammons, Microsoft MVP
$4,000
$8,900
CodeCareerAcademy.cominfo@codecareeracademy.com
Cosmos DB and ASP.NET CoreWho am I?Hey Jeff, Could You Please Set My Expectation Levels?What is the focus of THIS Cosmos DB talk?What is the focus of THIS Cosmos DB talk?What is the focus of THIS Cosmos DB talk?What is the focus of THIS Cosmos DB talk?What is Cosmos DB?It is a…It is a…Globally ReplicatedMulti-ModalMulti-ModalMulti-ModalMulti-ModalMulti-ModalRich Querying Over Schema-Free DataRich Querying Over Schema-Free DataConfigurable and Reliable PerformanceFaster DevelopmentSo…Of course not, but…So What Can I Use Today?Document DB ConceptsWhat's the Big Deal About Schemaless?You Have to Pick the Right ToolWhat is Partitioning?Subdivide Your DataSo What's the Biggest Pain Point?Danger!NO! NO! NO!NO! NO! NO!NO! NO! NO!NO! NO! NO!NO! NO! NO!NO! NO! NO!NO! NO! NO!NO! NO! NO!But wait!But wait!But wait!But wait!Slide Number 43sTo Be Clear…Burn This Into Your Brain RIGHT NOWBurn This Into Your Brain RIGHT NOWHow Can I Minimize Costs?Take the Big Bucket ApproachExampleExample: Option 1Example: Option 1Example: Option 2Choose CarefullySo Schemaless Means Not Thinking About Structure?Modeling Your DataReferencedEmbeddedDifference from Relational DBHow About Another Pain Point?Auto Incrementing IDPaging DataSo How Do We Get Started?Where Are the Docs?Be Careful With Docs!Here's the Core Walk Through!Grab the Emulator!Create Your Base AppNuGet Yourself Some Cosmos DB LoveCreate Your CollectionCreate Your Domain ClassesCreate Your Domain ClassesGut Check TimeWhich is More Important?OptionsBasically EF's DilemmaEither Way…If You Haven't Worked with Core Yet…If You Haven't Worked with Core Yet…If You Haven't Worked with Core Yet…Configure Your Project for the EmulatorQuestions?Follow Me on The WebSlide Number 84Slide Number 85
Recommended