统一通用接口和具体类

时间:2017-02-01 19:11:01

标签: c# .net asp.net-web-api unity-container

我有一个通用的存储库接口,

IRepositroy<T>

抽象基类:

public abstract class RepositoryBase
{
    protected readonly IDbContextFactory _dbContextFactory;
    protected readonly IUnitOfWork _unitOfWork;

    protected RepositoryBase(IDbContextFactory dbContextFactory, IUnitOfWork uow)
    {
        this._dbContextFactory = dbContextFactory;
        _unitOfWork = uow;
    }
}

然后我有实现这些接口的具体存储库类

public class CarRepo : RepositoryBase, IRepository<Car> ...

我试图将这些注入到我的WebApi控制器中,但是没有成功。

我收到错误消息,说我没有无参数的ctor,并且无法将CarRepo强制转换为IRepository。

在我的容器配置中,我有:

container.RegisterType(typeof(IRepository<>), typeof(CarRepo));

以下是我在尝试导航到该控制器方法时得到的结果:

{&#34;消息&#34;:&#34;发生错误。&#34;,&#34; ExceptionMessage&#34;:&#34;尝试创建控制器时发生错误键入&#39; CarController&#39;。确保控制器具有无参数的公共构造函数。&#34;,&#34; ExceptionType&#34;:&#34; System.InvalidOperationException&#34;,&#34; StackTrace&#34;:&#34;在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)\ r \ n在System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage请求)\ r \ n在System.Web上.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()&#34;,&#34; InnerException&#34;:{&#34; Message&#34;:&#34;发生错误。&#34;, &#34; ExceptionMessage&#34;:&#34;依赖关系的解析失败,输入= \&#34; CarController \&#34;,name = \&#34;(none)\&#34;。\ r \ n发生异常时:解析参数\&#34; repository \&#34;构造函数CareController(IRepository 1[[CarRepository, DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] repository).\r\nException is: InvalidCastException - Unable to cast object of type 'CarRepository' to type 'IRepository 1 [Repository]&#39;。\ r \ n --------------------------- -------------------- \ r \ n在异常时,容器是:\ r \ n \ r \ n解析CarController,(无)\ r \ n \ n解析参数\&#34;存储库\&#34;构造函数CarController(IRepository 1[[CarRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] repository)\r\n","ExceptionType":"Microsoft.Practices.Unity.ResolutionFailedException","StackTrace":" at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable 1 resolverOverrides)\ r \ n在Microsoft.Practices.Unity.UnityContainer.Resolve(Type t,String name,ResolverOverride) [] resolverOverrides)\ r \ n在Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer容器,Type t,ResolverOverride []覆盖)\ r \ n在Unity.WebApi.UnityDependencyScope.GetService(类型serviceType)\ r \ n在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage请求,输入controllerType,Func 1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'CarRepository' to type 'IRepository 1 [CarRepository]&#39;。&#34;,&#34; ExceptionType&#34;:&# 34; System.InvalidCastException&#34;,&#34; StackTrace&#34;:&#34;在lambda_method(Closure,IBuilderContext)\ r \ n在Microsoft.Practices.ObjectBuilder2.DynamicBuildP lanGenerationContext。&lt;&gt; c__DisplayClass1.b__0(IBuilderContext context)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\ r \ n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\ r \ n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name,IEnumerable`1 resolverOverrides)&#34; }}}

1 个答案:

答案 0 :(得分:0)

由NightOwl888复制的评论中的帖子略有不同,但它显示了正在执行的操作的正确实现。您需要RepositoryBase类中的构造函数。 (public RepositoryBase(){})

这基本上可以确保您的抽象类具有默认实现。