mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-27 04:19:04 +08:00
Refactored the way elements are instantiated.
Elements are now resolved using work context scope.
This commit is contained in:
@@ -3,7 +3,7 @@ using Orchard.Localization;
|
||||
using Orchard.Utility.Extensions;
|
||||
|
||||
namespace Orchard.Layouts.Framework.Elements {
|
||||
public abstract class Element {
|
||||
public abstract class Element : IElement {
|
||||
protected Element() {
|
||||
T = NullLocalizer.Instance;
|
||||
Data = new ElementDataDictionary();
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Orchard.Layouts.Framework.Elements {
|
||||
public interface IElement : ITransientDependency {}
|
||||
}
|
||||
@@ -358,6 +358,7 @@
|
||||
<Compile Include="Framework\Display\ElementCreatingDisplayShapeContext.cs" />
|
||||
<Compile Include="Framework\Drivers\ImportElementContext.cs" />
|
||||
<Compile Include="Framework\Drivers\ImportLayoutContext.cs" />
|
||||
<Compile Include="Framework\Elements\IElement.cs" />
|
||||
<Compile Include="Handlers\ElementDriversCoordinator.cs" />
|
||||
<Compile Include="Handlers\ElementRuleCoordinator.cs" />
|
||||
<Compile Include="Helpers\DictionaryExtensions.cs" />
|
||||
|
||||
@@ -5,16 +5,19 @@ using Orchard.Localization;
|
||||
namespace Orchard.Layouts.Services {
|
||||
public class ElementFactory : IElementFactory {
|
||||
private readonly IElementEventHandler _elementEventHandler;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
|
||||
public ElementFactory(IElementEventHandler elementEventHandler) {
|
||||
public ElementFactory(IElementEventHandler elementEventHandler, IWorkContextAccessor workContextAccessor) {
|
||||
_elementEventHandler = elementEventHandler;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public Element Activate(Type elementType, Action<Element> initialize = null) {
|
||||
var element = (Element)Activator.CreateInstance(elementType);
|
||||
var workContext = _workContextAccessor.GetContext();
|
||||
var element = (Element)workContext.Resolve(elementType);
|
||||
|
||||
if (initialize != null)
|
||||
initialize(element);
|
||||
@@ -23,7 +26,8 @@ namespace Orchard.Layouts.Services {
|
||||
}
|
||||
|
||||
public T Activate<T>(Action<T> initialize = null) where T : Element {
|
||||
var element = (T)Activator.CreateInstance(typeof(T));
|
||||
var workContext = _workContextAccessor.GetContext();
|
||||
var element = workContext.Resolve<T>();
|
||||
|
||||
if (initialize != null)
|
||||
initialize(element);
|
||||
|
||||
Reference in New Issue
Block a user