类关联方法

时间:2013-01-16 15:37:09

标签: c# asp.net-mvc design-patterns

我是MVC的新手,我的问题是我没有以正确的方式解决这个问题,它目前的工作方式我的预期,但我不确定结构以及放置方法的位置。

我有一个类用于帮助显示视图(视图模型?),名为FolderFileList,它还包含一个名为GetFolderStructure()的函数,该函数采用路径和文件夹并循环创建所有文件和文件夹的列表,然后返回它们。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace xxx.Models
{

    public class FolderFileList
    {
        public DirectoryInfo Directory { get; set; }
        public FileInfo File { get; set; }

        public List<FileInfo> FileList { get; set; }
        public List<DirectoryInfo> FolderList { get; set; }
        public List<DirectoryInfo> RootFolderList { get; set; }
        public string FolderRoot { get; set; 
    }


    public static FolderFileList GetFolderStructure(string path, string foldername)
    {
        //initialise variables
        DirectoryInfo selecteddirectory = null;
        DirectoryInfo rootdirectory = null;
        var files = new List<FileInfo>();

        var listoffilesandfolders = new FolderFileList();
        listoffilesandfolders.FileList = new List<FileInfo>();
        listoffilesandfolders.FolderList = new List<DirectoryInfo>();
        listoffilesandfolders.RootFolderList = new List<DirectoryInfo>();
        listoffilesandfolders.FolderRoot = foldername;

        //get selected directory
        try
        {
            selecteddirectory = new DirectoryInfo(path + foldername);
            listoffilesandfolders.FolderList = selecteddirectory.GetDirectories("*", SearchOption.AllDirectories).ToList();
        }
        catch (DirectoryNotFoundException exp)
        {
            throw new Exception("Could not open the directory", exp);
        }
        catch (IOException exp)
        {
            throw new Exception("Failed to access directory", exp);
        }

        //get root directory
        try
        {
            rootdirectory = new DirectoryInfo(path);
            listoffilesandfolders.RootFolderList = rootdirectory.GetDirectories("*", SearchOption.TopDirectoryOnly).ToList();
        }
        catch (DirectoryNotFoundException exp)
        {
            throw new Exception("Could not open the directory", exp);
        }
        catch (IOException exp)
        {
            throw new Exception("Failed to access directory", exp);
        }

        //get all files and subfolder files
        try
        {
            files = selecteddirectory.GetFiles("*", SearchOption.AllDirectories).ToList();
        }
        catch (FileNotFoundException exp)
        {
            throw new Exception("Could not find file", exp);
        }
        catch (IOException exp)
        {
            throw new Exception("Failed to access fie", exp);
        }

        files = files.OrderBy(f => f.Name).ToList();

        foreach (FileInfo file in files)
        {
            listoffilesandfolders.FileList.Add(file);
        }

        return listoffilesandfolders;
    }
}

我的控制器:

    public ActionResult Folder(string foldername)
    {
        var path = Server.MapPath(@"~\");

        var folderstructure = FolderFileList.GetFolderStructure(path, foldername);


        return View(folderstructure);

    }

我的问题是:

  1. 我在哪里上课?它目前位于模型文件夹中。
  2. 该方法依赖于类,是否可以在类中使用它?我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

回答第一个问题:

就个人而言,我会创建一个名为“ViewModels”或“Classes”的文件夹。这完全取决于你。如果移动类,则应更新代码中的命名空间。

编号2:您可以在ViewModel中存放该方法。正如StewartLC下面提到的那样,视图可以调用该方法,您可能不希望这样。如果没有,我会把它放在控制器中。