public RepositoryImpl(CodeFirstIOCDemoContext context, Func <T, U> persistRouing, string tableName) { var property = typeof(CodeFirstIOCDemoContextImpl).GetProperty(tableName); // 通过反射的机制,根据“tableName”参数给出的属性名,获取实现IContext某个数据表的具体对象引用 // 并保存到类型变量里,以便将所有的查询、Include、增删改等操作传递给这个对象。 _table = (DbSet <U>)property.GetValue(context._contextImpl, new object[] { }); _query = _table; _persistRouing = persistRouing; _tableName = tableName; _context = context; // 如果类型T和U不是同一个类型,说明要么U继承与T,或者U实现了T这个接口 // 这样一来,为了规避编译器编译错误,需要再封一层 if (typeof(T) != typeof(U)) { IfImplementation = new RepositoryIfImpl(this, tableName); } else // 否则就很简单了 { IfImplementation = (IRepository <T>) this; } }
public RepositoryImpl(CodeFirstIOCDemoContext context) : this(context, null, typeof(U).Name) { }