[英]Refactor methods, asp.net mvc4
I have two likely identical methods, like this:我有两种可能相同的方法,如下所示:
[HttpGet]
public ActionResult CreateDirectory(string designId, CreateDirectoryModel model)
{
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(model.DesignId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
model.DesignId = designId;
model.Directories = new List<string>();
model.Directories.Add("/");
model.Directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
for (int i = 1; i < model.Directories.Count; i++) {
model.Directories[i] = model.Directories[i].Substring(path.Length).Replace('\\', '/');
}
model.Directories.Sort();
return View(model);
}
and this method:这个方法:
public void GetUploadFileFolders(UploadViewModel model, string designId)
{
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
model.DesignId = designId;
model.Directories = new List<string>();
model.Directories.Add("/");
model.Directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
for (int i = 1; i < model.Directories.Count; i++) {
model.Directories[i] = model.Directories[i].Substring(path.Length).Replace('\\', '/');
}
model.Directories.Sort();
}
But is it possible to combine this two methods to one method?但是有可能将这两种方法结合为一种方法吗?
But how to combine this two methods?但是如何将这两种方法结合起来呢?
Thank you谢谢
But how to change this:但如何改变这一点:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadFile(UploadViewModel uploadViewModel)
{
CreateDirectoryModel model = new CreateDirectoryModel();
try {
GetUploadFileFolders(model, uploadViewModel.DesignId );
//GetUploadFileFolders(uploadViewModel, uploadViewModel.DesignId);
string validationError = null;
bool fileUploaded = (uploadViewModel.UploadData != null) && (uploadViewModel.UploadData.ContentLength > 0) && !string.IsNullOrEmpty(uploadViewModel.UploadData.FileName);
if (ModelState.IsValid && fileUploaded) {
var extension = Path.GetExtension(uploadViewModel.UploadData.FileName).TrimStart('.');
if (!Seneca.SfsLib.FileSystemHelper.UploadOptInExtensions.Contains(extension)) {
ViewBag.Message = Resources.Entity.DesignTemplate.NotAllowedExtension;
return View(uploadViewModel);
}
var fileName = Path.GetFileName(uploadViewModel.UploadData.FileName);
TemplateLibraryEntry entry = GetTemplateLibraryEntry(uploadViewModel.DesignId, customerSchema);
string folder = uploadViewModel.Directories[uploadViewModel.SelectedFolderId];
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath, folder.Replace('/', '\\').Trim('\\'), fileName);
if (!System.IO.File.Exists(path)) {
uploadViewModel.UploadData.SaveAs(path);
return RedirectToAction(uploadViewModel.DesignId, "DesignTemplate/File");
}
validationError = Resources.Entity.DesignTemplate.FileAlreadyExists;
}
if (!fileUploaded)
validationError = Resources.Entity.DesignTemplate.FileNotSelected;
GetUploadFileFolders(model, uploadViewModel.DesignId);
ViewBag.Message = validationError;
return View(uploadViewModel);
}
catch (Exception ex) {
Logger.Current.LogMessage(LogType.Warning, StateHelper.GetSchema(), LogCategory.Sfs, "Unable to upload file: {0}", ex.ToString());
AddDelayedNotification(Resources.Entity.DesignTemplate.FileCouldNotBeUploaded, Notification.NotificationType.Error);
}
return View(uploadViewModel);
}
You can use an interface (or a base class) that defines the parts you need for both classes:您可以使用接口(或基类)来定义两个类所需的部分:
public interface IMyModel
{
string DesignId { get; set; }
List<string> Directories { get; set; }
}
Your classes (or at least CreateDirectoryModel
) must then implement the interface:您的类(或至少
CreateDirectoryModel
)必须实现接口:
public class CreateDirectoryModel : IMyModel
{
// ...
}
In the second method you change the parameter type to IMyModel
like this:在第二种方法中,您将参数类型更改为
IMyModel
如下所示:
public void GetUploadFileFolders(IMyModel model, string designId)
{
// ...
}
Then you can call the second method from your first method.然后你可以从你的第一个方法调用第二个方法。
[HttpGet]
public ActionResult CreateDirectory(string designId, CreateDirectoryModel model)
{
GetUploadFileFolders(model, designId);
return View(model);
}
You can create a property in UploadViewModel as what i see all properties are same in both :您可以在 UploadViewModel 中创建一个属性,因为我看到的所有属性都相同:
public class UploadViewModel
{
CreateDirectoryModel createModel {get;set;}
}
public void GetUploadFileFolders(CreateDirectoryModel model, string designId)
{
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
model.DesignId = designId;
model.Directories = new List<string>();
model.Directories.Add("/");
model.Directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
for (int i = 1; i < model.Directories.Count; i++)
{
model.Directories[i] = model.Directories[i].Substring(path.Length).Replace('\\', '/');
}
model.Directories.Sort();
}
and in your action:在你的行动中:
[HttpGet]
public ActionResult CreateDirectory(string designId, CreateDirectoryModel model)
{
GetUploadFileFolders(model,designId)
return View(model);
}
and when using it for UploadViewModel you can now do:将它用于 UploadViewModel 时,您现在可以执行以下操作:
GetUploadFileFolders(model.creatModel,designId);
using convention based mapper Automapper you can combine them as使用基于约定的映射器 Automapper,您可以将它们组合为
[HttpGet]
public ActionResult CreateDirectory(string designId, CreateDirectoryModel model)
{
string customerSchema = SfsHelpers.StateHelper.GetSchema();
TemplateLibraryEntry entry = GetTemplateLibraryEntry(model.DesignId, customerSchema);
var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);
model.DesignId = designId;
model.Directories = new List<string>();
model.Directories.Add("/");
model.Directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
for (int i = 1; i < model.Directories.Count; i++) {
model.Directories[i] = model.Directories[i].Substring(path.Length).Replace('\\', '/');
}
model.Directories.Sort();
//
Mapper.CreateMap<CreateDirectoryModel,UploadViewModel>();
var uploadmodel=Mapper.Map<UploadViewModel>(model);
uploadmodel.Directories.Sort();
//
return View(model);
}
however i suggest you that if these two classes are not specialized ,use one of them thereby reducing redundency但是我建议您,如果这两个类不是专门的,请使用其中一个以减少冗余
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.