设计通过继承静态类W /共享的功能 [英] Design Static Classes w/ Shared Functionality through Inheritance

查看:86
本文介绍了设计通过继承静态类W /共享的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字,保持和检索数据对象库静态类。我想提供一些常用的功能给他们,所以我让他们从一个抽象类继承。



这是不行的,因为静态类不允许继承。我相信在.NET设计,所以我会接受,这是有充分理由的。



因此,这使我相信,我有一些基本的设计问题在这里。



我的问题是我怎么设计我的代码来实现我在寻找的结果?或者是它只是一个具有复制在我所有的类中的方法的问题。也许单



静态回购:



 公共静态类CreditMemos:InvoiceRepository<贷记通知单> 
{
公共静态列表<贷记通知单> GetByDocumentNumber(字符串documentNumber)
{
返回CheckCache(
ConvertReaderToCreditMemos(
Invoices.GetByDocumentNumber<贷记通知单>(documentNumber)));
}
}



所需的基本:

 抽象类InvoiceRepository< T> 
其中t:DataObjects.Invoice
{
静态列表< T> invoiceCache =新的List< T>();

静态保护名单,LT; T> CheckCache(列表< T>业绩)
{
的foreach(VAR导致的结果)
如果(invoiceCache.Any(P =>!p.ID == result.ID))
invoiceCache.Add(结果);
返回invoiceCache.Where(P => results.Any(A => a.ID == p.ID))。了ToList();
}
}


解决方案

您可能刚落静态从你的继承类,并让他们通过隐藏构造类似的工作:

 公共密封类CreditMemos:InvoiceRepository<贷记通知单> 
{
私人CreditMemos(){}
...

或者你可以让 InvoiceRepository其中p T> 自己的静态类,而不是继承,你只需要使用方法在其他静态类上课。 (顺便说一句,该公约是利用在所有类型的名称,包括通用类型的第一个字母,所以 T ,而不是 T

 公共静态类CreditMemos 
{
公共静态列表<贷记通知单> GetByDocumentNumber(字符串documentNumber)
{
返回InvoiceRepository<贷记通知单> .CheckCache(新名单<贷记通知单>());
}
}
公共静态类InvoiceRepository< T>
,其中T:DataObjects.Invoice
{
静态列表< T> invoiceCache =新的List< T>();

静态内部列表< T> CheckCache(列表< T>业绩)
{
的foreach(VAR导致的结果)
如果(invoiceCache.Any(P =>!p.ID == result.ID))
invoiceCache.Add(结果);
返回invoiceCache.Where(P => results.Any(A => a.ID == p.ID))。了ToList();
}
}


I have a number of static repository classes that hold and retrieve data objects. I want to provide some common functionality to them, so I have them inherit from an abstract class.

This doesn't work, since static classes are not allowed to inherit. I trust in the .NET design, so I will accept that this is for good reason.

So this leads me to believe I have some fundamental design issues here.

My question is how do I design my code to achieve the result I'm looking for? Or, is it just a matter of having to replicate the methods across all my classes. Perhaps a singleton?

Static Repo:

public static class CreditMemos : InvoiceRepository<CreditMemo>
{
    public static List<CreditMemo> GetByDocumentNumber(string documentNumber)
    {
        return CheckCache(
            ConvertReaderToCreditMemos(
                Invoices.GetByDocumentNumber<CreditMemo>(documentNumber)));
    }
}

Desired Base:

abstract class InvoiceRepository<t>
    where t: DataObjects.Invoice
{
    static List<t> invoiceCache = new List<t>();

    static protected List<t> CheckCache(List<t> results)
    {
        foreach (var result in results)
            if (!invoiceCache.Any(p => p.ID == result.ID))
                invoiceCache.Add(result);
        return invoiceCache.Where(p => results.Any(a => a.ID == p.ID)).ToList();
    }
}

解决方案

You could just drop the static from your inheriting classes, and make them work similarly by hiding the constructor:

public sealed class CreditMemos : InvoiceRepository<CreditMemo>
{
    private CreditMemos() { }
    ...

Or you can make InvoiceRepository<t> its own static class, and instead of inheriting, you just use the methods in your other static classes. (by the way, the convention is to capitalize the first letter in all type names, including generic types, so T, not t)

public static class CreditMemos
{
    public static List<CreditMemo> GetByDocumentNumber(string documentNumber)
    {
        return InvoiceRepository<CreditMemo>.CheckCache(new List<CreditMemo>());
    }
}
public static class InvoiceRepository<T>
    where T: DataObjects.Invoice
{
    static List<T> invoiceCache = new List<T>();

    static internal List<T> CheckCache(List<T> results)
    {
        foreach (var result in results)
            if (!invoiceCache.Any(p => p.ID == result.ID))
                invoiceCache.Add(result);
        return invoiceCache.Where(p => results.Any(a => a.ID == p.ID)).ToList();
    }
}

这篇关于设计通过继承静态类W /共享的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆