如何在 Sitecore 的 WeBlog 模块中覆盖 ITagManager
How to override ITagManager in WeBlog module within Sitecore
我正在尝试实施文档中 to be able to restrict the number of tags which show in the tag cloud in WeBlog. Additionally, I am using these instructions 中的解决方案。
我修改了 WeBlog 配置以指向我自己的自定义 TagManager 实现。
<setting name="WeBlog.Implementation.TagManager" value="My.Namespace.CustomTagManager"/>
如果我加载 sitecore/admin/showconfig.aspx
,我可以确认配置设置已更新为新值。
我的 CustomTagManager
目前是 ITagManager
接口的基本实现。
public class CustomTagManager : ITagManager
{
public string[] GetTagsByBlog(ID blogId)
{
throw new System.NotImplementedException();
}
public string[] GetTagsByBlog(Item blogItem)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetTagsByEntry(EntryItem entry)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetAllTags()
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetAllTags(BlogHomeItem blog)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> SortByWeight(IEnumerable<string> tags)
{
throw new System.NotImplementedException();
}
}
我可以反映部署的DLL,看到这些变化肯定是做了,但是这些变化没有影响。 None 的异常被抛出,标签云继续填充,就好像我根本没有做任何更改一样。就好像配置文件的变化被完全忽略了。
为了编写我自己的客户 TagManager,我还需要更改什么 class?
我正在使用 WeBlog 5.2 和 Sitecore 7.1。
查看 WeBlog 代码后,很明显使用了回退对象,而我的配置更改被忽略了。
这是因为 WeBlog 做了:
var type = Type.GetType(typeName, false);
只有在 mscorlib.dll 或当前程序集中找到类型时,GetType
方法才有效。因此,修复就像提供程序集的完全限定名称一样简单。
<setting name="WeBlog.Implementation.TagManager" value="My.Assembly.CustomTagManager, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
这是微博代码:
private static T CreateInstance<T>(string typeName, Func<T> fallbackCreation) where T : class
{
var type = Type.GetType(typeName, false);
T instance = null;
if (type != null)
{
try
{
instance = (T)Sitecore.Reflection.ReflectionUtil.CreateObject(type);
}
catch(Exception ex)
{
Log.Error("Failed to create instance of type '{0}' as type '{1}'".FormatWith(type.FullName, typeof(T).FullName), ex, typeof(ManagerFactory));
}
}
if(instance == null)
instance = fallbackCreation();
return instance;
}
我正在尝试实施文档中
我修改了 WeBlog 配置以指向我自己的自定义 TagManager 实现。
<setting name="WeBlog.Implementation.TagManager" value="My.Namespace.CustomTagManager"/>
如果我加载 sitecore/admin/showconfig.aspx
,我可以确认配置设置已更新为新值。
我的 CustomTagManager
目前是 ITagManager
接口的基本实现。
public class CustomTagManager : ITagManager
{
public string[] GetTagsByBlog(ID blogId)
{
throw new System.NotImplementedException();
}
public string[] GetTagsByBlog(Item blogItem)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetTagsByEntry(EntryItem entry)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetAllTags()
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> GetAllTags(BlogHomeItem blog)
{
throw new System.NotImplementedException();
}
public Dictionary<string, int> SortByWeight(IEnumerable<string> tags)
{
throw new System.NotImplementedException();
}
}
我可以反映部署的DLL,看到这些变化肯定是做了,但是这些变化没有影响。 None 的异常被抛出,标签云继续填充,就好像我根本没有做任何更改一样。就好像配置文件的变化被完全忽略了。
为了编写我自己的客户 TagManager,我还需要更改什么 class?
我正在使用 WeBlog 5.2 和 Sitecore 7.1。
查看 WeBlog 代码后,很明显使用了回退对象,而我的配置更改被忽略了。
这是因为 WeBlog 做了:
var type = Type.GetType(typeName, false);
只有在 mscorlib.dll 或当前程序集中找到类型时,GetType
方法才有效。因此,修复就像提供程序集的完全限定名称一样简单。
<setting name="WeBlog.Implementation.TagManager" value="My.Assembly.CustomTagManager, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
这是微博代码:
private static T CreateInstance<T>(string typeName, Func<T> fallbackCreation) where T : class
{
var type = Type.GetType(typeName, false);
T instance = null;
if (type != null)
{
try
{
instance = (T)Sitecore.Reflection.ReflectionUtil.CreateObject(type);
}
catch(Exception ex)
{
Log.Error("Failed to create instance of type '{0}' as type '{1}'".FormatWith(type.FullName, typeof(T).FullName), ex, typeof(ManagerFactory));
}
}
if(instance == null)
instance = fallbackCreation();
return instance;
}