存储库模式基本?

时间:2014-04-02 10:32:20

标签: c# entity-framework

有人可以向我解释这些陈述在C#中的意思吗?:

class RepositoryBase : IDisposable, IRepositoryBase where TEntity : class{}

class DbContextFactory : IContextFactory where TContext : DbContext, new() 

我了解RepositoryBase继承自IDisposableIRepositoryBase类。它后面的东西是什么意思?

3 个答案:

答案 0 :(得分:1)

类型约束

这些都是类型约束的示例。您可以从the official documentation了解更多相关信息:

  

where T : class:type参数必须是引用类型;这也适用于任何类,接口,委托或数组类型。

     

where T : new():type参数必须具有公共无参数构造函数。与其他约束一起使用时,必须最后指定 new()约束。

     

where T : <base class name>:type参数必须是或来自指定的基类。

所以在你的情况下:

class RepositoryBase<TEntity> : /* etc */ where TEntity : class{}

表示TEntity中的RepositoryBase<TEntity>必须是引用类型,即对象。

对于第二种情况:

class DbContextFactory<TContext> : /* etc */ where TContext : DbContext, new() 

表示TContext必须是DbContext的实例,并且TContext必须具有

形式的公共构造函数
public TContext() { /* etc */ }

接口

原帖海报说:

  

我了解RepositoryBase继承自IDisposableIRepositoryBase类。它后面的东西是什么意思?

声明

class RepositoryBase : IDisposable, IRepositoryBase

表示RepositoryBase 实施 IDisposableIRepositoryBase 接口 RepositoryBase 不会从中继承 ,因为它们不是可以继承的类#34;来自(在C#意义上)。

相反,IDisposableIRepositoryBase都是 接口 ,它们仅将行为和方法指定为合同,但不指定实现。在C#中,将所有接口命名为以大写I前缀开头的惯例是惯例。

答案 1 :(得分:0)

你不是说

public class RepositoryBase<TEntity> : IDisposable, IRepositoryBase where TEntity : class{} 

然后它只是意味着TEntity泛型类型被认为是一个类,就是全部。

答案 2 :(得分:-1)

RepositoryBase实现了接口IDisposable和IRepositoryBase