diff --git a/Source/Bifrost.DocumentDB/Bifrost.DocumentDB.csproj b/Source/Bifrost.DocumentDB/Bifrost.DocumentDB.csproj new file mode 100644 index 000000000..d04454915 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Bifrost.DocumentDB.csproj @@ -0,0 +1,85 @@ + + + + + Debug + AnyCPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D} + Library + Properties + Bifrost.DocumentDB + Bifrost.DocumentDB + v4.5 + 512 + ..\Solutions\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\Bifrost.DocumentDB.XML + true + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\Solutions\packages\Microsoft.Azure.Documents.Client.0.9.0-preview\lib\net40\Microsoft.Azure.Documents.Client.dll + + + ..\Solutions\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + + + + {fb310720-cd89-44b6-bd54-7861f65d8466} + Bifrost + + + + + + + + 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}. + + + + + \ No newline at end of file diff --git a/Source/Bifrost.DocumentDB/Entities/ConfigurationExtensions.cs b/Source/Bifrost.DocumentDB/Entities/ConfigurationExtensions.cs new file mode 100644 index 000000000..dcf5bcd03 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Entities/ConfigurationExtensions.cs @@ -0,0 +1,86 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using System; +using Bifrost.Configuration; +using Bifrost.DocumentDB.Entities; + +namespace Bifrost.Configuration +{ + /// + /// Extensions for configuration + /// + public static partial class ConfigurationExtensions + { + /// + /// Configures storage to use a DocumentDB + /// + /// Storage to configure + /// Chained callback for configuring the specifics + /// Chained for fluent configuration + public static IConfigure UsingDocumentDB(this IHaveStorage storage, Action callback) + { + var configuration = new EntityContextConfiguration(); + callback(configuration); + + var connection = new EntityContextConnection(configuration); + configuration.Connection = connection; + + storage.EntityContextConfiguration = configuration; + + return Configure.Instance; + } + + + /// + /// Configure the Url endpoint for the database server + /// + /// to configure + /// + /// Chained to configure + public static EntityContextConfiguration WithUrl(this EntityContextConfiguration configuration, string url) + { + configuration.Url = url; + return configuration; + } + + /// + /// Configure the default database by its databaseId + /// + /// to configure + /// Database id to connect to + /// Chained to configure + public static EntityContextConfiguration WithDefaultDatabase(this EntityContextConfiguration configuration, string databaseId) + { + configuration.DatabaseId = databaseId; + return configuration; + } + + /// + /// Configure the authorization key to use + /// + /// to configure + /// Authorization key to use + /// Chained to configure + public static EntityContextConfiguration UsingAuthorizationKey(this EntityContextConfiguration configuration, string authorizationKey) + { + configuration.AuthorizationKey = authorizationKey; + return configuration; + } + } +} diff --git a/Source/Bifrost.DocumentDB/Entities/EntityContext.cs b/Source/Bifrost.DocumentDB/Entities/EntityContext.cs new file mode 100644 index 000000000..ce8542d50 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Entities/EntityContext.cs @@ -0,0 +1,98 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using System; +using System.Linq; +using Bifrost.Entities; +using Microsoft.Azure.Documents; +using Microsoft.Azure.Documents.Linq; + +namespace Bifrost.DocumentDB.Entities +{ + /// + /// Represents an implementation of specifically for DocumentDB + /// + /// Type of entity + public class EntityContext : IEntityContext + { + EntityContextConnection _connection; + DocumentCollection _collection; + + /// + /// Initializes a new instance of + /// + /// + public EntityContext(EntityContextConnection connection) + { + _connection = connection; + _collection = connection.GetCollectionFor(); + } + +#pragma warning disable 1591 // Xml Comments + public IQueryable Entities + { + get + { + var queryable = _connection.Client.CreateDocumentQuery(_collection.DocumentsLink); + return queryable; + } + } + + public void Attach(T entity) + { + } + + public void Insert(T entity) + { + _connection.Client.CreateDocumentAsync(_collection.DocumentsLink, entity); + } + + public void Update(T entity) + { + _connection.Client.ReplaceDocumentAsync(_collection.DocumentsLink, entity); + } + + public void Delete(T entity) + { + + } + + public void Save(T entity) + { + _connection.Client.ReplaceDocumentAsync(_collection.DocumentsLink, entity); + } + + public void Commit() + { + } + + public T GetById(TProperty id) + { + throw new NotImplementedException(); + } + + public void DeleteById(TProperty id) + { + } + + public void Dispose() + { + } +#pragma warning restore 1591 // Xml Comments + } +} diff --git a/Source/Bifrost.DocumentDB/Entities/EntityContextConfiguration.cs b/Source/Bifrost.DocumentDB/Entities/EntityContextConfiguration.cs new file mode 100644 index 000000000..ed5765118 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Entities/EntityContextConfiguration.cs @@ -0,0 +1,52 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using System; +using Bifrost.Configuration; +using Bifrost.Entities; + +namespace Bifrost.DocumentDB.Entities +{ + /// + /// Implements the specific for the DocumentDB support + /// + public class EntityContextConfiguration : IEntityContextConfiguration + { + /// + /// Gets or sets the url endpoint for the database server + /// + public string Url { get; set; } + + /// + /// Gets or sets the database id + /// + public string DatabaseId { get; set; } + + /// + /// Gets or sets the authorization key + /// + public string AuthorizationKey { get; set; } + + +#pragma warning disable 1591 // Xml Comments + public Type EntityContextType { get { return typeof(EntityContext<>); } } + + public IEntityContextConnection Connection { get; set; } +#pragma warning restore 1591 // Xml Comments + } +} diff --git a/Source/Bifrost.DocumentDB/Entities/EntityContextConnection.cs b/Source/Bifrost.DocumentDB/Entities/EntityContextConnection.cs new file mode 100644 index 000000000..cfc234ad0 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Entities/EntityContextConnection.cs @@ -0,0 +1,112 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using System; +using System.Linq; +using Bifrost.Entities; +using Bifrost.Execution; +using Microsoft.Azure.Documents; +using Microsoft.Azure.Documents.Client; + +namespace Bifrost.DocumentDB.Entities +{ + /// + /// Represents an implementation of + /// + public class EntityContextConnection : IEntityContextConnection + { + EntityContextConfiguration _configuration; + + /// + /// Initializes a new instance of + /// + /// Configuration to use + public EntityContextConnection(EntityContextConfiguration configuration) + { + _configuration = configuration; + } + + /// + /// Gets the for the connection + /// + public DocumentClient Client { get; private set; } + + /// + /// Gets the for the connection + /// + public Database Database { get; private set; } + + + /// + /// Get a for a specific type + /// + /// Type to get collection for + /// The for the type + public DocumentCollection GetCollectionFor() + { + return GetCollectionFor(typeof(T)); + } + + + /// + /// Get a for a specific type + /// + /// Type to get collection for + /// The for the type + public DocumentCollection GetCollectionFor(Type type) + { + DocumentCollection collection = null; + + var collectionName = type.Name; + + Client.ReadDocumentCollectionFeedAsync(Database.SelfLink) + .ContinueWith(f => collection = f.Result.Where(c => c.Id == collectionName).SingleOrDefault()) + .Wait(); + + if (collection == null) + { + collection = new DocumentCollection { Id = collectionName }; + Client + .CreateDocumentCollectionAsync(Database.SelfLink, collection) + .ContinueWith(r => collection = r.Result.Resource) + .Wait(); + } + + return collection; + } + +#pragma warning disable 1591 // Xml Comments + public void Initialize(IContainer container) + { + Client = new DocumentClient(new Uri(_configuration.Url), _configuration.AuthorizationKey); + + Client.ReadDatabaseFeedAsync() + .ContinueWith(a => Database = a.Result.Where(d => d.Id == _configuration.DatabaseId).SingleOrDefault()) + .Wait(); + + if (Database == null) + { + this.Database = new Database { Id = _configuration.DatabaseId }; + Client.CreateDatabaseAsync(Database) + .ContinueWith(d=>Database = d.Result.Resource) + .Wait(); + } + } +#pragma warning restore 1591 // Xml Comments + } +} diff --git a/Source/Bifrost.DocumentDB/Events/ConfigurationExtensions.cs b/Source/Bifrost.DocumentDB/Events/ConfigurationExtensions.cs new file mode 100644 index 000000000..89d1f7523 --- /dev/null +++ b/Source/Bifrost.DocumentDB/Events/ConfigurationExtensions.cs @@ -0,0 +1,29 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using Bifrost.Configuration; + +namespace Bifrost.Configuration +{ + /// + /// Extensions for configuration + /// + public static partial class ConfigurationExtensions + { + } +} diff --git a/Source/Bifrost.DocumentDB/Properties/AssemblyInfo.cs b/Source/Bifrost.DocumentDB/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..635b3b3eb --- /dev/null +++ b/Source/Bifrost.DocumentDB/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Bifrost.DocumentDB")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Bifrost.DocumentDB")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("b6c215d8-8d19-4248-844a-498b5a86c93e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Source/Bifrost.DocumentDB/Read/QueryProvider.cs b/Source/Bifrost.DocumentDB/Read/QueryProvider.cs new file mode 100644 index 000000000..bb4eb98ee --- /dev/null +++ b/Source/Bifrost.DocumentDB/Read/QueryProvider.cs @@ -0,0 +1,69 @@ +#region License +// +// Copyright (c) 2008-2014, Dolittle (http://www.dolittle.com) +// +// Licensed under the MIT License (http://opensource.org/licenses/MIT) +// +// You may not use this file except in compliance with the License. +// You may obtain a copy of the license at +// +// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +using System; +using System.Linq; +using Bifrost.DocumentDB.Entities; +using Bifrost.Read; +using Microsoft.Azure.Documents.Linq; + +namespace Bifrost.DocumentDB.Read +{ + /// + /// Represents an implementation of + /// + public class QueryProvider : IQueryProviderFor + { + EntityContextConnection _connection; + + /// + /// Initializes a new instance of + /// + /// to use for getting to the server + public QueryProvider(EntityContextConnection connection) + { + _connection = connection; + } + + +#pragma warning disable 1591 // Xml Comments + public QueryProviderResult Execute(IDocumentQuery query, PagingInfo paging) + { + var queryable = query as IQueryable; + var result = new QueryProviderResult(); + + var collection = _connection.GetCollectionFor(queryable.ElementType); + _connection.Client.ReadDocumentFeedAsync(collection.DocumentsLink) + .ContinueWith(r => result.TotalItems) + .Wait(); + + /* + * Todo: As of 12th of October - this is not supported by the DocumentDB Linq Provider or DocumentDB itself! + if (paging.Enabled) + { + var start = paging.Size * paging.Number; + queryable = queryable.Skip(start).Take(paging.Size); + }*/ + + result.Items = queryable; + + return result; + } +#pragma warning restore 1591 // Xml Comments + } +} diff --git a/Source/Bifrost.DocumentDB/packages.config b/Source/Bifrost.DocumentDB/packages.config new file mode 100644 index 000000000..dbd41dabf --- /dev/null +++ b/Source/Bifrost.DocumentDB/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Source/Solutions/Bifrost_All.sln b/Source/Solutions/Bifrost_All.sln index c9391c2aa..04c61c3e0 100644 --- a/Source/Solutions/Bifrost_All.sln +++ b/Source/Solutions/Bifrost_All.sln @@ -99,6 +99,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bifrost.Default", "..\Bifro EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bifrost.Autofac", "..\Bifrost.Autofac\Bifrost.Autofac.csproj", "{26ECC23C-4139-4167-9DED-00002E5D9570}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bifrost.DocumentDB", "..\Bifrost.DocumentDB\Bifrost.DocumentDB.csproj", "{8DB73E9B-8AE1-4341-8DF8-469C578A150D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -640,6 +642,20 @@ Global {26ECC23C-4139-4167-9DED-00002E5D9570}.Release|Mixed Platforms.Build.0 = Release|Any CPU {26ECC23C-4139-4167-9DED-00002E5D9570}.Release|x64.ActiveCfg = Release|Any CPU {26ECC23C-4139-4167-9DED-00002E5D9570}.Release|x86.ActiveCfg = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|ARM.ActiveCfg = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|x64.ActiveCfg = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Debug|x86.ActiveCfg = Debug|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|Any CPU.Build.0 = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|ARM.ActiveCfg = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|x64.ActiveCfg = Release|Any CPU + {8DB73E9B-8AE1-4341-8DF8-469C578A150D}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -683,6 +699,7 @@ Global {9E87D7C6-65C2-4FAD-81FF-FDC7F0400384} = {ADBBEFA5-7DC6-43B7-8DE3-D66F529AAE1C} {D8A3E244-AC49-4372-8C03-F5AA3107BD80} = {ADBBEFA5-7DC6-43B7-8DE3-D66F529AAE1C} {26ECC23C-4139-4167-9DED-00002E5D9570} = {2B61FCED-42AC-48B3-B2E9-6F0EE50E0674} + {8DB73E9B-8AE1-4341-8DF8-469C578A150D} = {2B61FCED-42AC-48B3-B2E9-6F0EE50E0674} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35;packages\Unity.2.1.505.2\lib\NET35