简体   繁体   English

具有多个公共方法的私有嵌套类

[英]Private nested class with multiple public methods

I got class to manage 3 different upload operations: 我上课管理3种不同的上传操作:

internal class UploadManager
{
   internal UploadManager(UploadOperation uploadOperation)
   {
       switch (uploadOperation.OperationType)
       {
           //run different set of async tasks based on OperationType, 
           //using nested class Uploader
       }
   }


   private class Uploader
   {
        //do all upload work
   }
}

Difference between 3 operations in Wcf operation contract they use, and data type to send. Wcf操作合同中使用的3个操作之间的区别以及要发送的数据类型。 What is the best achitecture for this case? 这种情况下最好的架构是什么? Make 3 different Uploader classes? 设置3个不同的Uploader类? Or 1 class with 3 methods? 还是1类3种方法? Or 1 class with 3 overloads for one method? 还是一种方法有3个重载的1类?

Overall should i use nested class for it? 总的来说,我应该使用嵌套类吗? Thanks. 谢谢。

add after comments: Q: will operation types evolve during time? 在评论后添加:问:操作类型会随时间变化吗? A: No 答:不可以

Q: Will you add more of them? 问:您会添加更多吗? A: Yes (1-5 more) 答:是(还有1-5个)

Q: Do they (will) need more parameters and they won't be shared across all types? 问:它们是否会需要更多参数,并且不会在所有类型之间共享? A: No 答:不可以

You should avoid instancing classes in your UploadManager, and instead provide a UploaderFactory class which selects the specific uploader to use. 您应该避免实例化您的UploadManager中的类,而应提供一个UploaderFactory类,该类选择要使用的特定上载器。 To make this happen the Uploader and uploaderFactory need to implement interfaces (IUploader and IUploaderFactory). 为了实现这一点,Uploader和uploaderFactory需要实现接口(IUploader和IUploaderFactory)。 The factory has a method - GetUploader(...) which returns an instance of Uploader of the correct type. 工厂有一个方法-GetUploader(...),它返回正确类型的Uploader的实例。

No need for private classes - make it internal. 无需私人班级-使其内部化。 The most important thing is to isolate functionality make everything testable. 最重要的是隔离功能使所有内容都可测试。

So something like this: 所以像这样:

interface IUploader
{
    void DoStuff();
}

interface IUploaderFactory
{
    IUploader GetUploader(UploadOperation uploadOperation);
}

internal class UploadManager
{
    internal UploadManager(IUploaderFactory uploaderFactory, UploadOperation uploadOperation)
    {
        var uploader = uploaderFactory.GetUploader(uploadOperation);

        //run different set of async tasks based on OperationType, 
        //using nested class Uploader
        uploader.DoStuff();
    }
}

internal class Uploader1 : IUploader
{
    public void DoStuff()
    {
        ...
    }
}

internal class Uploader2 : IUploader
{
    public void DoStuff()
    {
        ...
    }
}

internal class Uploader3 : IUploader
{
    public void DoStuff()
    {
        ...
    }
}

The main benefits of this approach are: 这种方法的主要优点是:

  • You can test each instance separatly. 您可以分别测试每个实例。 For instanace - you test the factory that it returns the correct Uploader, and in a whole different test-set you test the specific uploaders. 为了即时起见-您可以测试工厂是否返回正确的上载器,并在一个完整的测试集中测试特定的上载器。
  • You can easily add new uploaders with minimal change in code (only the new uploader and the factory needs to be modified. Oh, and the UploadOperation structure). 您可以轻松地添加新的上载器,而无需更改代码(只需修改新的上载器和工厂。哦,还有UploadOperation结构)。

This is kind of vague answer, but I think it adresses the arcitecture approach. 这是一个模糊的答案,但我认为它是一种架构方法。

Hope it helps - good luck! 希望对您有所帮助-祝您好运!

使用KISS原理,我将通过3种方法参加1节课。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM