Skip to content

Commit fd03640

Browse files
author
ryancrawcour
committed
refactor of samples
1 parent 7f6a585 commit fd03640

File tree

1,201 files changed

+9083
-1634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,201 files changed

+9083
-1634
lines changed

Readme.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
The source code for the DocumentDB .NET Client SDK will be published here.
1+
Welcome to the repo containing all things .NET for Azure DocumentDB
2+
3+
In this repo you can find the following;
4+
5+
- **azure-mobileservices-net/** - A DocumentDB Domain Manager for Azure Mobile Services
6+
- **samples/** - a great source of sample code showing you how to use the .NET SDK in your projects.
7+
- **sdk/** - the source for the .NET SDK will be published here as soon as possible.
8+
- **tutorials/** - the source for some end-to-end applications built using this .NET SDK.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
<appSettings file="appSettings.config"/>
7+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{F244DE0C-FC94-4D96-AECF-C92BBBAA6627}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DocumentDB.Samples.CollectionManagement</RootNamespace>
11+
<AssemblyName>DocumentDB.Samples.CollectionManagement</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<SccProjectName>SAK</SccProjectName>
15+
<SccLocalPath>SAK</SccLocalPath>
16+
<SccAuxPath>SAK</SccAuxPath>
17+
<SccProvider>SAK</SccProvider>
18+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
19+
<RestorePackages>true</RestorePackages>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
22+
<PlatformTarget>AnyCPU</PlatformTarget>
23+
<DebugSymbols>true</DebugSymbols>
24+
<DebugType>full</DebugType>
25+
<Optimize>false</Optimize>
26+
<OutputPath>bin\Debug\</OutputPath>
27+
<DefineConstants>DEBUG;TRACE</DefineConstants>
28+
<ErrorReport>prompt</ErrorReport>
29+
<WarningLevel>4</WarningLevel>
30+
</PropertyGroup>
31+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
32+
<PlatformTarget>AnyCPU</PlatformTarget>
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>bin\Release\</OutputPath>
36+
<DefineConstants>TRACE</DefineConstants>
37+
<ErrorReport>prompt</ErrorReport>
38+
<WarningLevel>4</WarningLevel>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Microsoft.Azure.DocumentDB.1.2.0\lib\net40\Microsoft.Azure.Documents.Client.dll</HintPath>
43+
<Private>True</Private>
44+
</Reference>
45+
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
46+
<HintPath>..\packages\Newtonsoft.Json.5.0.7\lib\net45\Newtonsoft.Json.dll</HintPath>
47+
<Private>True</Private>
48+
</Reference>
49+
<Reference Include="System" />
50+
<Reference Include="System.Configuration" />
51+
<Reference Include="System.Core" />
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="Microsoft.CSharp" />
55+
<Reference Include="System.Data" />
56+
<Reference Include="System.Xml" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="Program.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="..\appSettings.config">
64+
<Link>appSettings.config</Link>
65+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
66+
</None>
67+
<None Include="App.config" />
68+
<None Include="packages.config" />
69+
</ItemGroup>
70+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
71+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
72+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
73+
<PropertyGroup>
74+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
75+
</PropertyGroup>
76+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
77+
</Target>
78+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
79+
Other similar extension points exist, see Microsoft.Common.targets.
80+
<Target Name="BeforeBuild">
81+
</Target>
82+
<Target Name="AfterBuild">
83+
</Target>
84+
-->
85+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
namespace DocumentDB.Samples.CollectionManagement
2+
{
3+
using Microsoft.Azure.Documents;
4+
using Microsoft.Azure.Documents.Client;
5+
using Microsoft.Azure.Documents.Linq;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Configuration;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
//-----------------------------------------------------------------------------------------------------------
13+
// This sample demonstrates the basic CRUD operations on a DatabaseCollection resource for Azure DocumentDB
14+
//
15+
// For advanced concepts like;
16+
// DocumentCollection IndexPolicy management please consult DocumentDB.Samples.IndexManagement
17+
//-----------------------------------------------------------------------------------------------------------
18+
19+
public class Program
20+
{
21+
private static DocumentClient client;
22+
23+
//Get an Id for your database & collection from config
24+
private static readonly string databaseId = ConfigurationManager.AppSettings["DatabaseId"];
25+
private static readonly string collectionId = ConfigurationManager.AppSettings["CollectionId"];
26+
27+
//Read the DocumentDB endpointUrl and authorisationKeys from config
28+
//These values are available from the Azure Management Portal on the DocumentDB Account Blade under "Keys"
29+
//NB > Keep these values in a safe & secure location. Together they provide Administrative access to your DocDB account
30+
private static readonly string endpointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
31+
private static readonly string authorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"];
32+
33+
public static void Main(string[] args)
34+
{
35+
try
36+
{
37+
//Get a DocumentClient
38+
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
39+
{
40+
RunDemoAsync().Wait();
41+
}
42+
}
43+
catch (DocumentClientException de)
44+
{
45+
Exception baseException = de.GetBaseException();
46+
Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
47+
}
48+
catch (Exception e)
49+
{
50+
Exception baseException = e.GetBaseException();
51+
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
52+
}
53+
finally
54+
{
55+
Console.WriteLine("End of demo, press any key to exit.");
56+
Console.ReadKey();
57+
}
58+
}
59+
60+
private static async Task RunDemoAsync()
61+
{
62+
//Try get a Database if exists, else create the Database resource
63+
Database database = await GetOrCreateDatabaseAsync(databaseId);
64+
65+
//Create a new collection on the Database
66+
DocumentCollection collection = await client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = collectionId });
67+
Console.WriteLine("Created Collection {0}.", collection);
68+
69+
//Read Collection Feed on the Database
70+
List<DocumentCollection> cols = await ReadCollectionsFeedAsync(database.SelfLink);
71+
foreach (var col in cols)
72+
{
73+
Console.WriteLine(col);
74+
}
75+
76+
//To list collections on a Database you could also just do a simple Linq query like this
77+
//The DocumentClient will transparently execute multiple calls to the Database Service
78+
//if it receives a continuation token. For larger sets of results the above method might be
79+
//preferred because you can control the number of records to return per call
80+
cols = client.CreateDocumentCollectionQuery(database.CollectionsLink).ToList();
81+
foreach (var col in cols)
82+
{
83+
Console.WriteLine(col);
84+
}
85+
86+
//Cleanup,
87+
//Deleting a DocumentCollection will delete everything linked to the collection.
88+
//As will deleting the Database. Therefore, we don't actually need to explictly delete the collection
89+
//it's just being done for demonstration purposes.
90+
await client.DeleteDocumentCollectionAsync(collection.SelfLink);
91+
await client.DeleteDatabaseAsync(database.SelfLink);
92+
}
93+
94+
/// <summary>
95+
/// This method uses a ReadCollectionsFeedAsync method to read a list of all collections on a database.
96+
/// It demonstrates a pattern for how to control paging and deal with continuations
97+
/// This should not be needed for reading a list of collections as there are unlikely to be many hundred,
98+
/// but this same pattern is introduced here and can be used on other ReadFeed methods.
99+
/// </summary>
100+
/// <returns>A List of DocuemntCollection entities</returns>
101+
private static async Task<List<DocumentCollection>> ReadCollectionsFeedAsync(string databaseSelfLink)
102+
{
103+
string continuation = null;
104+
List<DocumentCollection> collections = new List<DocumentCollection>();
105+
106+
do
107+
{
108+
FeedOptions options = new FeedOptions
109+
{
110+
RequestContinuation = continuation,
111+
MaxItemCount = 50
112+
};
113+
114+
FeedResponse<DocumentCollection> response = (FeedResponse<DocumentCollection>) await client.ReadDocumentCollectionFeedAsync(databaseSelfLink, options);
115+
116+
foreach (DocumentCollection col in response)
117+
{
118+
collections.Add(col);
119+
}
120+
121+
continuation = response.ResponseContinuation;
122+
123+
} while (!String.IsNullOrEmpty(continuation));
124+
125+
return collections;
126+
}
127+
128+
/// <summary>
129+
/// Get the database by name, or create a new one if one with the name provided doesn't exist.
130+
/// </summary>
131+
/// <param name="id">The name of the database to search for, or create.</param>
132+
private static async Task<Database> GetOrCreateDatabaseAsync(string id)
133+
{
134+
// Create a query object for database, filter by name.
135+
IEnumerable<Database> query = from db in client.CreateDatabaseQuery()
136+
where db.Id == id
137+
select db;
138+
139+
// Run the query and get the database (there should be only one) or null if the query didn't return anything.
140+
// Note: this will run synchronously. If async exectution is preferred, use IDocumentServiceQuery<T>.ExecuteNextAsync.
141+
Database database = query.FirstOrDefault();
142+
if (database == null)
143+
{
144+
// Create the database.
145+
database = await client.CreateDatabaseAsync(new Database { Id = id });
146+
}
147+
148+
return database;
149+
}
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DocumentDB.Samples.CollectionManagement")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DocumentDB.Samples.CollectionManagement")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("314deca1-c4c0-464b-b969-3ff216ef099c")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Azure.DocumentDB" version="1.2.0" targetFramework="net45" />
4-
<package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" />
4+
<package id="Newtonsoft.Json" version="5.0.7" targetFramework="net45" />
55
</packages>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
5+
</startup>
6+
<appSettings file="appSettings.config"/>
7+
</configuration>

0 commit comments

Comments
 (0)