/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Text;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using OpenIddict.Abstractions;
using OpenIddict.MongoDb.Models;
namespace OpenIddict.MongoDb
{
///
/// Exposes a method allowing to resolve an application store.
///
public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver
{
private readonly IServiceProvider _provider;
public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider)
{
_provider = provider;
}
///
/// Returns an application store compatible with the specified application type or throws an
/// if no store can be built using the specified type.
///
/// The type of the Application entity.
/// An .
public IOpenIddictApplicationStore Get() where TApplication : class
{
var store = _provider.GetService>();
if (store != null)
{
return store;
}
if (!typeof(OpenIddictApplication).IsAssignableFrom(typeof(TApplication)))
{
throw new InvalidOperationException(new StringBuilder()
.AppendLine("The specified application type is not compatible with the MongoDB stores.")
.Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictApplication' ")
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ")
.Append("that inherits from the 'OpenIddictApplication' entity.")
.ToString());
}
return (IOpenIddictApplicationStore) _provider.GetRequiredService(
typeof(OpenIddictApplicationStore<>).MakeGenericType(typeof(TApplication)));
}
}
}