我想保存发布到ASP.NET MVC Core操作中的文章的某些文件。然后,我将它们作为IFormFile
中的HttpContext
。这些文件可以是图像,视频和文档。这些文件中的每一个都有其保存限制,例如大小或扩展名,每种类型都有其特定的限制。例如,文章的主图像不应大到1500 *900。因此,我决定将这种设计应用于他们:
我的代码:
public interface IArticleFile
{
void CheckFileRules(IFormFile formFile);
void SaveFile(IFormFile formFile, string path);
}
public interface IArticleImage : IArticleFile
{
void CheckImageRules(IFormFile formFile);
}
public interface IArticleVideo : IArticleFile
{
void CheckVideoRules(IFormFile formFile);
}
public class ArticleMainImage: IArticleImage
{
public void CheckFileRules(IFormFile formFile) {/*CHECK STH*/}
public void CheckImageRules(IFormFile formFile) {/*CHECK STH*/}
public void SaveFile(IFormFile formFile, string path) { /* Save file */ }
}
public class ArticleSummaryImage : IArticleImage
{
public void CheckFileRules(IFormFile formFile) {/*CHECK STH*/}
public void CheckImageRules(IFormFile formFile) {/*CHECK STH*/}
public void SaveFile(IFormFile formFile, string path) { /* Save file */ }
}
public class ArticleDto
{
/*some properties*/
public IFormFile MainImage { get; set; }
public IFormFile SummaryImage { get; set; }
public IFormFile ThumbnailImage { get; set; }
public List<IFormFile> Videos { get; set; }
public List<IFormFile> Documents { get; set; }
}
public class Article: IArticleService
{
public void AddArticle(ArticleDto articleDto)
{
var articleMainImage = new ArticleMainImage();
articleMainImage.CheckFileRules(articleDto.MainImage);
articleMainImage.CheckImageRules(articleDto.MainImage);
var articleSummaryImage = new ArticleMainImage();
articleSummaryImage.CheckFileRules(articleDto.MainImage);
articleSummaryImage.CheckImageRules(articleDto.MainImage);
//Do these for Thumbnail image, videos, and documents
}
}
但是,我想我应该在这里使用一种设计模式,以使它们以后易于操作,并避免在Service层中重复某些代码。我怎样才能更好地做到这一点?
答案 0 :(得分:1)
您可以通过更改代码的某些部分来使用 Command 设计模式。在这里,您可以有两种类型的命令。第一个用于验证,第二个用于文件保存。因此,通过将它们注册为命令,您可以在一处调用它们。您可以将.NET Action
类用作命令,并在CommandHandler
类中使用它:
因此,如果您有这样的命令处理程序类:
public class CommandsHandler
{
private List<Action> Commands { get; set; }
public void AddCommand(Action saver)
{
Commands.Add(saver);
}
public void ExecuteAll()
{
Commands.ForEach(c => c.Invoke());
}
}
然后更改您的文章类以获取命令处理程序。请注意,每个超类都有责任注册其验证方法,以防止在子代中重复注册:
public abstract class ArticleFile
{
protected IFormFile FileForm;
protected string FileFullPath;
public abstract void CheckFileRules();
public void SaveFile(){
protected ArticleFile(CommandsHandler validator, CommandsHandler saver, IFormFile file, string path)
{
FileForm = file;
FileFullPath = path;
validator.AddCommand(CheckFileRules);
saver.AddCommand(SaveFile);
}
}
public abstract class ArticleImage : ArticleFile
{
public abstract void CheckImageRules();
protected ArticleImage(CommandsHandler validator, CommandsHandler saver, IFormFile file, string path)
: base(validator, saver, file, path)
{
validator.AddCommand(CheckImageRules);
}
}
public class ArticleVideo : ArticleFile
{
public override void CheckFileRules() {/*CHECK STH*/ }
public void CheckVideoRules() {/*CHECK STH*/ }
public ArticleVideo(CommandsHandler validator, CommandsHandler saver, IFormFile file, string path)
: base(validator, saver, file, path)
{
validator.AddCommand(CheckVideoRules);
}
}
public class ArticleMainImage : ArticleImage
{
public ArticleMainImage(CommandsHandler validator, CommandsHandler saver, IFormFile file, string path)
: base(validator, saver, file, path)
{
validator.AddCommand(CheckImageDimensions);
}
public override void CheckFileRules() {/*CHECK STH*/}
public override void CheckImageRules() {/*CHECK STH*/}
public void CheckImageDimensions() { }
}
然后,您可以轻松创建对象并调用验证程序和保存程序:
public class Article : IArticleService
{
public void AddArticle(ArticleDto articleDto)
{
var articleValidator = new CommandsHandler();
var articleFileSaver = new CommandsHandler();
var articleMainImage = new ArticleMainImage(articleValidator, articleFileSaver, articleDto.MainImage, "");
var articleVideos = new List<ArticleVideo>();
articleDto.Videos.ForEach(v =>
articleVideos.Add(new ArticleVideo(articleValidator, articleFileSaver, v, "")));
articleValidator.ExecuteAll();
articleFileSaver.ExecuteAll();
}
}