Mef と、インポートとエクスポートがどのように機能するかについて頭を悩ませています。私のプロジェクト構造は次のとおりです。
Projects:
MefMVPApp (Main MVC 4 app)
MefMVCFramework.Common(Interfaces shared between the projects)
MefMVCDemo.Plugins.OrderStatus (pluggable area.)
MefMVCDemo.Plugins.Data (Repository for OrderStatus)
OrderStatus.Models(domain models shared between the projects)
メインの Mvc アプリの目標は、mef を介してプラグ可能な領域をホストすることです。
OrderStatus 領域には、OrderStatusController というコントローラーがあり、Export 属性と ImportingConstructor で装飾されています。
[Export(typeof(IController))]
[ExportMetadata("controllerName", "OrderStatus")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class OrderStatusController : Controller
{
private readonly IRepository<OrderStatusApp.OrderStatusResponse>_repository ;
[ImportingConstructor]
public OrderStatusController(IRepository<OrderStatusApp.OrderStatusResponse> oRepository)
{
_repository = oRepository;
}
public ActionResult Index()
{
var model = _repository.GetAll();
return View();
}
}
IRepository は MefMVCFramework.Common アセンブリのクラスであり、一般的な CRUD 操作に使用されます。
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(int id);
void Add(T entity);
int SaveOrUpdate(T entity);
bool Delete(T entity);
bool Delete(int id);
}
MefMVCDemo.Plugins.Data アセンブリには、汎用リポジトリに固有の OrderManagementRepository というクラスが含まれており、エクスポート属性でマークされています。
[Export(typeof(IRepository<OrderStatusApp.OrderStatusResponse>))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class OrderManagementRepository : IRepository<OrderStatusApp.OrderStatusResponse>
{
private readonly JsonServiceClient _client;
public OrderManagementRepository()
{
_client = new JsonServiceClient("http://localhost:52266");
}
public IEnumerable<OrderStatusApp.OrderStatusResponse> GetAll()
{
throw new NotImplementedException("Can not get all");
}
public OrderStatusApp.OrderStatusResponse GetById(int id)
{
throw new NotImplementedException();
}
public void Add(OrderStatusApp.OrderStatusResponse entity)
{
throw new NotImplementedException();
}
public int SaveOrUpdate(OrderStatusApp.OrderStatusResponse entity)
{
throw new NotImplementedException();
}
public bool Delete(OrderStatusApp.OrderStatusResponse entity)
{
throw new NotImplementedException();
}
public bool Delete(int id)
{
throw new NotImplementedException();
}
}
Mefx ツールを使用すると、自分の部品を確認でき、不合格はありません。
mefx /dir:C:\
Source.PreBranch.Keep\Prototypes\Projects\MefDemoApp\mefMVC4App\bin /parts
MefMVCDemo.Plugins.Data.OrderManagementRepository
mefMVCDemo.Plugins.OrderStatus.Controllers.OrderStatusController
MefMVCDemo.Plugins.OrderStatus.Verbs.OrderStatusVerb
インポートが表示されます。
mefx /dir:C:\
Source.PreBranch.Keep\Prototypes\Projects\MefDemoApp\mefMVC4App\bin /imports
MefMVCFramework.Common.IRepository(OrderStatus.Models.OrderStatusApp+OrderStatus
Response)
MefMVCFramework.Common.IRepository(OrderStatus.Models.OrderStatusApp+OrderStatus
Response)
/orderstatus uri を使用してメインの mvc サイトを参照すると、次のエラーが表示されます。このオブジェクトに対してパラメーターなしのコンストラクターが定義されていません。
オーバーロードを取らない OrderStatusController にデフォルトのコンストラクターを追加してもうまくいかないようです。
問題は、私が間違っていることだと思いますか?コンストラクターのインターフェイスがすべて null になるのはなぜですか。また、「このオブジェクトに対してパラメーターなしのコンストラクターが定義されていません」という mvc エラーが発生するのはなぜですか。