非 System.Web 替代 HttpPostedFileBase?
Non System.Web substitute for HttpPostedFileBase?
我正在尝试将一些 C#
MVC 遗留代码移动到共享 DLL 中。到目前为止一切顺利,但有人问我共享 DLL 不需要以任何方式引用 System.Web
。
来自 System.Web 的那个 DLL 中使用的唯一类型是 HttpPostedFileBase
:
public string ChangeAttachment(int userId, HttpPostedFileBase file)
{
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath))
{
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(attachmentsFolderPath, userId.ToString() + Path.GetExtension(file.FileName));
if (File.Exists(fileTarget))
{
File.Delete(fileTarget);
}
file.SaveAs(fileTarget);
return fileTarget;
}
如您所见,此处不需要 HTTP 或 Web 功能,因为仅使用其 FileName
和 SaveAs()
成员。
是否有一个替代品,我可以在 调用者 轻松地将 HttpPostedFileBase
转换为它,这样我需要作为参数传递的只是一个非网络文件?
注意:HttpPostedFileBase
直接继承自 System.Object
,而不是任何文件 class。
HttpPostedFileBase
是一个摘要 class。因此,挑战在于您实际上并不是在替换 class,而是在替换 HttpPostedFileWrapper
,这是实施。 (这不是 class 继承的东西,而是从它继承的东西。)
HttpPostedFileWrapper
依次引用其他 System.Web
classes,例如 HttpInputStream
和“HttpPostedFile”。
所以你不能替换它。也许要求您不要引用 System.Web 的意图是您要移动与 Web 功能不直接相关的遗留代码,例如业务逻辑。如果您不能将代码完全排除在外,也许您可以将它从您正在创建的新程序集中排除,然后让另一个程序集引用 System.Web
。如果他们不需要这个特定的功能,他们只引用一个程序集,但如果他们需要这个,那么他们也可以添加第二个程序集,该程序集引用 System.Web
.
如果你不想引用System.Web
并且还想使用SaveAs方法,你可以定义一个接口和一个包装器来做一个link。这不是一个非常简单的方法:
//// Second assembly (Without referencing System.Web):
// An interface to link the assemblies without referencing to System.Web
public interface IAttachmentFile {
void SaveAs(string FileName);
}
..
..
// Define ChangeAttachment method
public string ChangeAttachment(int userId, IAttachmentFile attachmentFile) {
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath)) {
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(
attachmentsFolderPath,
userId.ToString() + Path.GetExtension(file.FileName)
);
if (File.Exists(fileTarget)) {
File.Delete(fileTarget);
}
// This call leads to calling HttpPostedFileBase.SaveAs
attachmentFile.SaveAs(fileTarget);
return fileTarget;
}
//// First assembly (Referencing System.Web):
// A wrapper class around HttpPostedFileBase to implement IAttachmentFile
class AttachmentFile : IAttachmentFile {
private readonly HttpPostedFileBase httpPostedFile;
public AttachmentFile(HttpPostedFileBase httpPostedFile) {
if (httpPostedFile == null) {
throw new ArgumentNullException("httpPostedFile");
}
this.httpPostedFile = httpPostedFile;
}
// Implement IAttachmentFile interface
public SaveAs(string fileName) {
this.httpPostedFile.SaveAs(fileName);
}
}
..
..
// Create a wrapper around the HttpPostedFileBase object
var attachmentFile = new AttachmentFile(httpPostedFile);
// Call the ChangeAttachment method
userManagerObject.ChangeAttachment(userId, attachmentFile);
我正在尝试将一些 C#
MVC 遗留代码移动到共享 DLL 中。到目前为止一切顺利,但有人问我共享 DLL 不需要以任何方式引用 System.Web
。
来自 System.Web 的那个 DLL 中使用的唯一类型是 HttpPostedFileBase
:
public string ChangeAttachment(int userId, HttpPostedFileBase file)
{
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath))
{
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(attachmentsFolderPath, userId.ToString() + Path.GetExtension(file.FileName));
if (File.Exists(fileTarget))
{
File.Delete(fileTarget);
}
file.SaveAs(fileTarget);
return fileTarget;
}
如您所见,此处不需要 HTTP 或 Web 功能,因为仅使用其 FileName
和 SaveAs()
成员。
是否有一个替代品,我可以在 调用者 轻松地将 HttpPostedFileBase
转换为它,这样我需要作为参数传递的只是一个非网络文件?
注意:HttpPostedFileBase
直接继承自 System.Object
,而不是任何文件 class。
HttpPostedFileBase
是一个摘要 class。因此,挑战在于您实际上并不是在替换 class,而是在替换 HttpPostedFileWrapper
,这是实施。 (这不是 class 继承的东西,而是从它继承的东西。)
HttpPostedFileWrapper
依次引用其他 System.Web
classes,例如 HttpInputStream
和“HttpPostedFile”。
所以你不能替换它。也许要求您不要引用 System.Web 的意图是您要移动与 Web 功能不直接相关的遗留代码,例如业务逻辑。如果您不能将代码完全排除在外,也许您可以将它从您正在创建的新程序集中排除,然后让另一个程序集引用 System.Web
。如果他们不需要这个特定的功能,他们只引用一个程序集,但如果他们需要这个,那么他们也可以添加第二个程序集,该程序集引用 System.Web
.
如果你不想引用System.Web
并且还想使用SaveAs方法,你可以定义一个接口和一个包装器来做一个link。这不是一个非常简单的方法:
//// Second assembly (Without referencing System.Web):
// An interface to link the assemblies without referencing to System.Web
public interface IAttachmentFile {
void SaveAs(string FileName);
}
..
..
// Define ChangeAttachment method
public string ChangeAttachment(int userId, IAttachmentFile attachmentFile) {
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath)) {
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(
attachmentsFolderPath,
userId.ToString() + Path.GetExtension(file.FileName)
);
if (File.Exists(fileTarget)) {
File.Delete(fileTarget);
}
// This call leads to calling HttpPostedFileBase.SaveAs
attachmentFile.SaveAs(fileTarget);
return fileTarget;
}
//// First assembly (Referencing System.Web):
// A wrapper class around HttpPostedFileBase to implement IAttachmentFile
class AttachmentFile : IAttachmentFile {
private readonly HttpPostedFileBase httpPostedFile;
public AttachmentFile(HttpPostedFileBase httpPostedFile) {
if (httpPostedFile == null) {
throw new ArgumentNullException("httpPostedFile");
}
this.httpPostedFile = httpPostedFile;
}
// Implement IAttachmentFile interface
public SaveAs(string fileName) {
this.httpPostedFile.SaveAs(fileName);
}
}
..
..
// Create a wrapper around the HttpPostedFileBase object
var attachmentFile = new AttachmentFile(httpPostedFile);
// Call the ChangeAttachment method
userManagerObject.ChangeAttachment(userId, attachmentFile);