简体   繁体   English

PRISM:RegisterType 给出隐式引用转换错误

[英]PRISM: RegisterType gives implicit reference conversion error

I have got three projects in my solution.我的解决方案中有三个项目。

One holds the interface:一个持有接口:

public interface ISetupData
{
    Dictionary<string, string> Data { get; set; } 
}

Another an implementation of the interface:另一个接口的实现:

public class SetupData : ISetupData
{
    public Dictionary<string, string> Data { get; set; }
}

The third the uses both:第三个同时使用:

Container.RegisterType<ISetupData, SetupData>();

I have set the references in the projects so everything is recognised.我已经在项目中设置了引用,所以一切都得到了认可。 But when i compile i get the error:但是当我编译时出现错误:

The type SetupData cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType(Microsoft.Practices.Unity.IUnityContainer, params Microsoft.Practices.Unity.InjectionMember[])'.类型 SetupData 不能用作泛型类型或方法“Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType(Microsoft.Practices.Unity.IUnityContainer, params Microsoft.Practices.Unity.InjectionMember[])”中的类型参数“TTo”。 There is no implicit reference conversion from SetupData to ISetupData.没有从 SetupData 到 ISetupData 的隐式引用转换。

If I put the declaration of the interface into the project of the implementation of the interface it works fine.如果我将接口的声明放入接口实现的项目中,它就可以正常工作。

I don't have any idea what this error means.我不知道这个错误是什么意思。 Can anyone help?任何人都可以帮忙吗?

So I have the following, which works for me for getting report data out of my database context - I have simplified it to just the parts I think you might be interested in:所以我有以下内容,它适用于我从我的数据库上下文中获取报告数据 - 我已将其简化为我认为您可能感兴趣的部分:

UnityConfig class UnityConfig

using System;
using Microsoft.Practices.Unity;
using WeeklyReport.Repository;
using System.Web.Mvc;
using Unity.Mvc3;

namespace WeeklyReport
{
    public class UnityConfig
    {
        public static void RegisterContainer()
        {
            var container = new UnityContainer();

            //ensure the repository is disposed after each request by using the lifetime manager
            container.RegisterType<IDataRepository, DataRepository>(new HierarchicalLifetimeManager());

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        }
    }
}

IDataRepository class IDataRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeeklyReport.Models;

namespace WeeklyReport.Repository
{
    public interface IDataRepository
    { 
        IEnumerable<ReportViewModel> GetAllMetrics();
    }
}

and my Repository class:和我的存储库类:

using System;
using System.Collections.Generic;
using System.Linq;
using WeeklyReport.Models;

namespace WeeklyReport.Repository
{
    public class DataRepository : IDataRepository
    {
         // ....
         public IEnumerable<ReportViewModel> GetAllMetrics()
         {
             // ....
         }
    }
}

I then instantiate private readonly IDataRepository repository;然后我实例化private readonly IDataRepository repository; in my Controller and I can call repository.GetAllMetrics() in my function that returns the data to my view.在我的控制器中,我可以在我的函数中调用repository.GetAllMetrics()将数据返回到我的视图。

Probably goes without saying, but hopefully you added the NuGet Package Unity .可能不言而喻,但希望您添加了 NuGet Package Unity

I originally ran across the same error you did because I forgot to add : IDataRepository on my DataRepository class, meaning I was not inheriting the IDataRepository .我最初遇到了与您相同的错误,因为我忘记在我的 DataRepository 类上添加: IDataRepository ,这意味着我没有继承IDataRepository I see that is not the problem in your case, but that is what the error means - it means the relationship is not established and the interface cannot find its underlying covariant ( ISetupData cannot find SetupData ).我看到这不是您的问题,但这就是错误的含义 - 这意味着关系未建立并且接口找不到其底层协变( ISetupData找不到SetupData )。

I do not see that you created a UnityContainer , but are calling Container directly, instead.我没有看到您创建了UnityContainer ,而是直接调用Container Probably not the best practice.可能不是最佳实践。 And you would have needed to then do DependencyResolver.SetResolver(new UnityDependencyResolver(container));然后你需要做DependencyResolver.SetResolver(new UnityDependencyResolver(container)); . . I would recommend creating an instance of the UnityContainer , not directly calling Container .我建议创建UnityContainer的实例,而不是直接调用Container

You said it worked when you "put the declaration of the interface into the project of the implementation of the interface"?你说你“把接口的声明放到接口实现的项目中”就行了? Sorry, but that was EXTREMELY confusing and I have no idea what you meant by that.抱歉,这太令人困惑了,我不知道你的意思。

I don't get why you are doing { get; set; }我不明白你为什么要这样做{ get; set; } { get; set; } { get; set; } on both sides. { get; set; }两边。 One is supposed to be the implementation for the other, and you had Dictionary , not IDictionary .一个应该是另一个的实现,你有Dictionary ,而不是IDictionary It should look like this:它应该是这样的:

public interface ISetupData
{
    IDictionary<string, string> Data;
}

After instantiating private readonly ISetupData myData;实例化private readonly ISetupData myData; you should be able to use myData.Data to mean SetupData .您应该能够使用myData.Data来表示SetupData

So I think if you use that, and do:所以我想如果你使用它,并做:

using System;
using Microsoft.Practices.Unity;
using WeeklyReport.Repository;
using System.Web.Mvc;
using Unity.Mvc3;

namespace WeeklyReport
{
    public class UnityConfig
    {
        public static void RegisterContainer()
        {
            var container = new UnityContainer();

            //ensure the repository is disposed after each request by using the lifetime manager
            container.RegisterType<ISetupData, SetupData>(new HierarchicalLifetimeManager());

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        }
    }
}

You should've been fine, as long as your Global.ascx.cs had UnityConfig.RegisterContainer();你应该没问题,只要你的Global.ascx.csUnityConfig.RegisterContainer(); in the App_Start section.App_Start部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM