我正在使用IOC编写一个简单的照片下载程序。有一个相当简单的步骤列表。获取要下载的照片列表。检查一下,检查一下,然后下载。下载可以在某些情况下通过FTP完成,在其他情况下通过http完成,在其他情况下通过Web服务完成。
我想知道的是,因为我是IOC的新手,使用IOC最佳实践/设计模式来开发10'下载线程是一个很好的模式。
我正在使用Castle Windsor但可以轻松切换到StructureMap或Spring,因为我在这个过程的早期。
编辑:要清楚,我了解如何创建一个IPhotoDownloader接口,然后是3个具体的photodownloader类,我不清楚的是如何控制线程以及如何知道何时在所有线程上完成所有操作继续。
第二次编辑以回应评论: 据我了解,直接引用IOC应用程序中的具体类是不好的,所以如果我想,例如,使用ThreadPool.QueueUserWorkItem多线程下载我的下载程序,我该怎么做。我是否只是使用容器来解析我想要在我的线程启动方法中的每个线程上的具体类?像:
void StartThead(){
PhotoRetriever retriever = container.Resolve<PhotoRetriever>();
}
答案 0 :(得分:0)
Krysztof让我开始走上正确的道路,基本上我在做这样的事情时看到了一些奇怪的行为:
_fileList = new Stack<Uri>();
for (int i = 0; i < 10; i++)
{
_fileList.Push(new Uri("http://mi.mirror.garr.it/mirrors/postfix/index.html"));
_fileList.Push(new Uri("ftp://mi.mirror.garr.it/mirrors/postfix/index.html"));
_fileList.Push(new Uri("http://www.google.com/adsense/static/it/LocalizedTerms.html"));
_fileList.Push(new Uri("http://www.cnn.com"));
}
_handles = new ManualResetEvent[ThreadCount];
_watch.Start();
// fire up threadCount threads to download files
for (int i = 0; i < ThreadCount; i++)
{
_handles[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(downloadThread), i);
}
// wait for all threads to finish
WaitHandle.WaitAll(_handles);
................
private void downloadThread(object stateInformation)
{
var retriever = _IOCContainer.Resolve<HtmlTitleRetriever>();
while(fileUri!= null) {
var fName = retriever.DownLoadFile(fileUri);
.....
我看到的行为是因为我不知道生命周期属性,默认的Singleton,实际上我需要在这个实例中使用PerThread。