[英]how to reference the Type in the Concrete class of a generic class
I found the following example, to which I have a follow up question.我找到了以下示例,对此我有一个后续问题。
stack overflow question 堆栈溢出问题
the existing code from the question is问题中的现有代码是
public interface IRepository<T> where T : EntityObject
{
RepositoryInstructionResult Add(T item);
RepositoryInstructionResult Update(T item);
RepositoryInstructionResult Delete(T item);
}
public class Repository<T> : IRepository<T> where T : EntityObject
{
virtual RepositoryInstructionResult Add(T item)
{ //implementation}
virtual RepositoryInstructionResult Update(T item);
{ //implementation}
virtual RepositoryInstructionResult Delete(T item);
{ //implementation}
}
public class BarRepository : Repositorybase<Bar>
{
public override RepositoryInstructionResult Update(Bar item);
{
//Call base method if needed
//Base.Update(item);
//implement your custom logic here
}
}
what I would like to do is change the Update method to something like我想做的是将 Update 方法更改为类似
public class BarRepository : Repositorybase<Bar>
{
// T is of Type Bar
public override RepositoryInstructionResult Update(T item);
{
//implement your custom logic here
}
}
Question: is there a way to expose the generic type in BarResposity: Repositorybase<Bar>
to the methods in BarRepository?问题:有没有办法将
BarResposity: Repositorybase<Bar>
中的泛型类型暴露给 BarRepository 中的方法?
looking for a better alternative to "search and replace" when building out the concrete class (eg make a copy of BarRespository as FooRepository and change all references from Bar to Foo).在构建具体的 class 时寻找“搜索和替换”的更好替代方法(例如,将 BarRespository 复制为 FooRepository 并将所有引用从 Bar 更改为 Foo)。 I would rather change the type in one place only.
我宁愿只在一个地方改变类型。
(edit) Usage needs to remain as (编辑)使用需要保持为
var obj = new BarRepository();
Just as a note, if you're going to override all Add/Update/Delete anyways, you can make them as abstract in the RepositoryBase and then the vs suggestion is your friend:就像一个注释,如果你要覆盖所有的 Add/Update/Delete ,你可以在 RepositoryBase 中将它们作为抽象,然后 vs 建议是你的朋友:
and if there is shared logic between all of concrete classes you can put it in the abstract class and override abstract protected methods instead.如果所有具体类之间存在共享逻辑,则可以将其放在抽象 class 中,并改写抽象受保护方法。
But what is wrong with this但这有什么问题
public class BarRepository<T> : Repository<Bar> where T : class
{
public override void Update(Bar item)
{
}
public void Update(T item)
{
}
}
It's a bit of a hack, but you could use a using
alias to define the entity type:这有点 hack,但您可以使用
using
别名来定义实体类型:
using MyType = Bar;
public class BarRepository : Repositorybase<MyType>
{
public override RepositoryInstructionResult Update(MyType item);
{
return base.Update(item);
}
}
Now when you copy to Foo.cs
, you can just change the using directive to现在,当您复制到
Foo.cs
时,您只需将 using 指令更改为
using MyType = Foo;
But, I would look to try and reuse as much generic code as possible, as it's not clear at all what MyType
is just by looking at the methods.但是,我会尝试尽可能多地重用通用代码,因为仅通过查看方法就不清楚
MyType
是什么。 There's nothing wrong with a find.replace to define a new repository type that customizes actions - you just want to keep the repeated to a minimum. find.replace 定义自定义操作的新存储库类型没有任何问题 - 您只想将重复保持在最低限度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.