Created
February 14, 2011 17:38
-
-
Save emiaj/826224 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
using System.Linq; | |
using ZG.Core.Entities; | |
using ZG.Core.Services; | |
using ZG.Mvc.Engines; | |
namespace ZG.Mvc.Theming.Model | |
{ | |
public interface IThemeComposition | |
{ | |
ITheme Compose(); | |
} | |
public interface ICompositionContext | |
{ | |
IOutputScope OutputScope { get; } | |
// Page Page{get;} ??? | |
} | |
public interface IThemeComposer<out TOut, in TCommand> | |
{ | |
TOut Compose(TCommand command, ICompositionContext context); | |
} | |
public class SkinCommand | |
{ | |
public string Name { get; set; } | |
} | |
public class ZoneCommand | |
{ | |
public string Name { get; set; } | |
} | |
public class LayoutCommand | |
{ | |
public string Name { get; set; } | |
} | |
public class ContainerCommand | |
{ | |
public string Name { get; set; } | |
public string Zone { get; set; } | |
} | |
public class EngineCommand | |
{ | |
public IEngineInvocation Invocation { get; set; } | |
} | |
public class LayoutComposer : IThemeComposer<Layout, LayoutCommand> | |
{ | |
public Layout Compose(LayoutCommand command, ICompositionContext context) | |
{ | |
var layout = new Layout(command.Name, () => context.OutputScope.Create(command.Name)); | |
return layout; | |
} | |
} | |
public class ZoneComposer:IThemeComposer<Zone,ZoneCommand> | |
{ | |
public Zone Compose(ZoneCommand command, ICompositionContext context) | |
{ | |
var zone = new Zone(command.Name, () => context.OutputScope.Create(command.Name)); | |
return zone; | |
} | |
} | |
public class ContainerComposer : IThemeComposer<Container, ContainerCommand> | |
{ | |
public Container Compose(ContainerCommand command, ICompositionContext context) | |
{ | |
// [context.OutputScope.Create(command.Zone + "_content")] => not real code of course | |
var container = new Container(command.Name, command.Zone, () => context.OutputScope.Create(command.Zone + "_content")); | |
return container; | |
} | |
} | |
public class EngineComposer : IThemeComposer<Engine, EngineCommand> | |
{ | |
public Engine Compose(EngineCommand command, ICompositionContext context) | |
{ | |
var slice = command.Invocation.Metadata<Slice>(); | |
var engine = new Engine(command.Invocation.Invoke, () => context.OutputScope.Create(slice.RenderInfo.Container)); | |
// SET CUSTOM IOutputWriter here? | |
return engine; | |
} | |
} | |
public class ThemeComposition:IThemeComposition | |
{ | |
private readonly Page _page; | |
private readonly ISkinService _skinService; | |
private readonly IThemeComposer<Layout, LayoutCommand> _layoutComposer; | |
private readonly IThemeComposer<Zone, ZoneCommand> _zoneComposer; | |
private readonly IThemeComposer<Container, ContainerCommand> _containerComposer; | |
private readonly IThemeComposer<Engine, EngineCommand> _engineComposer; | |
private readonly ICompositionContext _context; | |
private readonly IEnumerable<IEngineInvocation> _invocations; | |
public ThemeComposition(Page page,ISkinService skinService, | |
IThemeComposer<Layout,LayoutCommand> layoutComposer, | |
IThemeComposer<Zone,ZoneCommand> zoneComposer, | |
IThemeComposer<Container,ContainerCommand> containerComposer, | |
IThemeComposer<Engine,EngineCommand> engineComposer, | |
ICompositionContext context, | |
IEnumerable<IEngineInvocation> invocations | |
) | |
{ | |
_page = page; | |
_skinService = skinService; | |
_layoutComposer = layoutComposer; | |
_zoneComposer = zoneComposer; | |
_containerComposer = containerComposer; | |
_engineComposer = engineComposer; | |
_context = context; | |
_invocations = invocations; | |
} | |
public ITheme Compose() | |
{ | |
var layoutZones = _skinService.GetSkin(_page.Skin).Layouts.First(x => x.Name == _page.Layout).Zones; | |
var themeLayout = _layoutComposer.Compose(new LayoutCommand {Name = _page.Layout}, _context); | |
var themeZones = layoutZones | |
.Select(x => _zoneComposer.Compose(new ZoneCommand {Name = x.Name}, _context)); | |
var themeContainers = _page.Slices.Select(x => x.RenderInfo) | |
.Select(x => _containerComposer.Compose(new ContainerCommand { Name = x.Container, Zone = x.Zone }, _context)); | |
var engines = _invocations.Select(x => _engineComposer.Compose(new EngineCommand {Invocation = x}, _context)); | |
var theme = new Theme(_page.Skin, themeLayout, themeZones, themeContainers, engines); | |
return theme; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment