我在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