[Music]
hello my name is norm a software
engineer on the dotnet team here at
address in this video we're gonna talk
about using Amazon downer to be with
dotnet if you haven't used entity before
it is a no sequel database that doesn't
require you to define a schema you just
defined the keys for your tables it is
also fully managed which means there are
no machines to maintain it is also easy
to scale when you create a table you
just specify the read and write
capacities to reserved for that table
these values can be raised or lowered
after you create the table as the
scaling needs of your tables change
without taking the table offline to use
DynamoDB with your done net applications
you need to add the ATP's SDK DynamoDB
v2 nougat package
the v2 refers to a large service API
change done several years ago the v2
suffix was added to the namespace and
the package to allow applications using
the original API to continue to work
alongside the new service API with the
Dino to be nougat package there are
three different api's you can use to
access Dino DB the first is using this
service client which is what we also
refer as the low-level API this API
provides full access to the done and B
API it does require you to write more
code when accessing derby because you
need to explicitly tell Danny B how to
store every piece of your data
the second API is the document model
with this API you use a document object
to store and retrieve your data this API
works really well when you're storing
unstructured data where each item could
have completely different attributes the
third API is the data model API where
you model your data as dotnet classes
and use a context object to save and
retrieve your data as instances of your
type classes now let's take a look at
these three api's in action here we are
in visual studio and I have a sample
application that is going to use all
three different API so each of these
samples
it's going to store a user in a user's
table and down interview I can create my
users table here in the ATIS explorer by
right-clicking on the note and say
create table specify name is users and
then I needed to find the key for my
table and a key is made up of a hash key
and optionally a range key you would use
a range key if you needed to provide
query capabilities within the scope of a
hash key in this example today I'm just
gonna set a hash key of ID and leave its
type as string we also would define the
region write capacities we want to
reserved for this table I'm just gonna
leave that the default for our demo
today I could continue further in this
wizard to also set up indexes for our
table but we're not gonna do that today
and just push create our tables gonna be
created and we can just start running
through our sample location to see how
that works so I'm gonna push f5 and
we'll step through our code and the
first one you can see is our low-level
sample in the low-level sample we create
our service client which is has all the
api's to die ODB to save an item we're
going to call the put item operation
we're going to use the async version for
the.net sdk we support dotnet framework
and dotnet core and dotnet core we only
support the async up relations so by
coding this in async i can use this same
exact code in both dotnet court and
dotnet framework the put operation takes
in a put item request and here you can
see is how we set our data first we say
which table we want to save our item in
and we create an item which is made up
of a dictionary of string and attribute
values now the attribute value is an
interesting object because that's what
tells that may be how you want to store
your data for example here is a storing
the ID and we're setting the s value on
the attribute value which tells them to
be that we want to store this value as a
string in the system and we've done the
same for first name last name and
address now active we have said we want
to store that as a boolean so we need to
call the bool property number of
children's is a number so even though it
is a string here we're setting the N
value which tells
envy this is a number and that's going
to allow us when we do query operations
to use query operators that are
appropriate for numbers we then have our
interest which is a list it's a list of
attribute values and this is where
things can get interesting because those
attribute values can also have lists and
maps underneath there and you can
imagine how that would be able to extort
more complex structures and our example
here though we're gonna just store a
list of strings so each one of those
attribute values were setting the s
property and then skills we're gonna set
as a map and a map is a dictionary of
string to attribute values and here
we're setting olives as numbers again
you can imagine we could also be using
lists and maps to create complex
structures there let's just go ahead and
step over this and now our item has been
saved in our table in fact if we go back
to our diving Explorer we can see we
have now save our record in the table
now to get our item back out on the
dynamic line we're going to call the get
item operation and it's going to take in
a get item request and we specify the
table name and we're going to turn on
consistent read for our demo application
because in our demo application we want
to be able to read the data that we just
saved if you do not have that
requirement where you don't need to read
data that was just saved you can set
that defaults and by setting that
defaults that cuts in half the Rica
pasady units consumed by your read
operation and then we need to specify
the key this is how we know which item
to load and it is again a hash map or a
dictionary is a dictionary because we
could have specified both a hash key and
a range key in our example we only said
a hash key so it's a dictionary of just
one item and again we're saying it's a
string and passing it our value passed
that and now we've got our item and we
can print that out you look at our
console you can see here is all the data
that we just stored into a baby now to
delete it we just call the delete item
operation and it's like the get where
you pass in a table name and
the key information now our item has
been deleted if we go back and we scan
our table we can see our data has been
removed so that's how you can use the
low-level API to access all of the
features but you do need to specify how
exactly you want to store all the data
on that attribute value next let's take
a look at the document model API so I'm
going to continue running and we'll step
into the next sample on the document
auto sample you can use that by
including the namespace Amazon DynamoDB
v2 document model it still uses the
service client but it wraps around it by
this table object and the table object
is what we use to load up all of our
documents so first we're gonna create
that by calling the load table method
which doesn't load the data it loads all
the metadata for our table it loads the
keys and the indexes that have been
configured for our table we pass in our
service client we tell it what table we
want to load this third property is
telling which conversion album to use
when converting the data from the
document down into that put item request
and setting the values in the attribute
value now the reason we need to set this
is when Donna B first launched it
contains support for strings numbers
sets and binary and so when you were
still boolean x' before in the old
version they were stored as numbers
zeros and ones later diabetes down to be
added support for boolean lists and maps
so to add support for soaring you did
and there we added the new algorithm but
we needed to make sure you set that on
there just say you wanted to use it so
any of your previous code that was
saving the values as numbers would still
be able to access that so here we have
our table object we're gonna start
creating a document annexing the
document we can just use our index or
just set all the values and here we
aren't saying which is a string which is
a number it's just using the types they
are in.net
so here's all our string properties
here's just setting our boolean and our
number and then andres is taking in a
list of strings and that conversion
algorithm is going to go figure out how
to convert those into the attribute
attribute values
data types for our mAb we actually
create another document and set all the
values there and then we set that
document unto the parent document and
that's how maps are done
then to save it on the table we call put
item passing in a document now our
object is saved
and to get it back out on the user table
which is called get item and we pass in
our key information we don't display
what the key is because when we loaded
the table up it pulls in all the
metadata for us so we've got our table
and here's our document and we can print
that out and we can see again it has the
same data as the first demo and then to
delete it we just call the delete item
passing in they value for our key and
our item has been deleted now let's
check out the de tamale pie with this
one we're gonna use typed classes to
represent our data so in here I have
written a class called user that
represents all the data I want to serve
my users table now by default this API
is going to use the name of the class in
the name of the properties to map and
data Ruby but I don't want to create a
class called users for one single user
I just called that user but I can set
this dynamic table attribute to map that
table to this class and the same with
properties here I've renamed my key to
be email and I've added the property to
remap that to the ID property for all
the others I'm just going to use the
default names for those if there's any
properties I don't want to save like
this full name I can add the ignore
property on there and it will not be
persisted to downer to be so let's go
back to the table and see how we use
this user object so first we're going to
create a context object and that takes
in a config and here on the config you
can see I've turned on consistent read
because for our example we're gonna read
de that we just saved and we're gonna
also say we want to use the latest
conversion algorithm next we're going to
create our users object and we're gonna
set all the values just like we would
any other dotnet class using an object
initialization in this case once we've
set that up on our context object we
call save on that let's go ahead and
skip to there and now our object has
been saved the context object used our
to figure out how to store the data back
into down Oh to be and I can get that
back out calling the load method which
takes in the user generic so it knows
which table to go pull that information
and it takes in the key information so
here we step through we have now a fully
loaded user object we can expand out and
you can see here is all of our data and
we can print that out again and see
again we're seeing all that same data
and then to delete it we call the delete
method passing in the generic so we know
which table we're talking about and the
key information and pass that and now
our our user has been deleted so you can
see we have at least three different
api's to be able to access donor to be
depending on your needs now another
common use case that we hear is people
would like to store json documents and
dude i'm to be or get jason tokens back
out so let's take a look at how we can
do that in this other demo i set that as
my start up project and this is a Web
API project and I have a control users
controller and in that controller I'm
going to be used the document model API
on the docket model API you can go get
my document and I'm those documents and
I say to JSON method so this is how you
can convert the data you get from Dino
to be into a JSON document and we can do
the same thing the reverse way so here
if I take in a JSON object on a document
object I can convert that to from JSON
to a document with that static method
from JSON let's go take a look at how
this works I'm going to start this up
and that's loaded I'm going to use
postman to use those api's I've saved a
couple requests here so let's expand
that out and first I want to post a user
so here is I'm going to hit my Web API
the users controller and I'm going to do
a post it's gonna post this button I'm
just gonna send it to JSON to push Send
and you can see here the breakpoint that
I got my JSON object and we're going to
convert that to a document and then
using our users table object we're gonna
go save that in there so we didn't have
to do anything with the data other than
convert it to a document and then save
it in there and then we could get the
same data out the cowl and get and here
on that we're gonna pass in the ID of
the user we want to get we'll do that in
a way P controller here's our user we're
gonna go use that as the key information
pull that out and then we're called the
- Jason on the document to convert that
to a JSON document and you can see is we
got our JSON document back from our web
server using our dyno - BAPI
so there you can see how you can also
use JSON as well and with data to be
there you can see with the three
different API options and the ability to
use JSON you have a lot of options and
how to use donor to be in your done
applications thank you for watching
[Music]