Skip to content

Commit

Permalink
Removed hard dependency to structuremap
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanforsberg committed Mar 18, 2013
1 parent eae0515 commit 1dc1be5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
33 changes: 18 additions & 15 deletions src/WhiteMagic.Tests/InMemoryContentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace WhiteMagic.Tests
{
public class InMemoryContentRepository : IContentRepository
{
private readonly IContainer _container;
private readonly Func<Type, IContentData> _activator;
private readonly IInmemoryPermantentLinkMapper _permanentLinkMapper;

private readonly Dictionary<PageReference, List<PageData>> _pages = new Dictionary<PageReference, List<PageData>>();
private readonly Dictionary<PageReference, List<PageReference>> _structure = new Dictionary<PageReference, List<PageReference>>();

private int _pageId = 3;

public InMemoryContentRepository(IContainer container, IInmemoryPermantentLinkMapper permanentLinkMapper)
public InMemoryContentRepository(Func<Type, IContentData> activator, IInmemoryPermantentLinkMapper permanentLinkMapper)
{
_container = container;
_activator = activator;
_permanentLinkMapper = permanentLinkMapper;
}

Expand Down Expand Up @@ -432,7 +432,8 @@ private TPageData ConstructContentData<TPageData>(string pageName, PageReference
private TPageData ConstructContentData<TPageData>(string pageName, PageReference pageLink, PageReference parentLink, String language)
where TPageData : IContentData
{
var page = _container.GetInstance<TPageData>();
var page = _activator.Invoke(typeof(TPageData));

page.Property.Add(new PropertyString() { Name = "PageName" });
page.Property.Add(new PropertyDate() { Name = "PageStartPublish" });
page.Property.Add(new PropertyDate() { Name = "PageStopPublish" });
Expand Down Expand Up @@ -461,7 +462,7 @@ private TPageData ConstructContentData<TPageData>(string pageName, PageReference
InitBlocks(page as PageData);
}

return page;
return (TPageData)page;
}

private void AddPageDataProperties(PageData page, string pageName, PageReference pageLink, PageReference parentLink)
Expand All @@ -478,19 +479,21 @@ private void AddPageDataProperties(PageData page, string pageName, PageReference

private void InitBlocks(object page)
{
foreach (var property in page.GetType().GetProperties())
{
if (property.PropertyType.IsSubclassOf(typeof(BlockData)))
{
var block = _container.GetInstance(property.PropertyType);
var blockProperties = page
.GetType().
GetProperties().
Where(p => p.PropertyType.IsSubclassOf(typeof (BlockData)));

if (property.CanWrite)
{
property.SetValue(page, block, null);
}
foreach (var property in blockProperties)
{
var block = _activator.Invoke(property.PropertyType);

InitBlocks(block);
if (property.CanWrite)
{
property.SetValue(page, block, null);
}

InitBlocks(block);
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/WhiteMagic.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
using System;
using EPiServer.Core;
using NUnit.Framework;
using StructureMap;

namespace WhiteMagic.Tests
Expand All @@ -10,13 +12,23 @@ public class TestBase
[SetUp]
public void Setup()
{
ContentRepository = new InMemoryContentRepository(ObjectFactory.Container, new InMemoryPermanentLinkMapper());
ContentRepository = new InMemoryContentRepository(StandardActivator, new InMemoryPermanentLinkMapper());
ContentRepository.CreateSystemPages();

Given();
When();
}

private static IContentData StandardActivator(Type type)
{
return Activator.CreateInstance(type) as IContentData;
}

private IContentData StructureMapActivator(Type type)
{
return ObjectFactory.Container.GetInstance(type) as IContentData;
}

public virtual void Given()
{

Expand Down

0 comments on commit 1dc1be5

Please sign in to comment.