Orchard CMS でプロパティ インジェクションを機能させようとしています (そして失敗しています)。
以下のコードはビューのコード ビハインドのように動作しているため、これが必要です (恐ろしいことです)。その理由は、コンストラクター インジェクションを使用できるコントローラーがビューにないためです。これは、 の代替ビューMenuItem
、つまりMenuItemLink-MyCustomMenuItem.cshtml
.
コメントの内容を除いて、それ以上言うことはありません (以下のコードで設定しようとしているプロパティの NULL コメントに注意してください)。
ああ、Orchard のプロパティ インジェクション コードを適応させてみましたが、同様にうまくいきLoggingModule
ません。
方法:
を。以下のプロパティ注入を機能させますか? (いずれにせよ必要になると確信しています)
b. (可能であれば)ビューの邪魔になる自分のコントローラー/ドライバーを取得して、代わりにコントローラーでコンストラクターインジェクションを使用できるようにしますか?
using System.Diagnostics;
using System.Xml.Linq;
using Autofac;
using Autofac.Core;
using Orchard;
using Module = Autofac.Module;
namespace MyCo.MyCustomMenuItem.Services
{
public class MyCustomMenuItemModule : Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry,
IComponentRegistration registration)
{
if (implementationType.ToString() ==
"MyCo.MyCustomMenuItem.Services.MyCustomMenuItem")
{
// this does get called, but doesn't have the desired effect
registration.Activated += (s, e) =>
e.Context.InjectUnsetProperties(e);
}
}
}
public interface IFeedX : IDependency
{
XDocument GetResource();
}
public class FeedX : IFeedX
{
public XDocument GetResource()
{
return new XDocument();
}
}
public interface IMyCustomMenuItem : IDependency
{
XDocument GetResourceData();
IFeedX FeedX { get; set; }
}
public class MyCustomMenuItem : IMyCustomMenuItem
{
public IFeedX FeedX { get; set; }
// called direct by razor view
public XDocument GetResourceData()
{
Debug.WriteLine(FeedX); // NULL??
return FeedX.GetResource();
}
}
}