Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Completed API change to support Locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Muc committed Aug 26, 2009
1 parent c85ad7d commit af02288
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 70 deletions.
30 changes: 26 additions & 4 deletions src/Secretary.Samples/SampleSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class SampleSetup
{
private void Sample_ApplicationStartup()
{
var webMusicSchool = new HttpSchool("webMusicSchool", "localhost", "/dawn");
webMusicSchool.Specializations.Add<Artist>(FileType.Audio, a => a.Id.ToString());

var musicSchool = new LocalSchool("MusicSchool", @"C:\test\music");

musicSchool.Specializations.Add<Artist>(FileType.Audio, a => a.Id.ToString());
Expand All @@ -24,6 +27,8 @@ private void Sample_ApplicationStartup()
imageSchool.Enroll().SpecializingIn(FileType.Image).For<Artist>();
imageSchool.Enroll().SpecializingIn(FileType.Image).For<User>();

webMusicSchool.Enroll().SpecializingIn(FileType.Audio).For<Artist>();

var ceremony = new GraduationCeremony(musicSchool.Enrollments);
var grads = new List<Secretary>();

Expand All @@ -33,6 +38,10 @@ private void Sample_ApplicationStartup()

grads.AddRange(ceremony.GetGraduates());

ceremony = new GraduationCeremony(webMusicSchool.Enrollments);

grads.AddRange(ceremony.GetGraduates());

Locate.InitializeWith(grads);
}

Expand All @@ -45,22 +54,35 @@ public void Setup()
var testArtist = new Artist() {Id = 1};
var testUser = new User() {Id = 1};

var test1 = Locate.FileOfType(FileType.Image).Named("Test.jpg").For(testArtist);
var test1 = Locate.File("Test.jpg")
.Of(FileType.Image)
.In(Location.Local)
.For(testArtist);

Assert.Equal(@"C:\test\images\1\Test.jpg", test1);

var test2 = Locate.FileOfType(FileType.Image).Named("Test.jpg").For(testUser);
var test2 = Locate.File("Test.jpg")
.Of(FileType.Image)
.In(Location.Local)
.For(testUser);

// notice how this is the same as the artist image
Assert.Equal(@"C:\test\images\1\Test.jpg", test2);

var test3 = Locate.FileOfType(FileType.Audio).Named("Test.mp3").For(testArtist);
var test3 = Locate.File("Test.mp3")
.Of(FileType.Audio)
.In(Location.Local)
.For(testArtist);

Assert.Equal(@"C:\test\music\1\Test.mp3", test3);

var test4 = Locate.FolderForType(FileType.Audio).For(testArtist);
var test4 = Locate.FolderForType(FileType.Audio).In(Location.Local).For(testArtist);

Assert.Equal(@"C:\test\music\1", test4);

var test5 = Locate.File("test.mp3").Of(FileType.Audio).In(Location.Web).For(testArtist);

Assert.Equal("http://localhost/dawn/1/test.mp3", test5);
}
}
}
2 changes: 1 addition & 1 deletion src/Secretary.UnitTests/FileLocationQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class FileLocationQueryTests
[Fact]
public void For_GivenNoSpecializedSecretaries_ShouldThrowNullReferenceException()
{
var sut = new FileLocationQuery(FileType.File) {Secretaries = new List<Secretary>()};
var sut = new FileLocationQuery("test.jpg") {Secretaries = new List<Secretary>()};

Assert.Throws<NullReferenceException>(
() => sut.For(new TestEntity() { Id = 1 })
Expand Down
28 changes: 18 additions & 10 deletions src/Secretary/FileLocationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@

namespace Secretary
{
public class FileLocationQuery : IFileLocationQuery, INamed
public class FileLocationQuery : IFileLocationQuery
{
public FileType FileType { get; private set; }
public string FileName { get; set; }
public Location LocationContext { get; private set; }
public string FileName { get; private set; }
public IList<Secretary> Secretaries { get; set;}

public FileLocationQuery(FileType fileType)
public FileLocationQuery(string fileName)
{
this.FileType = fileType;
FileName = fileName;
}

public INamed Named(string fileName)
public FileLocationQuery Of(FileType fileType)
{
FileName = fileName;
FileType = fileType;
return this;
}

public FileLocationQuery In(Location location)
{
LocationContext = location;
return this;
}

public string For<TEntity>(TEntity entity)
{
var secretary = Secretaries.Where(s => (s as Secretary<TEntity>) != null)
.Where(s => s.FileTypeHandled == FileType).SingleOrDefault();
var secretary = Secretaries
.Where(s => (s as Secretary<TEntity>) != null)
.Where(s => s.FileTypeHandled == FileType)
.Where(s => s.LocationContext == LocationContext)
.SingleOrDefault();

if (secretary == null)
throw new NullReferenceException("No secretary trained to handle that request");

var casted = (Secretary<TEntity>) secretary;
casted.Entity = entity;

return secretary.Locate(FileName).AbsoluteFilePath;

}
}
}
14 changes: 12 additions & 2 deletions src/Secretary/FolderLocationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,27 @@ namespace Secretary
public class FolderLocationQuery : IFolderLocationQuery
{
public FileType FileType { get; private set; }
public Location LocationContext { get; private set; }
public IList<Secretary> Secretaries { get; set; }

public FolderLocationQuery(FileType fileType)
{
this.FileType = fileType;
}

public FolderLocationQuery In(Location location)
{
this.LocationContext = location;
return this;
}

public string For<TEntity>(TEntity entity)
{
var secretary = Secretaries.Where(s => (s as Secretary<TEntity>) != null)
.Where(s => s.FileTypeHandled == FileType).SingleOrDefault();
var secretary = Secretaries
.Where(s => (s as Secretary<TEntity>) != null)
.Where(s => s.FileTypeHandled == FileType)
.Where(s => s.LocationContext == LocationContext)
.SingleOrDefault();

if (secretary == null)
throw new NullReferenceException("No secretary trained to handle that request");
Expand Down
2 changes: 1 addition & 1 deletion src/Secretary/HttpSchool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public HttpSchool(string name, Uri baseUri)

public override string BaseFilePath
{
get { return baseUri.AbsolutePath; }
get { return baseUri.AbsoluteUri; }
}

public override Location Location
Expand Down
24 changes: 24 additions & 0 deletions src/Secretary/LocalLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;

namespace Secretary
{
public class LocalLocation : Location
{

public override IFile CreateFileInstance(string basePath, string fileName)
{
var fullPath = Combine(basePath, fileName);
return new LocalFileReference(fullPath);
}

public override IFolder CreateFolderInstance(string basePath)
{
return new LocalFolderReference(basePath);
}

public override string Combine(string firstPart, string secondPart)
{
return Path.Combine(firstPart, secondPart);
}
}
}
8 changes: 4 additions & 4 deletions src/Secretary/Locate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ private static void GuardAgainstUninitializedUsage()
/// <summary>
/// Entry API to file reference retrieval
/// </summary>
/// <param name="fileType"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static IFileLocationQuery FileOfType(FileType fileType)
public static FileLocationQuery File(string fileName)
{
GuardAgainstUninitializedUsage();

return new FileLocationQuery(fileType)
return new FileLocationQuery(fileName)
{
Secretaries = Secretaries
};
Expand All @@ -54,7 +54,7 @@ public static IFileLocationQuery FileOfType(FileType fileType)
/// </summary>
/// <param name="fileType"></param>
/// <returns></returns>
public static IFolderLocationQuery FolderForType(FileType fileType)
public static FolderLocationQuery FolderForType(FileType fileType)
{
GuardAgainstUninitializedUsage();

Expand Down
44 changes: 6 additions & 38 deletions src/Secretary/Location.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
using System;
using System.IO;

namespace Secretary
{
public class Location
public abstract class Location
{
public static readonly Location Web = new Location("Web",
(basePath, fileName) =>
{
var fullPath = basePath + "/" + fileName;
return new HttpFileReference(fullPath);
},
basePath => new HttpFolderReference(basePath)
);

public static readonly Location Local = new Location("Local",
(basePath, fileName) =>
{
var fullPath = Path.Combine(basePath, fileName);
return new LocalFileReference(fullPath);
},
basePath => new LocalFolderReference(basePath)
);


public Location(string name, Func<string, string, IFile> fileFactoryMethod, Func<string, IFolder> folderFactoryMethod)
{
Name = name;
CreateFileInstance = fileFactoryMethod;
CreateFolderInstance = folderFactoryMethod;
}

public Func<string, string, IFile> CreateFileInstance;
public Func<string, IFolder> CreateFolderInstance;

public string Name { get; private set; }
public static readonly Location Web = new WebLocation();
public static readonly Location Local = new LocalLocation();

public override string ToString()
{
return "Location." + Name;
}
public abstract IFile CreateFileInstance(string basePath, string fileName);
public abstract IFolder CreateFolderInstance(string basePath);
public abstract string Combine(string firstPart, string secondPart);
}
}
5 changes: 1 addition & 4 deletions src/Secretary/Secretary.Generic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;

namespace Secretary
{
Expand All @@ -25,9 +24,7 @@ public override IFolder GetFolder()
protected string GetBasePath()
{
var entityPath = EntityPathBuilder.Invoke(Entity);

// TODO move this to LocationContext delegate
var basePath = Path.Combine(RootFolder, entityPath);
var basePath = LocationContext.Combine(RootFolder, entityPath);

return basePath;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Secretary/Secretary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@
<Compile Include="HttpFileReference.cs" />
<Compile Include="HttpFolderReference.cs" />
<Compile Include="HttpSchool.cs" />
<Compile Include="LocalLocation.cs" />
<Compile Include="LocalSchool.cs" />
<Compile Include="Location.cs" />
<Compile Include="Secretary.Generic.cs" />
<Compile Include="WebLocation.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
7 changes: 1 addition & 6 deletions src/Secretary/Syntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ public interface ISpecializedIn : IFluentInterface
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IFileLocationQuery : IFluentInterface
{
INamed Named(string fileName);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public interface INamed : IFluentInterface
{
string For<TEntity>(TEntity entity);
}


[EditorBrowsable(EditorBrowsableState.Never)]
public interface IFolderLocationQuery : IFluentInterface
{
Expand Down
21 changes: 21 additions & 0 deletions src/Secretary/WebLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Secretary
{
public class WebLocation : Location
{
public override IFile CreateFileInstance(string basePath, string fileName)
{
var fullPath = Combine(basePath, fileName);
return new HttpFileReference(fullPath);
}

public override IFolder CreateFolderInstance(string basePath)
{
return new HttpFolderReference(basePath);
}

public override string Combine(string firstPart, string secondPart)
{
return firstPart.TrimEnd('/') + "/" + secondPart.TrimStart('/');
}
}
}

0 comments on commit af02288

Please sign in to comment.