六、Working with Data(二) Understanding Content Handlers

Content handler定义了当content part有事件发生时响应特定的事件。比如当一个part激活时。content handler使你在content item生命周期的特定时候执行actions,也使你建立数据存储和优先操作data model呈现content item。

通常,通过继承ContentHandler类来为content part定义handler。ContentHandler是基类提供的方法和属性当定义你自己的content handler时通常会需要,另一种定义自己的content handler的方法是创建实现IContentHandler接口的类。

 

Defining Data Repository and Adding Filters

当用持久化数据的content part工作 ,关于handler添加接受IRepository参数类型的在part中你为record定义的对象的构造器。下面代码展示了content handler的基本的实现,MapRecord是在不同文件中定义的类。

using Map.Models;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;

namespace Map.Handlers {
    public class MapHandler : ContentHandler {
        public MapHandler(IRepository<MapRecord> repository) {
            Filters.Add(StorageFilter.For(repository));
        }
    }
}

StorageFilter 类负责从repository objectg到数据库持久化。你能为content handler添加其它类型的filters。例如:你能添加一个ActivatingFilter到Filters集合来定义part怎样添加到一个type中。

 

Lifecycle Events

除了定义repository,你能为handing events添加代码,使用下面的方法添加的代码为event执行。

  • OnActivated
  • OnCreated
  • OnCreating
  • OnIndexed
  • OnIndexing
  • OnInitializing
  • OnLoaded
  • OnLoading
  • OnPublished
  • OnPublishing
  • OnRemoved
  • OnRemoving
  • OnUnpublished
  • OnUnpublishing
  • OnVersioned
  • OnVersioning

例如:TagPartHandler类包含Removed和Indexing事件的处理代码。像下面的例子:

public class TagsPartHandler : ContentHandler {
    public TagsPartHandler(IRepository<TagsPartRecord> repository, ITagService tagService) {
        Filters.Add(StorageFilter.For(repository));

        OnRemoved<TagsPart>((context, tags) => 
            tagService.RemoveTagsForContentItem(context.ContentItem));

        OnIndexing<TagsPart>((context, tagsPart) => 
            context.DocumentIndex.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
    }
}

 

Data Manipulation

你能重写下面的方法执行与数据的状态关联actions。

  • GetItemMetadata
  • BuildDisplayShape
  • BuildEditorShape
  • UpdateEditorShape

例如:BlogPostPartHandler类重写了GetItemMetadata方法,使用像下面的代码添加路由值:

protected override void GetItemMetadata(GetContentItemMetadataContext context) {
    var blogPost = context.ContentItem.As<BlogPostPart>();

    if (blogPost == null)
        return;

    context.Metadata.CreateRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Create"},
        {"blogId", blogPost.BlogPart.Id}
    };
    context.Metadata.EditorRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Edit"},
        {"postId", context.ContentItem.Id},
        {"blogId", blogPost.BlogPart.Id}
    };
    context.Metadata.RemoveRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Delete"},
        {"postId", context.ContentItem.Id},
        {"blogSlug", blogPost.BlogPart.As<RoutePart>().Slug}
    };
}

转载于:https://www.cnblogs.com/ibrady/archive/2012/03/22/2412434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值