简体   繁体   English

无法使用类型创建抽象类或接口接口的实例

[英]Cannot create an instance of the abstract class or interface interface with type

I know what this error is, my question is slightly different. 我知道这个错误是什么,我的问题略有不同。

This is one of these brain fade moments. 这是这些大脑衰落的时刻之一。

I've been reading this interesting article on Repositoy and Unit of Work patterns: 我一直在阅读有关存储库和工作单元模式的这篇有趣的文章:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net- mvc应用

I am having a play with this code and I'm now trying to tweak it so I can use IoC with DI. 我正在使用此代码,现在尝试对其进行调整,以便可以将IoC与DI一起使用。

Now, here is where I have just hit a mental blank. 现在,这是我刚刚陷入精神空白的地方。

I am changing all my classes to use interfaces etc. So given this code snippet: 我将所有类更改为使用接口等。因此,给出以下代码片段:

    public class UnitOfWork : IDisposable
    {
        private SchoolContext context = new SchoolContext();
        private GenericRepository<Department> departmentRepository;
        private GenericRepository<Course> courseRepository;

        public GenericRepository<Department> DepartmentRepository
        {
            get
            {

                if (this.departmentRepository == null)
                {
                    this.departmentRepository = new GenericRepository<Department>(context);
                }
                return departmentRepository;
            }
        }
    }
}

How do I edit this line: 我如何编辑此行:

this.departmentRepository = new GenericRepository<Department>(context);

So I don't have to reference the actual class so I can ensure it is decoupled and avoid producing the syntax error: 因此,我不必引用实际的类,因此可以确保将其解耦并避免产生语法错误:

Cannot create an instance of the abstract class or interface interface with type

Many thanks in advance! 提前谢谢了!

In brief, you should declare the field as an interface type. 简单地说,你应该声明域作为一个接口类型。 The interface could have a name like IGenericRepository<T> . 该接口可以具有类似于IGenericRepository<T>的名称。 Then the class GenericRepository<T> should implement that interface. 那么类GenericRepository<T>应实现该接口。 Then you register the class with your IoC framework, which basically tells the framework, "when I ask for an instance of this interface, give me an instance of that class". 然后,您在IoC框架中注册该类,该类基本上告诉该框架:“当我请求该接口的实例时,请给我该类的实例”。 Then you do something like this: 然后,您可以执行以下操作:

this.departmentRepository = InversionOfControl.Resolve<IGenericRepository<Department>>();

(The resolve method has a signature like public T Resolve<T>() ) (resolve方法的签名类似于public T Resolve<T>()

http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle

Seems GenericRepository be abstract class which cannot be instantiated. 似乎GenericRepository是无法实例化的抽象类。

http://msdn.microsoft.com/en-us/library/sf985hc5(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/library/sf985hc5(v=vs.71).aspx

You need derived class for GenericRepository, ie 您需要派生类为GenericRepository,即

DepartmentRepository : GenericRepository<Department>

暂无
暂无

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

相关问题 无法创建类型的实例。 类型是接口或抽象类,不能被实例化 - Could not create an instance of type . Type is an interface or abstract class and cannot be instantiated 无法创建抽象类或接口的实例,但它不是抽象类,也不是接口? - Cannot create an instance of the abstract class or interface, but it is not an abstract class and it's not an interface? 无法创建抽象类或接口的实例 - Cannot create an instance of the abstract class or interface 无法创建类型 X 的实例。类型是接口或抽象类,无法实例化 - Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated C#声明类型定义无法创建抽象类或接口的实例 - C# Declaring Type Definition Cannot create an instance of the abstract class or interface 无法创建抽象类或接口“ System.Drawing.Image”的实例 - Cannot create an instance of the abstract class or interface 'System.Drawing.Image' 无法在WCF Silverlight服务上创建抽象类或接口的实例 - Cannot create an instance of the abstract class or interface on WCF Silverlight Service 无法创建抽象类或接口“ System.Array”的实例 - Cannot create an instance of the abstract class or interface 'System.Array' “无法创建抽象类或接口的实例” C#错误消息 - “Cannot create an instance of the abstract class or interface” C# error message C#:无法创建抽象类或接口的实例 - C#: Cannot create an instance of the abstract class or interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM