Now I am making a dynamic query in my project using System.Linq.Dynamic. I use Autofac as my default IOC container. But now I have a problem with registering common components, here is my code:
interface:
public interface IDynamicQuery { IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class; }
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; } }
Then I will register them in the application launch entry:
builder.RegisterType<IDynamicQuery>().As<DynamicQuery>().InstancePerHttpRequest();
But when I run my project based on MVC 4, it throws me an exception like:
The type 'TinyFrame.Framework.Query.IDynamicQuery' is not assignable to service 'TinyFrame.Framework.Query.DynamicQuery'.
An exception was var container = builder.Build(); : var container = builder.Build();
I know how to register a generic class in autofac, but I donβt know how to register above a class that I raised, can anyone help me with this? I am new to autofac, thanks in advice.
c # autofac
Charlieshi
source share