Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

42 lines
1.4 KiB
C#

namespace System.Web.Mvc {
using System;
using System.Collections.Generic;
using System.Linq;
internal class MultiServiceResolver<TService> : IResolver<IEnumerable<TService>> where TService : class {
private IEnumerable<TService> _itemsFromService;
private Func<IEnumerable<TService>> _itemsThunk;
private Func<IDependencyResolver> _resolverThunk;
public MultiServiceResolver(Func<IEnumerable<TService>> itemsThunk) {
if (itemsThunk == null) {
throw new ArgumentNullException("itemsThunk");
}
_itemsThunk = itemsThunk;
_resolverThunk = () => DependencyResolver.Current;
}
internal MultiServiceResolver(Func<IEnumerable<TService>> itemsThunk, IDependencyResolver resolver)
: this(itemsThunk) {
if (resolver != null) {
_resolverThunk = () => resolver;
}
}
public IEnumerable<TService> Current {
get {
if (_itemsFromService == null) {
lock (_itemsThunk) {
if (_itemsFromService == null) {
_itemsFromService = _resolverThunk().GetServices<TService>();
}
}
}
return _itemsFromService.Concat(_itemsThunk());
}
}
}
}