Skip to content

Commit

Permalink
Fix some code style issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Jan 3, 2017
1 parent fbe309c commit 56efd44
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

#region CA1811 Review uncalled private code
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "Microsoft.Restier.Core.Submit.DataModificationItem.#ServerValues")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.ModelProducer.#InnerModelBuilder")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.QueryExecutor.#Inner")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "Microsoft.Restier.Publishers.OData.Query.RestierQueryExecutor.#Inner")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "Microsoft.Restier.Publishers.OData.ValidationResultDto.#Severity")]
Expand Down Expand Up @@ -172,6 +173,7 @@
[assembly: SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "source", Scope = "member", Target = "Microsoft.Restier.Core.DataSourceStub.#GetPropertyValue`1(System.Object,System.String)")]
[assembly: SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "propertyName", Scope = "member", Target = "Microsoft.Restier.Core.DataSourceStub.#GetPropertyValue`1(System.Object,System.String)")]
[assembly: SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "odataProperties", Scope = "member", Target = "Microsoft.Restier.Publishers.OData.RestierController`1.#GetQuery(System.Web.OData.Extensions.HttpRequestMessageProperties)")]
[assembly: SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.ModelProducer.#GetModelAsync(Microsoft.Restier.Core.Model.ModelContext,System.Threading.CancellationToken)")]
#endregion

#region CA2000 Dispose objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
#endif
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
Expand Down Expand Up @@ -62,27 +63,43 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
var dbContext = context.GetApiService<DbContext>();

var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace;
// @robertmclaws: The query below actually returns all registered Containers across all registered DbContexts.
// It is likely a bug in some other part of OData. But we can roll with it.

// @robertmclaws: The query below actually returns all registered Containers
// across all registered DbContexts.
// It is likely a bug in some other part of OData. But we can roll with it.
var efEntityContainers = efModel.GetItems<EntityContainer>(DataSpace.CSpace);
// @robertmclaws: Because of the bug above, we should not make any assumptions about what is returned, and get
// the specific container we want to use. Even if the bug gets fixed, the next line should still
// continue to work.

// @robertmclaws: Because of the bug above, we should not make any assumptions about what is returned,
// and get the specific container we want to use. Even if the bug gets fixed, the next line should still
// continue to work.
var efEntityContainer = efEntityContainers.FirstOrDefault(c => c.Name == dbContext.GetType().Name);
// @robertmclaws: Now that we're doing a proper FirstOrDefault() instead of a Single(), we wont' crash if more
// than one is returned, and we can check for null and inform the user specifically what happened.

// @robertmclaws: Now that we're doing a proper FirstOrDefault() instead of a Single(),
// we wont' crash if more than one is returned, and we can check for null
// and inform the user specifically what happened.
if (efEntityContainer == null)
{
if (efEntityContainers.Count > 1)
{
// @robertmclaws: In this case, we have multiple DbContexts available, but none of them match up.
// Tell the user what we have, and what we were expecting, so they can fix it.
var containerNames = efEntityContainers.Aggregate("", (current, next) => next.Name + ", ");
throw new Exception(string.Format(Resources.MultipleDbContextsExpectedException, containerNames.Substring(0, containerNames.Length - 2), efEntityContainer.Name));
var containerNames = efEntityContainers.Aggregate(
string.Empty, (current, next) => next.Name + ", ");
throw new Exception(string.Format(
CultureInfo.InvariantCulture,
Resources.MultipleDbContextsExpectedException,
containerNames.Substring(0, containerNames.Length - 2),
efEntityContainer.Name));
}

// @robertmclaws: In this case, we only had one DbContext available, and if wasn't thw right one.
throw new Exception(string.Format(Resources.DbContextCouldNotBeFoundException, dbContext.GetType().Name, efEntityContainer.Name));
throw new Exception(string.Format(
CultureInfo.InvariantCulture,
Resources.DbContextCouldNotBeFoundException,
dbContext.GetType().Name,
efEntityContainer.Name));
}

var itemCollection = (ObjectItemCollection)efModel.GetItemCollection(DataSpace.OSpace);

foreach (var efEntitySet in efEntityContainer.EntitySets)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<value>Need 'Point type', while input is {0}.</value>
</data>
<data name="MultipleDbContextsExpectedException" xml:space="preserve">
<value>This project has multiple EntityFrameworkApis using different DbContexts, and the correct context could not be loaded. \r\n The contexts available are '{0}' but the Container expects '{1}'.</value>
<value>This project has multiple EntityFrameworkApi using different DbContexts, and the correct context could not be loaded. \r\n The contexts available are '{0}' but the Container expects '{1}'.</value>
</data>
<data name="PreconditionCheckFailed" xml:space="preserve">
<value>The precondition check for request {0} on resource {1} is failed.</value>
Expand All @@ -142,4 +142,4 @@
<data name="UnsupportedPropertyType" xml:space="preserve">
<value>Unsupported type for property: {0}.</value>
</data>
</root>
</root>

0 comments on commit 56efd44

Please sign in to comment.