简体   繁体   English

对于泛型+非泛型,是否/为什么需要从两次继承 WebViewPage?

[英]Does/why does WebViewPage need to be inherited from twice, for generic+non-generic?

I've inherited a codebase, and found some code that I can't figure out why (and if ) it's needed.我继承了一个代码库,发现了一些我无法弄清楚为什么(以及是否需要)它的代码。

It's a custom ViewPage , but we have the exact same code repeated twice - once for ViewPage and once for ViewPage<T> .这是一个自定义ViewPage ,但我们将完全相同的代码重复了两次 - 一次用于ViewPage一次用于ViewPage<T>

Basically:基本上:

public class MyPageBase : WebViewPage
{
    //A whole bunch of properties intended to be accessible on every page
}
public class MyPageBase<T> : WebViewPage<T>
{
    //The exact same properties. Doesn't actually use T anywhere. The code is literally identical.
}

Having so much repeated code is far from ideal.拥有如此多的重复代码远非理想。 Worse, I don't understand why it's needed.更糟糕的是,我不明白为什么需要它。 A few tests have shown that the top one doesn't seem to do anything, but I'm unable to do a comprehensive search of all views (this MyPageBase is used dozens of apps).一些测试表明,最上面的一个似乎并没有做任何事情,但我不能做一个全面的搜索的所有视图(这MyPageBase使用几十个应用程序)。

So my question is: Does/why does WebViewPage need to be inherited from twice, for generic+non-generic?所以我的问题是:对于泛型+非泛型,是否/为什么 WebViewPage 需要从两次继承?

first of all it's not inherited twice you have two implementation of the same class one is generic and other non generic.首先,它没有被继承两次,你有同一个类的两个实现,一个是通用的,另一个是非通用的。

you haven't provided us the inner code so here goes an example, say you have something like this..你还没有向我们提供内部代码,所以这里有一个例子,假设你有这样的东西..

public class MyPageBase<T> : WebViewPage<T>
    {
        //The exact same properties
        private DbContext db;
        public MyPageBase()
        {
            db = new MPContext();
        }

        public List<T> Fill()
        {
            return db.Set<T>().ToList();
        }

        public T FillBy(object id)
        {
            return db.Set<T>().Find(id);
        }
    }

So why do you need a generic page?那么为什么需要一个通用页面呢? if there are some tasks that are common in all pages you just make a generic method to do the job.如果有一些任务在所有页面中都是通用的,您只需使用通用方法来完成这项工作。 below is a very sample usage.下面是一个非常示例的用法。 say you have USERS AND ORDERS tables in your dbcontext假设您的 dbcontext 中有USERS AND ORDERS

public class UsersPage<USERS>:MyPageBase<USERS>{

  public void Index()
  {
     var filledData = Fill<USERS>();
  }
}

public class UsersPage<ORDERS >:MyPageBase<ORDERS >{

  public void Index()
  {
     var filledData = Fill<ORDERS>();
  }
}

ofcourse you could easily do this by当然你可以很容易地做到这一点

var filledData = db.USERS.ToList();

and you can ask why all the fuss?你可以问为什么所有的大惊小怪? to implement the generic methods but some times there will happen to be more complex scenarios than fetching all the records etc.实现通用方法,但有时会发生比获取所有记录等更复杂的场景。

say you have 20+ tables and you decide to fill only 5 records from each table.假设您有 20 多个表,并且您决定只填充每个表中的 5 条记录。 without a generic implementation you know have to go all over 20+ pages and change your code如果没有通用的实现,你知道必须遍历 20 多页并更改代码

from

var filledData = db.TABLE_TYPE.ToList();

to

var filledData = db.TABLE_TYPE.Take(5).ToList()

however with generics you could just fix it in the below method, you could even make it parametric但是使用泛型你可以在下面的方法中修复它,你甚至可以让它参数化

public List<T> Fill()
    {
        return db.Set<T>().Take(5).ToList();
    }

and you are safe..你很安全..

now If you were to use the non generic implementation of MyPageBase现在如果您要使用 MyPageBase 的非通用实现

all these stuff you needed to do you had to write them over and over again.所有这些你需要做的事情,你必须一遍又一遍地写。

ofcourse writing more and more code gives you experience but after a while when working in a program especially on a large scale you want to keep things simple, understandable and maintable as possible..当然,编写越来越多的代码会给你带来经验,但一段时间后,当你在一个程序中工作时,尤其是在大规模的程序中,你希望尽可能保持简单、易懂和可维护。

I'm sorry for my bad english,我很抱歉我的英语不好,

I hope I was clear and this helped you!我希望我很清楚,这对你有帮助!

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

相关问题 为什么通用测试会失败,但非通用测试却不会? - Why does a generic test fail but the non generic does not? 为什么泛型IList &lt;&gt;不继承非泛型IList - Why generic IList<> does not inherit non-generic IList 为什么存在通用和非通用IComparable接口? - Why does a generic and a non-generic IComparable interface exist? 为什么泛型类型在方法签名中出现两次? - Why does the generic type appear twice in method signatures? 为什么 IEnumerator<T> 继承自 IDisposable 而非泛型 IEnumerator 不? - Why does IEnumerator<T> inherit from IDisposable while the non-generic IEnumerator does not? 为什么编译器允许使用可空和不可空泛型参数来实例化泛型类? - Why does the compiler allow instantiation of generic class both with a nullable and non-nullable generic parameter? 为什么实现具有类型约束的通用接口的泛型类需要重复这些约束? - Why does a generic class implementing a generic interface with type constraints need to repeat these constraints? 为什么这个通用演员会失败 - Why does this generic cast fail 为什么要使用通用ICollection <T> 不继承一些带有Count属性的非通用接口? - Why generic ICollection<T> does not inherit some non-generic interface with Count property? 从泛型类型继承的数组 - Array of inherited from generic types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM