>> You may have heard of
planetary-scale databases with
Cosmos DB but did you
know that you can place
OData magic on top of that and have
a powerful query engine
that runs over the rest
API at your fingertips?
You've got to check this out.
[MUSIC]
Welcome to another episode of ON.NET.
This is a special OData
supercharged edition.
We've talked about
OData a few times and
today I've got her Hassan
with me again in the studio.
He's an avid user of OData,
a resident OData expert
and we're going to talk
about using it with
a nontraditional technology.
So I think a lot
of.NET developers are
used to SQL and relational databases,
but there's a whole other world
out there of other databases.
So tell us first who you are,
what you do and then let's dive
into this special instance.
>> Hi Jeremy, my name
is Hassan Habib.
I'm a software engineer
here at Microsoft.
I am at SILA organization which is
an organization that handles
all legal matters at Microsoft.
I work on daily basis with
different technologies to
provide better systems for
our internal employees
here at Microsoft,
and I get to play with a lot
of these amazing technologies
and try to put them into
real life scenarios
and learn from them and I'd like to
share that with rest of the world.
>> Awesome. Well let's share
planet-scale database with
the rest of the world.
>> Let's do it. So remember
when we talked about OData,
we said OData doesn't really have
any hard dependency on
the Entity Framework.
It's great to leverage
the Entity Framework.
It allows you to run SQL
or any blind expressions,
aligned the functions
with your C-Sharp code.
But you don't really have to have it.
Today I'm going to show you how
OData can work with Cosmos DB,
the exact same way we worked with
any other data source in
previous video about OData.
So what I did here,
this is my Azure portal.
>> Well, let's talk about
Cosmos DB real quick.
So Cosmos DB is?
>> It's an amazing technology,
storage technology that
allows people to rapidly
execute operations in
almost split milliseconds.
One of my favorite demos about
OData is Hanselman dead,
the way they the had an instance in
Japan that smearing and
instance here in the US and he
was drawing on the board that
sits the data in the instance
that's in Japan and it was reflecting
immediately on
the instance that's here.
That shows fact.
>> So out of the box distributed
database and there is
several APIs you can pick.
So if you're a MongoDB
developer, we support MongoDB.
If you're a SQL developer,
there's a SQL API that allows you
to use SQL-like syntax for that.
There's a graph API,
but it is a no SQL database,
which means it's not
necessarily relational,
it's so much structured data.
So there are properties
and values but
every record doesn't
have to be the same
which makes it unique
and interesting and
relative to certain types
of datasets and workloads.
But you've got a Cosmos DB
database setup here.
>> Yes. Absolutely.
I'm going to show you
first how do you create that.
So you can go here and if you click
''Add'' and then you get to select,
''Cosmos.'' Too much OData today.
So Azure Cosmos DB
and then you get to build
that, you create that.
Then you get to select
whatever API you want to use.
So in this instance, we
were using core SQL but you
get to choose actually
any kind you want.
While we're on the subject,
I want to talk to you
about something here.
Azure Table gives you OData built in.
So you don't have to
build an API in front of
Cosmos in order for
you to leverage OData.
It's actually built in functionality
that allows you to hit
the Cosmos instance endpoint with
your existing knowledge about OData
without having to write
one line of code,
that's down to zero and
I don't know how far.
>> Zero line.
>> Down to zero lines.
>> So were four lines of
code and some others.
>> Down to zero line.
>> So you are promising
zero. So we we've
got the API that we can pick and
Azure Table will play very nicely
with OData out of the box.
Then you've got location which is
interesting because
you've mentioned this,
Cosmos DB will geo replicate
to multiple locations,
so I can have one.
If I want my database
to crash and burn,
if that data-center goes
down or I can have a dozen
spread around the world and I don't
have to change my code, do I?
To handle that replication
across those nodes or so.
>> Because what we want to show
you like, if you're ready,
most of the people that
we're trying to talk to,
they already have APIs,
they want to use
different data sources.
I want you to go ahead
and explore with
Azure Table, that's out of the box.
I'm going to send, we're
going to put a link in
the description about
the official documentation
about Azure Table,
how to leverage OData
with Azure Table.
I want to show you today with
the core SQL Cosmos
how that works out.
So you just select core SQL and
click ''Review'' and ''Create''.
We're already created one, so we
don't have to create another one.
So I'm just going to go back here
to my resource groups in here,
and I have one in here
that's called CosmoswithSQL.
So here's how you get
started with this real quick and
you start learning about things.
If you go to the ''Quickstart'' part,
you'll notice they have a lot of
samples that you can
download and run,
plug-and-play without having to
write one line of code, right?
>> Yes.
>> So I'm going to go
to the .NET core one,
I'm just going to download an
existing sample that has all that.
The table key, and a private key,
and the URL, all of that.
>> Okay. So it's got their
credentials and it's all simple.
>> All set, automatically set up.
In addition to that,
let me extract that real
quick and then I'll show
you how much beautiful this is.
In addition to that, it
actually comes in with
prebuilt AMVCUI and
prebuilt repository pattern
to handle the data for you.
>> Okay. So we've already
abstracted the data,
we're not getting it
directly from our views?
>> Yes.
>> So it sounds like a place
where you might be able to
maybe sneak in a little code?
>> OData stuff. Yes. That's what
we're going to show you today.
Let's run the project first
and see how things go.
Don't download stuff
from the internet,
so this isn't a trust worthy.
>> You've got your-
>> You've got your warning.
>> You probably know
about this warning too,
you are in legal, right?
>> Yes. Absolutely. So
this is quickstart core.
It has everything built for us.
Let me just rebuild the project
real quick so it can retrieve.
You can also do
load project dependencies.
I'm just going to rebuild
it so I can run it,
but you can also load project's
dependencies if you decide to
change your NuGet packages
at any point in time.
So I'm just going to rebuild
the project here real quick.
Rebuild succeeded or so it claims.
Now, give it a second there.
>> So you know the trick here,
right if it ever slows
down just move the mouse.
>> Yeah. I was to do
like this. All right.
So we have that. Let
me show you what the
project that comes out of the box.
You build a Cosmos DB.
If I run my project right now,
it's already connected to
my Cosmos DB and I get to control
the data full CRUD Operations,
delete details, edit, and create.
>> So that's a nice example
out of the box.
You get all the Operations.
You'd probably want to performing,
wait, we're pulling up something
and it's a To-Do List, awesome.
>> Yeah. So we created a sample
To-Do List and then you get to go,
let's just create one for fun.
So let's say, "Jeremy is awesome."
>> I like that To-Do.
Well, why isn't To-Do?
In your implying that
this is something that
can happen in the future.
>> To-Do Jeremy is also okay.
Let's say, "Brush Your Teeth."
I guess Brush Your Teeth.
>> Jeremy is awesome as well.
>> Anyways, either way.
So Brush Your Teeth,
Brush Your Teeth.
That's from the previous session,
so the Control C. Okay, right?
If I go and create,
people who are familiar with
CRUD Operations with ASP.NET MVC,
they know exactly
they're very familiar
with this native bootstrap.
You get To-Do full operation
delete and whatnot, right?
>> Okay.
>> This list here is built to list
tasks that are not completed,
the tasks that are
completed disappear.
>> But they are still there.
>> They're just send
their in the database.
But just to make sure that
this is actually is the case.
Again, you don't have to have
a lot of knowledge there.
If you're already familiar with SQL,
there is a data exploring
here that shows you.
So I have a ToDoList in here
and I have bunch of Items.
If you'll look, I have
already an Item in here,
"You should wash
your teeth everyday."
Wash your teeth. This is not
that exact same one that we've
created. This is a different one.
So we can create
however many we want.
But what I want to show
you here today is,
out of this example on probably
people watching us right now,
they could probably do
that by themselves.
If you look at the example,
it already comes in with
IDocumentDBRepository,
that's our Repository
pattern and they have
the Document Repository Pattern
which have all the secrets
and the keys,
and it comes in with
all the operations that I need.
I have no Entity Framework
in here just pre-built
client for me to interact and full
CRUD Operations with Cosmos DB.
>> Okay.
>> So now you'd think,
the next thing I want
to do is just add
OData and it is magic
and see what happens.
>> Okay.
>> So the first thing I want to do,
I'm just going to go in here
just like we did last time
and Manage OData NuGet Packages.
If you go to browse, type in OData.
This is Microsoft and we
want the one with the Core,
ASP.NET Core OData since we
downloaded the 2.2 Core OData.
So I'm just going to
install that real quick.
Second, let say yes, OData, right?
>> Okay.
>> Then I want to add
a controller in here.
All of this like, if you're using
Visual Studio this has
been around for a while.
You can have full CRUD Operations
generated for you out of the box.
So if I choose API Controller
with read writes,
so I'm not using the
Entity Framework here.
I'm just going to go in
here until it build me
full CRUD Operations for Items.
So that's my ItemsController,
and then I'm going to create that.
I'll just create a sample Full CRUD
for you for an API Controller.
We got just that in a second.
>> So much nicer,
I'm going to show
my age a little bit.
I've been developing for many years
and it used to be File New,
and then you're scrambling
to find examples.
Searching on the web, go
into bulletin boards,
dialing into momentum in BBS.
Okay, maybe to further. This is
nicer we get this scaffolded for us.
So we've got a template
that we can work from.
>> Yes. So this is to say
created a template for us.
We have everything here that we need.
Now, before we add stuff in here,
let's just test the thing itself,
lets use the Document Repository.
So if you look here at the existing
control that we were using,
it's called the ItemController
already it does
some initiation for you
for the repository.
So I'm just going to
steal that from here.
I'm going to steal that in here.
I'm going to select this.
I'm going to go to
the ItemsController.
I'm going to put this right here.
The only difference here
is that the constructor is
ItemsController instead of
ItemController, like that.
Then I just want to pull in
if you do control period,
it will pull in whatever
this IDocumentRepository is defined.
>> Okay.
>> So I just insane
guided repo ready.
I got everything ready for me.
>> Okay.
>> The other thing I want is
from the ItemController here,
as you can see,
it's returning a bunch of items
that are not completed, right?
The items are not completed.
We going to return
the items that are completed.
So I'm confusing myself,
just to show people what's out there.
We could create new ones.
We have the UI.
We have everything. So
I can just do that.
So this is returning
Repository.GetItemsAsync, right?
The difference here is that,
I want to return on an iron
innumerable for the item
instead of a string.
So let me do that in here.
Then in addition to that,
this is an awaitable call.
So we want a sink and
a task that encapsulates.
>> Yeah.
>> This goal, there we
go. Everything is happy.
>> Okay.
>> Now, we need to put
that to test, right?
So I'm saying give me the items
that are complete not completed.
>> So far we've added
the OData Package,
but no data has been involved
in the making of this demo.
>> Yes. We're just creating
a controller that just
returns a bunch of things.
>> Okay.
>> So that's run, let see what's
the endpoint here /api/items, right?
So I'm just going to go ahead
in here and run my project.
Give it a second there.
>> Tony you just got
to crank that mouse.
>> You got to okay, let see.
>> How the spinners
going the other way.
>> Yes. So that's the trick.
>> Yeah.
>> That's the trick. Okay. So this
is our normal CRUD Operations.
If I go and say, we know that
this list is only displaying
the items are not completed
which makes sense because
it's a To-Do List.
Why would you see items
that are completed,
and then you go /api/items.
If I hit that endpoint,
what happens? It's returning.
So I just turned a View Controller
into a Normal Controller,
no magic in there.
>> Okay.
>> Right. Now, comes
the interesting part.
We want to add OData to this, right?
So just like we did last time,
if I go into Services in
here and say, Services add OData.
Then I'll just have
the control period just to pull
in the library that the NuGet
Package that we just pulled in.
>> Okay.
>> So that's one part. Then again,
down here at the routes level this
is already broken down for us.
So we don't have to
do anything, routes.
and then I want to enable
dependency injection.
Remember how we said,
we want to enable
the dependency injection for
OData Services to be in effect and
I also want to have the expand,
the select all kinds
of stuff that I want.
>> Right, this is that opt-in of
the different functionality
that you're going to expose.
>> Exactly. So order by
whatever is out there, right?
>> Okay.
>> All right. What else is left?
What's left is that,
I want to go back to
my ItemsController
and add EnableQuery.
Right?
>> Okay.
>> What happens now?
We'll see let's find out.
Let's run this and see what happens.
>> I can hear the music
playing in the background.
It's building up
the exciting moment. All right.
>> All right. Let's see what happens.
So if I go and say /api/items,
we want to make sure the
system is still working.
So system is still working.
But if I go in and say, I don't
really care about all of that,
I want some OData magic give
me just the name of the item.
Boom, I find.
Wow, that's it.
>> That's it.
>> Whatever we talked about
in the past about OData,
like you don't need
Entity Framework
whatever storage you have good.
>> No, I'm just saying, wow,
that seemed too easy.
>> Yes.
>> So basically, if you'd
chosen Azure Table storage.
You're saying, we'd get
this out of the box.
But if we don't,
we add a package, configure it,
and tag the controller endpoint
and we're back in the races.
>> The only difference
is four lines of code.
It's up to you to decide
what do you want to do.
>> That's amazing. So
I understand that,
you have a blog post that's
tied into this functionality.
It seems easy but if you want
to follow through step-by-step,
we've got a link for you,
it's aka.ms/odata-cosmosdb that's
O-D-A-T-A hyphen C-O-S-M-O-S-DB,
OData-CosmosDB, check
it out. Thank you sir.
>> Yeah. Absolutely. Sir, thank you.
>> Cosmos DB.
>> Cosmos DB.
[MUSIC].
