Refactored the way elements are instantiated.

Elements are now resolved using work context scope.
This commit is contained in:
Sipke Schoorstra
2015-09-07 18:22:56 +01:00
parent a760f012f2
commit 4ad806af8e
12 changed files with 82 additions and 12 deletions

View File

@@ -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();

View File

@@ -0,0 +1,3 @@
namespace Orchard.Layouts.Framework.Elements {
public interface IElement : ITransientDependency {}
}

View File

@@ -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" />

View File

@@ -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);