// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc
{
///
/// Extension methods for adding view engines to a descriptor collection.
///
public static class ViewEngineDescriptorExtensions
{
///
/// Adds a type representing a to a descriptor collection.
///
/// A list of ViewEngineDescriptors
/// Type representing an .
/// ViewEngineDescriptor representing the added instance.
public static ViewEngineDescriptor Add(
[NotNull] this IList descriptors,
[NotNull] Type viewEngineType)
{
var descriptor = new ViewEngineDescriptor(viewEngineType);
descriptors.Add(descriptor);
return descriptor;
}
///
/// Inserts a type representing a to a descriptor collection.
///
/// A list of ViewEngineDescriptors
/// Type representing an .
/// ViewEngineDescriptor representing the inserted instance.
public static ViewEngineDescriptor Insert(
[NotNull] this IList descriptors,
int index,
[NotNull] Type viewEngineType)
{
if (index < 0 || index > descriptors.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
var descriptor = new ViewEngineDescriptor(viewEngineType);
descriptors.Insert(index, descriptor);
return descriptor;
}
///
/// Adds an to a descriptor collection.
///
/// A list of ViewEngineDescriptors
/// An instance.
/// ViewEngineDescriptor representing the added instance.
public static ViewEngineDescriptor Add(
[NotNull] this IList descriptors,
[NotNull] IViewEngine viewEngine)
{
var descriptor = new ViewEngineDescriptor(viewEngine);
descriptors.Add(descriptor);
return descriptor;
}
///
/// Insert an to a descriptor collection.
///
/// A list of ViewEngineDescriptors
/// An instance.
/// ViewEngineDescriptor representing the added instance.
public static ViewEngineDescriptor Insert(
[NotNull] this IList descriptors,
int index,
[NotNull] IViewEngine viewEngine)
{
if (index < 0 || index > descriptors.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
var descriptor = new ViewEngineDescriptor(viewEngine);
descriptors.Insert(index, descriptor);
return descriptor;
}
///
/// Returns the only instance of from .
///
/// The type of the instance to find.
/// The to search.
/// The only instance of in .
///
/// Thrown if there is not exactly one in .
///
public static TInstance InstanceOf(
[NotNull] this IList descriptors)
{
return descriptors
.Select(descriptor => descriptor.ViewEngine)
.OfType()
.Single();
}
///
/// Returns the only instance of from
/// or null if the sequence is empty.
///
/// The type of the instance to find.
/// The to search.
///
/// Thrown if there is more than one in .
///
public static TInstance InstanceOfOrDefault(
[NotNull] this IList descriptors)
{
return descriptors
.Select(descriptor => descriptor.ViewEngine)
.OfType()
.SingleOrDefault();
}
///
/// Returns all instances of from .
///
/// The type of the instances to find.
/// The to search.
/// An IEnumerable of that contains instances from
/// .
public static IEnumerable InstancesOf(
[NotNull] this IList descriptors)
{
return descriptors
.Select(descriptor => descriptor.ViewEngine)
.OfType();
}
}
}