2010-11-10 61 views
0

我有一个工厂类,我觉得需要重新分解,采取下面的例子:我松散以下领域驱动设计原则,因此这FileFactory类是处理外部依赖的工厂

public class FileFactory 
{ 
    public static FileType Create(string fileName) 
    { 
     if(IsImageFile(fileName))  
     { 
      return new ImageFileType(); 
     } 
     else if(IsDocumentFile(fileName)) 
     { 
      return new DocumentFileType(); 
     } 
     ... 
    } 

    private static bool IsImageFile(string fileName) 
    { 
     string imageFileTypes[] = string[] {".jpg", ".gif", ".png"}; //How to avoid this line of code? 
     return imageFileTypes.Contains(fileName); 
    } 
} 

一个域对象。工厂类是否应该访问存储库/数据库以获取文件类型?

我应该如何处理这种情况下的依赖关系?

回答

0

一个新的,未知的图像文件类型是不太可能的。硬编码这是很好的。

如果您确实有意将该文件列表保留为外部依赖项,请将文件类型列表传递给FileFactory构造函数,并将Create()作为实例方法而不是静态方法。这将让你可以测试和固体。