我可以返回一个FileStream作为一个通用的接口文件?作为一个、接口、文件、FileStream

2023-09-06 06:20:44 作者:挂柯南、挂科难

我正在写需要返回引用二进制文件类接口。通常情况下我会提供一个引用文件作为文件路径。但是,我考虑在数据库中存储一些文件(如小缩略图)直接而不是在文件系统上。在这种情况下,我不希望添加读取缩略图从数据库到光盘上,然后返回一个文件路径为我的程序读取的额外步骤。我想直接流出来的图像数据库到我的程序,避免任何内容写入磁盘,除非用户明确要保存的东西。

可否有我的界面返回的FileStream 甚至图片有意义吗?然后,它会到实现类,以确定是否的FileStream 图片的来源是一个文件在数据库中的光盘或二进制数据。

 公共接口MyInterface的
{
    串缩略图{获取;}
    带安装{获取;}
}
 

VS

 公共接口MyInterface的
{
    图片缩略图{获取;}
    的FileStream附件{获取;}
}
 

解决方案 项目中部分css文件用IP加端口可以访问,用域名不能访问,返回403,这个为什么

有关小规模的内容,其中的用法是明确就像一个缩略图我想你会过得更好返回类型,因为它会使用(即图像)。如果你有大量的内容,其中的目的可能会发生变化也可能是原因阅读部分则的流是他们的路要走。在这两种情况下的内容的源从类的用户不可见。您还需要考虑数据流的处置语义,你是否希望类的用户有过多久数据库连接保持打开状态的控制。

I'm writing a class interface that needs to return references to binary files. Typically I would provide a reference to a file as a file path. However, I'm considering storing some of the files (such as a small thumbnail) in a database directly rather then on a file system. In this case I don't want to add the extra step of reading the thumbnail out of the database onto the disc and then returning a path to the file for my program to read. I'd want to stream the image directly out of the database into my program and avoid writing anything to the disc unless the user explicit wants to save something.

Would having my interface return a FileStreamor even a Imagemake sense? Then it would be up to the implementing class to determine if the source of the FileStream or Image is a file on a disc or binary data in a database.

public interface MyInterface
{ 
    string Thumbnail {get;}
    string Attachment {get;}
}

vs

public interface MyInterface
{ 
    Image Thumbnail {get;}
    FileStream Attachment {get;}
}

解决方案

For small content where the usage is clear like a thumbnail I think you would be better off returning the type as it will be used (i.e. Image). If you've got large content where the purpose may vary or they may be reasons to read it partially then Stream is they way to go. In either case the source of the content is hidden from the user of the class. You will also want to consider the dispose semantics of the stream and whether or not you want the user of the class to have control over how long a database connection stays open.