Working on providing support for some common aspects.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041960
This commit is contained in:
loudej
2009-11-23 22:32:43 +00:00
parent 7c9dbd081d
commit 37b270dcab
14 changed files with 198 additions and 137 deletions

View File

@@ -1,7 +1,7 @@
using Orchard.Models; using Orchard.Models;
namespace Orchard.Core.Common.Models { namespace Orchard.Core.Common.Models {
public class BodyPart : Orchard.Models.ContentPart { public class BodyAspect : ContentPart {
public string Body { get; set; } public string Body { get; set; }
public string Format { get; set; } public string Format { get; set; }
} }

View File

@@ -1,7 +0,0 @@
using Orchard.Models.Driver;
namespace Orchard.Core.Common.Models {
public class BodyPartProvider : ContentProvider {
}
}

View File

@@ -0,0 +1,37 @@
using System;
using Orchard.Core.Common.Records;
using Orchard.Core.Common.Utilities;
using Orchard.Models;
using Orchard.Security;
namespace Orchard.Core.Common.Models {
public class CommonAspect : ContentPart<CommonRecord> {
private readonly LazyField<IUser> _owner = new LazyField<IUser>();
private readonly LazyField<IContent> _container = new LazyField<IContent>();
public IUser Owner {
get { return _owner.Value; }
set {_owner.Value = value;}
}
public IContent Container {
get { return _container.Value; }
set {_container.Value = value;}
}
internal void OnGetOwner(Func<IUser> loader) {
_owner.Loader(loader);
}
internal void OnSetOwner(Func<IUser,IUser> setter) {
_owner.Setter(setter);
}
internal void OnGetContainer(Func<IContent> loader) {
_container.Loader(loader);
}
internal void OnSetContainer(Func<IContent, IContent> setter) {
_container.Setter(setter);
}
}
}

View File

@@ -1,51 +0,0 @@
using System;
using Orchard.Core.Common.Records;
using Orchard.Models;
using Orchard.Security;
namespace Orchard.Core.Common.Models {
public class CommonPart : ContentPart<CommonRecord> {
private readonly Lazy<IUser> _owner = new Lazy<IUser>();
private readonly Lazy<IContent> _container = new Lazy<IContent>();
public IUser Owner {
get { return _owner.Value; }
set { _owner.Value = value; }
}
public IContent Container {
get { return _container.Value; }
set { _container.Value = value; }
}
public void LoadOwner(Func<IUser> loader) {
_owner.Loader(loader);
}
public void LoadContainer(Func<IContent> loader) {
_container.Loader(loader);
}
}
public class Lazy<T> {
private Func<T> _loader;
private T _value;
public void Loader(Func<T> loader) {
_loader = loader;
}
public T Value {
get {
if (_loader != null) {
_value = _loader();
_loader = null;
}
return _value;
}
set {
_value = value;
_loader = null;
}
}
}
}

View File

@@ -1,56 +0,0 @@
using Orchard.Core.Common.Records;
using Orchard.Data;
using Orchard.Models;
using Orchard.Models.Driver;
using Orchard.Security;
using Orchard.Services;
namespace Orchard.Core.Common.Models {
public class CommonPartProvider : ContentProvider {
private readonly IClock _clock;
private readonly IAuthenticationService _authenticationService;
private readonly IContentManager _contentManager;
public CommonPartProvider(
IRepository<CommonRecord> repository,
IClock clock,
IAuthenticationService authenticationService,
IContentManager contentManager) {
_clock = clock;
_authenticationService = authenticationService;
_contentManager = contentManager;
AddOnCreating<CommonPart>(SetCreateTimesAndAuthor);
Filters.Add(new StorageFilter<CommonRecord>(repository));
AddOnLoaded<CommonPart>(LoadOwnerModel);
}
void SetCreateTimesAndAuthor(CreateContentContext context, CommonPart instance) {
if (instance.Record.CreatedUtc == null) {
instance.Record.CreatedUtc = _clock.UtcNow;
}
if (instance.Record.ModifiedUtc == null) {
instance.Record.ModifiedUtc = _clock.UtcNow;
}
if (instance.Record.OwnerId == 0) {
instance.Owner = _authenticationService.GetAuthenticatedUser();
if (instance.Owner != null)
instance.Record.OwnerId = instance.Owner.Id;
}
}
protected override void UpdateEditors(UpdateContentContext context) {
var part = context.ContentItem.As<CommonPart>();
if (part==null)
return;
part.Record.ModifiedUtc = _clock.UtcNow;
}
void LoadOwnerModel(LoadContentContext context, CommonPart part) {
part.LoadOwner(() => _contentManager.Get<IUser>(part.Record.OwnerId));
part.LoadContainer(() => part.Record.Container == null ? null : _contentManager.Get(part.Record.Container.Id));
}
}
}

View File

@@ -2,6 +2,6 @@ using Orchard.Core.Common.Records;
using Orchard.Models; using Orchard.Models;
namespace Orchard.Core.Common.Models { namespace Orchard.Core.Common.Models {
public class RoutablePart : ContentPart<RoutableRecord> { public class RoutableAspect : ContentPart<RoutableRecord> {
} }
} }

View File

@@ -1,11 +0,0 @@
using Orchard.Core.Common.Records;
using Orchard.Data;
using Orchard.Models.Driver;
namespace Orchard.Core.Common.Models {
public class RoutablePartProvider : ContentProvider {
public RoutablePartProvider(IRepository<RoutableRecord> repository) {
Filters.Add(new StorageFilter<RoutableRecord>(repository));
}
}
}

View File

@@ -0,0 +1,7 @@
using Orchard.Models.Driver;
namespace Orchard.Core.Common.Providers {
public class BodyAspectProvider : ContentProvider {
}
}

View File

@@ -0,0 +1,90 @@
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Records;
using Orchard.Data;
using Orchard.Models;
using Orchard.Models.Driver;
using Orchard.Security;
using Orchard.Services;
namespace Orchard.Core.Common.Providers {
public class CommonAspectProvider : ContentProvider {
private readonly IClock _clock;
private readonly IAuthenticationService _authenticationService;
private readonly IContentManager _contentManager;
public CommonAspectProvider(
IRepository<CommonRecord> repository,
IClock clock,
IAuthenticationService authenticationService,
IContentManager contentManager) {
_clock = clock;
_authenticationService = authenticationService;
_contentManager = contentManager;
Filters.Add(new StorageFilter<CommonRecord>(repository));
AddOnCreating<CommonAspect>(DefaultTimestampsAndOwner);
AddOnLoaded<CommonAspect>(LazyLoadHandlers);
AddOnActivated<CommonAspect>(PropertySetHandlers);
}
void DefaultTimestampsAndOwner(CreateContentContext context, CommonAspect instance) {
// assign default create/modified dates
if (instance.Record.CreatedUtc == null) {
instance.Record.CreatedUtc = _clock.UtcNow;
}
if (instance.Record.ModifiedUtc == null) {
instance.Record.ModifiedUtc = _clock.UtcNow;
}
// and use the current user as Owner
if (instance.Record.OwnerId == 0) {
instance.Owner = _authenticationService.GetAuthenticatedUser();
if (instance.Owner != null)
instance.Record.OwnerId = instance.Owner.Id;
}
}
void LazyLoadHandlers(LoadContentContext context, CommonAspect aspect) {
// add handlers that will load content for id's just-in-time
aspect.OwnerField.Loader(() => _contentManager.Get<IUser>(aspect.Record.OwnerId));
aspect.ContainerField.Loader(() => aspect.Record.Container == null ? null : _contentManager.Get(aspect.Record.Container.Id));
}
static void PropertySetHandlers(ActivatedContentContext context, CommonAspect aspect) {
// add handlers that will update records when aspect properties are set
aspect.OwnerField.Setter(user => {
if (user == null) {
aspect.Record.OwnerId = 0;
}
else {
aspect.Record.OwnerId = user.ContentItem.Id;
}
return user;
});
aspect.ContainerField.Setter(container => {
if (container == null) {
aspect.Record.Container = null;
}
else {
aspect.Record.Container = container.ContentItem.Record;
}
return container;
});
}
protected override void UpdateEditors(UpdateContentContext context) {
var part = context.ContentItem.As<CommonAspect>();
if (part == null)
return;
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
// kind of a loose rule of thumb. may not be sufficient
part.Record.ModifiedUtc = _clock.UtcNow;
}
}
}

View File

@@ -0,0 +1,11 @@
using Orchard.Core.Common.Records;
using Orchard.Data;
using Orchard.Models.Driver;
namespace Orchard.Core.Common.Providers {
public class RoutableAspectProvider : ContentProvider {
public RoutableAspectProvider(IRepository<RoutableRecord> repository) {
Filters.Add(new StorageFilter<RoutableRecord>(repository));
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
namespace Orchard.Core.Common.Utilities {
public class LazyField<T> {
private T _value;
private Func<T> _loader;
private Func<T, T> _setter;
public T Value {
get {return GetValue();}
set {SetValue(value);}
}
public void Loader(Func<T> loader) {
_loader = loader;
}
public void Setter(Func<T, T> setter) {
_setter = setter;
}
private T GetValue() {
if (_loader != null) {
_value = _loader();
_loader = null;
}
return _value;
}
private void SetValue(T value) {
_loader = null;
if (_setter != null) {
_value = _setter(value);
}
else {
_value = value;
}
}
}
}

View File

@@ -61,12 +61,13 @@
<Reference Include="System.Web.Mobile" /> <Reference Include="System.Web.Mobile" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Common\Models\CommonPartProvider.cs" /> <Compile Include="Common\Utilities\LazyField.cs" />
<Compile Include="Common\Models\CommonPart.cs" /> <Compile Include="Common\Providers\CommonAspectProvider.cs" />
<Compile Include="Common\Models\BodyPartProvider.cs" /> <Compile Include="Common\Models\CommonAspect.cs" />
<Compile Include="Common\Models\BodyPart.cs" /> <Compile Include="Common\Providers\BodyAspectProvider.cs" />
<Compile Include="Common\Models\RoutablePart.cs" /> <Compile Include="Common\Models\BodyAspect.cs" />
<Compile Include="Common\Models\RoutablePartProvider.cs" /> <Compile Include="Common\Models\RoutableAspect.cs" />
<Compile Include="Common\Providers\RoutableAspectProvider.cs" />
<Compile Include="Common\Records\CommonRecord.cs" /> <Compile Include="Common\Records\CommonRecord.cs" />
<Compile Include="Common\Records\RoutableRecord.cs" /> <Compile Include="Common\Records\RoutableRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -47,7 +47,7 @@ namespace Orchard.Sandbox.Controllers
public ActionResult Create(PageCreateViewModel model) { public ActionResult Create(PageCreateViewModel model) {
var page = _contentManager.Create<SandboxPage>("sandboxpage", item => { var page = _contentManager.Create<SandboxPage>("sandboxpage", item => {
item.Record.Name = model.Name; item.Record.Name = model.Name;
item.As<CommonPart>().Container = CurrentSite.ContentItem; item.As<CommonAspect>().Container = CurrentSite.ContentItem;
}); });
return RedirectToAction("show", new { page.ContentItem.Id }); return RedirectToAction("show", new { page.ContentItem.Id });
} }

View File

@@ -11,9 +11,9 @@ namespace Orchard.Sandbox.Models {
// define the "sandboxpage" content type // define the "sandboxpage" content type
Filters.Add(new ActivatingFilter<SandboxPage>("sandboxpage")); Filters.Add(new ActivatingFilter<SandboxPage>("sandboxpage"));
Filters.Add(new ActivatingFilter<CommonPart>("sandboxpage")); Filters.Add(new ActivatingFilter<CommonAspect>("sandboxpage"));
Filters.Add(new ActivatingFilter<RoutablePart>("sandboxpage")); Filters.Add(new ActivatingFilter<RoutableAspect>("sandboxpage"));
Filters.Add(new ActivatingFilter<BodyPart>("sandboxpage")); Filters.Add(new ActivatingFilter<BodyAspect>("sandboxpage"));
Filters.Add(new StorageFilter<SandboxPageRecord>(pageRepository) { AutomaticallyCreateMissingRecord = true }); Filters.Add(new StorageFilter<SandboxPageRecord>(pageRepository) { AutomaticallyCreateMissingRecord = true });
// add settings to site, and simple record-template gui // add settings to site, and simple record-template gui