This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #3752] Refactor and cleanup view components
* Moved instantiation of view components into DefaultViewComponentActivator * Introduced IViewComponentFactory to handling setup of new view component instances. * Added a release method on IViewComponentActivator for handling tear down of view component instances.
- Loading branch information
Showing
12 changed files
with
416 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNetCore.Mvc.ViewFeatures/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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.Concurrent; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewComponents | ||
{ | ||
/// <summary> | ||
/// Default implementation for <see cref="IViewComponentFactory"/>. | ||
/// </summary> | ||
public class DefaultViewComponentFactory : IViewComponentFactory | ||
{ | ||
private readonly IViewComponentActivator _activator; | ||
private readonly Func<Type, PropertyActivator<ViewComponentContext>[]> _getPropertiesToActivate; | ||
private readonly ConcurrentDictionary<Type, PropertyActivator<ViewComponentContext>[]> _injectActions; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="DefaultViewComponentFactory"/> | ||
/// </summary> | ||
/// <param name="activator"> | ||
/// The <see cref="IViewComponentActivator"/> used to create new view component instances. | ||
/// </param> | ||
public DefaultViewComponentFactory(IViewComponentActivator activator) | ||
{ | ||
if (activator == null) | ||
{ | ||
throw new ArgumentNullException(nameof(activator)); | ||
} | ||
|
||
_activator = activator; | ||
|
||
_getPropertiesToActivate = type => PropertyActivator<ViewComponentContext>.GetPropertiesToActivate( | ||
type, | ||
typeof(ViewComponentContextAttribute), | ||
CreateActivateInfo); | ||
|
||
_injectActions = new ConcurrentDictionary<Type, PropertyActivator<ViewComponentContext>[]>(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public object CreateViewComponent(ViewComponentContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var component = _activator.Create(context); | ||
|
||
InjectProperties(context, component); | ||
|
||
return component; | ||
} | ||
|
||
private void InjectProperties(ViewComponentContext context, object viewComponent) | ||
{ | ||
var propertiesToActivate = _injectActions.GetOrAdd( | ||
viewComponent.GetType(), | ||
type => | ||
PropertyActivator<ViewComponentContext>.GetPropertiesToActivate( | ||
type, | ||
typeof(ViewComponentContextAttribute), | ||
CreateActivateInfo)); | ||
|
||
for (var i = 0; i < propertiesToActivate.Length; i++) | ||
{ | ||
var activateInfo = propertiesToActivate[i]; | ||
activateInfo.Activate(viewComponent, context); | ||
} | ||
} | ||
|
||
private static PropertyActivator<ViewComponentContext> CreateActivateInfo(PropertyInfo property) | ||
{ | ||
return new PropertyActivator<ViewComponentContext>(property, context => context); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void ReleaseViewComponent(ViewComponentContext context, object component) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (component == null) | ||
{ | ||
throw new ArgumentNullException(nameof(component)); | ||
} | ||
|
||
_activator.Release(context, component); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.