我在Mef周圍纏繞頭腦,以及導入和導出如何工作。我的項目結構如下。mvc 4 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的控制器,並用導出屬性和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,對於通用庫和inherents標有屬性導出類。
[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中似乎不起作用。
我想問題是我做錯了什麼?爲什麼我的接口在構造函數中最終都是空的,爲什麼會出現關於「爲此對象定義的無參數構造函數」的mvc錯誤。
感謝您的回覆。我使用上面的代碼創建了控制器工廠(我稱之爲MefControllerFactory),將以下行添加到我的global.asax文件中。 ControllerBuilder.Current.SetControllerFactory(typeof運算(MefControllerFactory));它在我加載站點時引發此錯誤:嘗試創建IControllerFactory'MefMVCApp.MefControllerFactory'時發生錯誤。確保控制器工廠具有公共無參數構造函數。] – 2013-04-10 00:04:39
它具有公共無參數構造函數嗎? – pollirrata 2013-04-10 03:52:27
感謝您的幫助。控制器工廠是我失蹤的一塊。我修改了你給我的代碼,現在讓它工作。 – 2013-04-10 18:23:40