【发布时间】:2014-04-28 01:15:05
【问题描述】:
我现在正在使用 System.Linq.Dynamic 在我的项目中进行动态查询。我使用 Autofac 作为我的默认 IOC 容器。但是现在我在注册通用组件时遇到了问题,这是我的代码:
界面:
public interface IDynamicQuery
{
IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class;
}
班级:
public class DynamicQuery :IDynamicQuery
{
public DynamicQuery(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
private readonly IUnitOfWork unitOfWork;
public IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class
{
var appRepository = unitOfWork.Repository<T>();
IQueryable<T> queryResult = null;
if (propertyName.Contains('$'))
propertyName = propertyName.Replace('$', '.');
queryResult = appRepository.GetMany(where).Where("" + propertyName + ".Contains(\"" + propertyValue + "\")");
return queryResult;
}
}
然后我将它们注册到应用程序启动条目中:
builder.RegisterType<IDynamicQuery>().As<DynamicQuery>().InstancePerHttpRequest();
但是当我基于 MVC 4 启动我的项目时,它会抛出一个异常,例如:
The type 'TinyFrame.Framework.Query.IDynamicQuery' is not assignable to service 'TinyFrame.Framework.Query.DynamicQuery'.
异常抛出:var container = builder.Build();
我知道如何在 autofac 中注册一个泛型类,但我不知道如何注册我提出的上述类,任何人都可以帮助我吗?我是 autofac 的新手,请多多指教。
【问题讨论】:
-
你弄错了。
IDynamicQuery不是DynamicQuery而是DynamicQuery是IDynamicQuery。 -
@Aron 天哪,我真是太粗心了……我昨晚睡得不好。谢谢你的建议。