图像处理应该放在 DDD 应用程序的什么位置?

Where should image processing reside in DDD app?

我正在使用 image sharper 来处理图像,然后保存在数据库中,目前我在服务层的 ImageService 中有逻辑:

private async Task Processor(ImageInputModel image)
    {
        using var imageResult = await Image.LoadAsync(image.Content);

        var original = await SaveImage(imageResult, imageResult.Width);
        var thumbnail = await SaveImage(imageResult, ThumbnailWidth);
        var articlePreview = await SaveImage(imageResult, ArticlePreviewWidth);
        var articleFullscreen = await SaveImage(imageResult, ArticleFullscreenWidth);

        var dbContext = _serviceFactory
        .CreateScope()
        .ServiceProvider
        .GetRequiredService<ApplicationDbContext>();

        var imageModel = new ImageData
        {
            OriginalFileName = image.Name,
            OriginalType = image.Type,
            OriginalContent = original,
            ThumbnailContent = thumbnail,
            ArticlePreviewContent = articlePreview,
            ArticleFullscreenContent = articleFullscreen,
            ArticleId = image.ArticleId,
            ProjectDataId = image.ProjectId
        };

        await dbContext.Images.AddAsync(imageModel);
        await dbContext.SaveChangesAsync();
    }

    private async Task<Stream> GetImageData(string id, string size)
    {
        var database = _dbContext.Database;

        var dbConnection = (SqlConnection)database.GetDbConnection();

        var command = new SqlCommand(
            $"SELECT {size}Content FROM Images WHERE Id = @id",
            dbConnection
            );

        command.Parameters.Add(new SqlParameter("@id", id));

        await dbConnection.OpenAsync();

        var reader = await command.ExecuteReaderAsync();

        Stream result = null;

        if (reader.HasRows)
        {
            while (await reader.ReadAsync())
            {
                result = reader.GetStream(0);
            }
        }

        await reader.CloseAsync();
        await dbConnection.CloseAsync();

        return result;
    }

    private static async Task<byte[]> SaveImage(Image image, int resizeWidth)
    {
        var width = image.Width;
        var height = image.Height;

        if (width > resizeWidth)
        {
            height = (int)((double)resizeWidth / width * height);
            width = resizeWidth;
        }

        image
            .Mutate(i => i
                .Resize(new Size(width, height)));

        image.Metadata.ExifProfile = null;

        var memoryStream = new MemoryStream();


        await image.SaveAsJpegAsync(memoryStream, new JpegEncoder
        {
            Quality = 75
        });

        return memoryStream.ToArray();
    }

现在假设我要将这个项目迁移到 DDD 设计或重新开始,我应该把这个逻辑放在哪里?我想也许我有一个领域模型图像并将这个逻辑放在那里,但后来我认为它没有意义,因为图像是用于许多其他领域模型的,它不是领域专家会问的单独实体。所以也许我输入了值对象或域服务但我不知道?有什么想法吗?

我会(一如既往地与架构 - 它基于意见)说图像不是实体,而是在这种情况下的价值对象,
因此它属于域。

处理是对图片的服务,所以基础设施-服务以图片为参数。

包含图像的东西的聚合根可以用它做一些事情,使用域服务或服务注入聚合(域中的接口)。

或者,您可以在包含图像对象的聚合中引发域事件,此事件的处理程序将进行处理。