惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threatpost
P
Privacy International News Feed
T
Tenable Blog
Know Your Adversary
Know Your Adversary
C
Cisco Blogs
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
爱范儿
爱范儿
U
Unit 42
K
Kaspersky official blog
C
Cybersecurity and Infrastructure Security Agency CISA
Jina AI
Jina AI
O
OpenAI News
L
LangChain Blog
PCI Perspectives
PCI Perspectives
P
Privacy & Cybersecurity Law Blog
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
V
V2EX
L
LINUX DO - 热门话题
S
Security Affairs
量子位
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
月光博客
月光博客
MyScale Blog
MyScale Blog
C
CERT Recently Published Vulnerability Notes
AWS News Blog
AWS News Blog
博客园 - 叶小钗
Forbes - Security
Forbes - Security
W
WeLiveSecurity
T
Troy Hunt's Blog
J
Java Code Geeks
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
Google Online Security Blog
Google Online Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
TaoSecurity Blog
TaoSecurity Blog
H
Help Net Security
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 飞翔的天空

sp.net core部署到iis中出现 HTTP Error 502.5 - Process Failure 的解决办法 aspnet zero Swagger 不出现Authorize按钮的解决办法 Js获取当前日期时间及其它操作 js阿拉伯数字转中文大写 php学习点滴 excel合并单元格内容 EXCEL公式以指定分隔符从右往左截取字符 drupal学习FAQ drupal模块简介 js中三目运算及readonly的解决办法 Zxing中文乱码的简单解决办法 easyUI MVC3一些注意的东东(4) orchard模块编写的错误及其解决办法 orchard文档之-orchard工作原理 orchard文档之-搜索和索引 orchard文档之-更新站点到新的orchard版本 orchard文档之-创建自定义表单 准备把orchard的自己认为重要的文档翻译一遍 orchard文档之-理解数据访问
orchard文档之-理解内容处理器
飞翔的天空 · 2013-05-31 · via 博客园 - 飞翔的天空

理解内容处理器

A content handler defines what happens with a content part in response to specific events, such as when the part is activated. The content handler enables you to perform actions at particular moments in the lifecycle of the content item. It also enables you to set up data repositories and manipulate the data model prior to rendering the content item.

Typically, you define a handler for a content part by creating a class that inherits from ContentHandler. The ContentHandler class is a base class that provides the methods and properties you will commonly need when defining your own content handler. Alternately, you can also create your own content handler by creating a class that implements IContentHandler.

Defining Data Repository and Adding Filters

When working with a content part that persists data, add a constructor for the handler that accepts an IRepository parameter for objects of the type you defined for records in the part. The following code shows a basic implementation of a content handler. MapRecord is a class defined in a separate file.

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));
        }
    }
}

You can add other types of filters to the content handler. For example, you can add an ActivatingFilter to the Filters collection to define how the part is added to a type.

Built-in filter types

  • StorageFilter class - Takes care of persisting the data from repository object to the database. Its usage is shown in the example above.
  • ActivatingFilter class - Attaches a part to a content type from code. As opposed to attaching parts via migrations, parts attached using this filter will neither be displayed in the Dashboard, nor users will be able to remove them from types. It's a legitimate way of attaching parts that should always exist on a given content type.

Lifecycle Events

In addition to defining the repository, you can add code for handling events. You use the following methods to add the code that is executed for the event:

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

For example, the TagPartHandler class contains code to take action for the Removed and Indexing events, as shown in the following example:

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

You can override the following methods to perform actions related to the state of the data:

  • GetItemMetadata
  • BuildDisplayShape
  • BuildEditorShape
  • UpdateEditorShape

For example, the BlogPostPartHandler class overrides the GetItemMetadata method to add route values using code like the following:

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}
    };
}