PDF ファイルの操作を行うサードパーティ コンポーネントがあります。操作を実行する必要があるときはいつでも、ドキュメント ストア (データベース、SharePoint、ファイル システムなど) から PDF ドキュメントを取得します。少し一貫性を持たせるために、PDF ドキュメントをbyte[]
.
このサードパーティ コンポーネントは、使用する必要があるメイン メソッドの 1 つへのパラメーターとしてMemoryStream[]
(MemoryStream
配列) を想定しています。
この機能を独自のコンポーネントにラップして、アプリケーション内のさまざまな領域でこの機能を使用できるようにしようとしています。私は本質的に次のことを思いついた:
public class PdfDocumentManipulator : IDisposable
{
List<MemoryStream> pdfDocumentStreams = new List<MemoryStream>();
public void AddFileToManipulate(byte[] pdfDocument)
{
using (MemoryStream stream = new MemoryStream(pdfDocument))
{
pdfDocumentStreams.Add(stream);
}
}
public byte[] ManipulatePdfDocuments()
{
byte[] outputBytes = null;
using (MemoryStream outputStream = new MemoryStream())
{
ThirdPartyComponent component = new ThirdPartyComponent();
component.Manipuate(this.pdfDocumentStreams.ToArray(), outputStream);
//move to begining
outputStream.Seek(0, SeekOrigin.Begin);
//convert the memory stream to a byte array
outputBytes = outputStream.ToArray();
}
return outputBytes;
}
#region IDisposable Members
public void Dispose()
{
for (int i = this.pdfDocumentStreams.Count - 1; i >= 0; i--)
{
MemoryStream stream = this.pdfDocumentStreams[i];
this.pdfDocumentStreams.RemoveAt(i);
stream.Dispose();
}
}
#endregion
}
私の「ラッパー」への呼び出しコードは次のようになります。
byte[] manipulatedResult = null;
using (PdfDocumentManipulator manipulator = new PdfDocumentManipulator())
{
manipulator.AddFileToManipulate(file1bytes);
manipulator.AddFileToManipulate(file2bytes);
manipulatedResult = manipulator.Manipulate();
}
上記に関するいくつかの質問:
- メソッドの
using
句はAddFileToManipulate()
冗長で不要ですか? - オブジェクトの
Dispose()
メソッドで問題なくクリーンアップできていますか? - これは の「許容される」使用法
MemoryStream
ですか? 一度にメモリ内に非常に多くのファイルがあるとは思っていません...おそらく合計1〜10のPDFページで、各ページは約200KBです。ASP.NET サイトをサポートするサーバー上で実行するように設計されたアプリ。 - コメント/提案はありますか?
コードレビューをありがとう:)