mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-24 05:42:10 +08:00
Refactored the way elements are instantiated.
Elements are now resolved using work context scope.
This commit is contained in:
@@ -97,10 +97,18 @@ namespace Orchard.Tests.DisplayManagement {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
object value;
|
||||
return _state.TryGetValue(name, out value) ? (T)value : default(T);
|
||||
|
||||
@@ -42,10 +42,18 @@ namespace Orchard.Tests.Localization {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
if (name == "CurrentCulture") return (T)((object)CultureName);
|
||||
if (name == "CurrentCalendar") return (T)((object)CalendarName);
|
||||
|
||||
@@ -123,10 +123,18 @@ namespace Orchard.Tests.Stubs {
|
||||
return _lifetimeScope.Resolve<T>();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
return _lifetimeScope.Resolve(serviceType);
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
return _lifetimeScope.TryResolve<T>(out service);
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
return _lifetimeScope.TryResolve(serviceType, out service);
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
return (T) _contextDictonary[name];
|
||||
}
|
||||
|
||||
@@ -69,10 +69,18 @@ namespace Orchard.Tests.Time {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
return default(T);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -131,10 +131,18 @@ namespace Orchard.Tokens.Tests {
|
||||
return _lifetimeScope.Resolve<T>();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
return _lifetimeScope.Resolve(serviceType);
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
return _lifetimeScope.TryResolve<T>(out service);
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
return _lifetimeScope.TryResolve(serviceType, out service);
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
return (T)_contextDictonary[name];
|
||||
}
|
||||
|
||||
@@ -79,7 +79,6 @@ namespace Orchard.Environment {
|
||||
RegisterVolatileProvider<DefaultVirtualPathMonitor, IVirtualPathMonitor>(builder);
|
||||
RegisterVolatileProvider<DefaultVirtualPathProvider, IVirtualPathProvider>(builder);
|
||||
|
||||
|
||||
builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>()
|
||||
.Named<IEventHandler>(typeof(IShellSettingsManagerEventHandler).Name)
|
||||
.Named<IEventHandler>(typeof(IShellDescriptorManagerEventHandler).Name)
|
||||
|
||||
@@ -19,10 +19,18 @@ namespace Orchard.Environment {
|
||||
return _componentContext.Resolve<T>();
|
||||
}
|
||||
|
||||
public override object Resolve(Type serviceType) {
|
||||
return _componentContext.Resolve(serviceType);
|
||||
}
|
||||
|
||||
public override bool TryResolve<T>(out T service) {
|
||||
return _componentContext.TryResolve(out service);
|
||||
}
|
||||
|
||||
public override bool TryResolve(Type serviceType, out object service) {
|
||||
return _componentContext.TryResolve(serviceType, out service);
|
||||
}
|
||||
|
||||
public override T GetState<T>(string name) {
|
||||
var resolver = _stateResolvers.GetOrAdd(name, FindResolverForState<T>);
|
||||
return (T)resolver();
|
||||
|
||||
@@ -10,20 +10,35 @@ namespace Orchard {
|
||||
/// </summary>
|
||||
public abstract class WorkContext {
|
||||
/// <summary>
|
||||
/// Resolves a registered dependency type
|
||||
/// Resolves a registered dependency type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the dependency</typeparam>
|
||||
/// <returns>An instance of the dependency if it could be resolved</returns>
|
||||
/// <typeparam name="T">The type of the dependency.</typeparam>
|
||||
/// <returns>An instance of the dependency if it could be resolved.</returns>
|
||||
public abstract T Resolve<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Tries to resolve a registered dependency type
|
||||
/// Resolves a registered dependency type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the dependency</typeparam>
|
||||
/// <param name="service">An instance of the dependency if it could be resolved</param>
|
||||
/// <returns>True if the dependency could be resolved, false otherwise</returns>
|
||||
/// <param name="serviceType">The type of the dependency.</param>
|
||||
/// <returns>An instance of the dependency if it could be resolved.</returns>
|
||||
public abstract object Resolve(Type serviceType);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to resolve a registered dependency type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the dependency.</typeparam>
|
||||
/// <param name="service">An instance of the dependency if it could be resolved.</param>
|
||||
/// <returns>True if the dependency could be resolved, false otherwise.</returns>
|
||||
public abstract bool TryResolve<T>(out T service);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to resolve a registered dependency type.
|
||||
/// </summary>
|
||||
/// <param name="serviceType">The type of the dependency.</param>
|
||||
/// <param name="service">An instance of the dependency if it could be resolved.</param>
|
||||
/// <returns>True if the dependency could be resolved, false otherwise.</returns>
|
||||
public abstract bool TryResolve(Type serviceType, out object service);
|
||||
|
||||
public abstract T GetState<T>(string name);
|
||||
public abstract void SetState<T>(string name, T value);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user