在DDD中保留自定义异常(应用程序异常)?在基础架构层?

Aca*_*uza 9 .net domain-driven-design

我正在构建一个具有以下架构的应用程序:

UI - 应用程序 - 域 - 基础架构

我有一个需要使用自定义异常的应用层.我保留这些自定义异常的地方?在基础架构层?问题是我的Application Layer没有引用Infrastructure层.

什么是正确的方法?

更新:

这是我在Application Layer中引发异常的代码:

public void InsertNewImage(ImagemDTO imagemDTO)
{
    if (isValidContentType(imagemDTO.ImageStreamContentType))
    {
        string nameOfFile = String.Format("{0}{1}", Guid.NewGuid().ToString(), ContentTypeHelper.GetExtension(imagemDTO.ImageStreamContentType));

        string path = String.Format("{0}{1}", ImageSettings.PathToSave, nameOfFile);

        _fileService.SaveFile(imagemDTO.ImageStream, path);

        Imagem imagem = new Imagem()
                            {
                                Titulo = imagemDTO.Titulo,
                                Descricao = imagemDTO.Descricao,
                                NomeArquivo = nameOfFile
                            };

        _imagemRepository.Add(imagem);

        _dbContext.SaveChanges();
    } else
    {
        throw new WrongFileTypeException(String.Format("{0} is not allowed.", ContentTypeHelper.GetExtension(imagemDTO.ImageStreamContentType)));
    }
}
Run Code Online (Sandbox Code Playgroud)

甚至ImageSettings也是我的应用层中的ConfigurationSection,因为它使用它.我没有看到其他方式可以将我的ImageSettings(应该保留在基础设施层)转移到基础架构层,有人可以提供帮助吗?

public class ImageSettings : ConfigurationSection
{
    /// <summary>
    /// Caminha onde será salvo as imagens
    /// </summary>
    [ConfigurationProperty("pathToSave", IsRequired = true)]
    public string PathToSave
    {
        get { return (string)this["pathToSave"]; }
        set { this["pathToSave"] = value; }
    }

    /// <summary>
    /// Extensões permitidas pra upload
    /// </summary>
    [ConfigurationProperty("allowedExtensions", IsRequired = true)]
    public string AllowedExtensions
    {
        get { return (string)this["allowedExtensions"]; }
        set { this["allowedExtensions"] = value; }
    }

    /// <summary>
    /// Tamanho das imagens
    /// </summary>
    [ConfigurationProperty("imageSize")]
    public ImageSizeCollection ImageSize
    {
        get
        {
            return (ImageSizeCollection)this["imageSize"];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kon*_*man 0

您是否有一个层可以解决横切问题(例如日志记录或依赖项注入),并且解决方案中的所有其他项目都会引用该层?如果是这样,您应该在此处放置这些自定义异常。我猜想“基础设施层”实际上是指这个横切层,但如果是这样,那么您的应用程序层没有引用它似乎很奇怪。

或者,您可以将这些异常保留在应用程序层本身中,前提是这些异常仅由该层使用,也可能由 UI 层使用。