【问题标题】:Asp.net - adding function to classAsp.net - 向类添加功能
【发布时间】:2015-12-19 02:15:57
【问题描述】:

我有一个代码优先的实体框架类,如下所示:

public class Image
{
    public int ImageID { get; set; }
    public string DataType { get; set; }
    public int Size { get; set; }
    public byte[] Content { get; set; }
}

我在控制器中编写了几行代码,该控制器接收 IFormFile,然后提取文件数据并将其读入 Image:

var content = formFile.OpenReadStream();
byte[] buf = new byte[content.Length];
content.Read(buf, 0, buf.Length);
var newImg = new Image();
newImg.Content = buf;
newImg.DataType = formFile.ContentType;

由于在上传图像的任何地方都会重复使用此代码,因此我想以某种方式将其封装为“SaveUpload”函数。它将用于整个解决方案的各种控制器中。

将其作为 {set;} 唯一的非映射属性添加到 Image 类/表的最佳方法是什么?

或者我是否以某种方式封装或继承了这个属性(抱歉,我还在学习 OOP 术语)?

【问题讨论】:

    标签: c# asp.net oop


    【解决方案1】:

    您可能需要考虑创建一个 UploadService 类来封装此逻辑。控制器和 Image 类都不应该真正了解这些细节,通过将其抽象为服务,您可能会发现代码更具可读性和更易于维护。

    上传服务:

    public class UploadService
    {
        public void SaveUpload(IFormFile formFile)
        {
            var content = formFile.OpenReadStream();
            byte[] buf = new byte[content.Length];
            content.Read(buf, 0, buf.Length);
            var newImg = new Image();
            newImg.Content = buf;
            newImg.DataType = formFile.ContentType;
    
            //insert newImg...
        }
    }
    

    我什至会考虑更进一步,通过使用 Unity 或 Ninject 等依赖注入框架将此服务注入您的控制器,这将允许您隔离服务以进行单元测试。

    【讨论】:

      【解决方案2】:

      你可以做类似的事情

      public class Image
      {
           public int ImageID { get; set; }
           public string DataType { get; set; }
           public int Size { get; set; }
           public byte[] Content { get; set; 
      
           public static Image SaveUpload(IFormFile formFile) {
              var newImage = new Image();
              var content = formFile.OpenReadStream();
              byte[] buf = new byte[content.Length];
              content.Read(buf, 0, buf.Length);
              var newImg = new Image();
              newImg.Content = buf;
              newImg.DataType = formFile.ContentType;
              return newImage;
          }
      }
      

      保存
       var image = Image.SaveUpload(formFile);
      

      【讨论】:

        【解决方案3】:

        在你的班级里做下一步:

        public static void YourMethod(/*parameters, if you need them*/){
            var content = formFile.OpenReadStream();
            byte[] buf = new byte[content.Length];
            content.Read(buf, 0, buf.Length);
            var newImg = new Image();
            newImg.Content = buf;
            newImg.DataType = formFile.ContentType;
        }
        

        【讨论】:

        • 谢谢,我不确定它是否可以这么简单。这是不好的做法,因为这意味着这个逻辑在定义数据库模式的同一个类中?
        • @DaveDickson 否。您可以为此函数创建另一个类。例如,您可能有一个类 UTILS.cs,您将在其中拥有此功能。如果您需要最高的安全性,您也可以创建 dll 库。这取决于你。
        【解决方案4】:

        可以添加普通的静态函数,但是为了良好的实践:POCO 类不应该有任何逻辑期望字段。更好的方法是创建其他类,例如:UploadService 哪个方法:Save(Image image)您的选择。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-15
          • 1970-01-01
          • 2010-09-07
          • 2017-12-06
          • 1970-01-01
          • 2013-07-01
          • 2012-07-22
          • 1970-01-01
          相关资源
          最近更新 更多