开发者

In DDD where to keep custom exceptions (application exceptions)? In Infrastructure layer?

I'm building a app with following architecture:

UI - Application - Domain - Infrastructure

I have a Application Layer that need use custom exceptions. Where I keep these custom exceptions? In Infrastructure layer? The problem is my Application Layer don't have reference to Infrastructure layer.

What is the correct way?

Update:

Here's my code that throw a exception in 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)));
    }
}

Even ImageSettings is a ConfigurationSection is in my Application Layer because it uses it. I don't see other way I can transfer my ImageSettings (which should stay in Infrastrucuture Layer) to Infrastructure Layer, someone can help?

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["allowedExt开发者_如何学Pythonensions"] = value; }
    }

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


This is most likely related to your previous question. Exceptions are part of the contract that is defined by application layer and is implemented by infrastructure (DIP and Onion architecture). They should be defined in Appliction terms and handled by Application, but thrown from Infrastructure. For example, in your Application code:

public class NotificationException : Exception {...}

public interface ICanNotifyUserOfSuccessfullRegistration {
    /// <summary>
    /// ...
    /// </summary>
    /// <exception cref="NotificationException"></exception>
    void Notify();
}

And in Infrastructure:

public class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration {
    public void Notify() {
        try {
            // try sending SMS here
        } catch(SmsRelatedException smsException) {
            throw new NotificationException(
                            "Unable to send SMS notification.", smsException);
        }
    }
}


In DDD where to keep custom exceptions (application exceptions)? In Infrastructure layer?

  • No

Why?

  • In a Clean Architecture, a center layer never depends on the outside, always the inverse
  • "The fundamental rule is that all code can depend on layers more central, but code cannot depend on layers further out from the core. In other words, all coupling is toward the center.". Please check the article from Jeffrey Palermo.

I've written an article with Onion Architecture with a code sample. Please check this.

But basically, you should add your custom exception to your layer (Application in this case). Just create a folder "Exceptions" and then add your user-defined exception there. Please check this link for my details about how to create user-defined exceptions


Do you have a layer where cross-cutting concerns (such as logging or dependency injection) are addressed and which is referenced by all other projects in your solution? If so, this is where youd should put these custom exceptions. I guess that by "infrastructure layer" you actually mean this cross-cutting layer, but if so, it seems strange that your application layer is not referencing it.

Alternatively, you could keep these exceptions in the application layer itself, provided that these exceptions are used only by that layer and perhaps by the UI layer too.


Acaz Souza - you're incorrect in saying the Application Layer shouldnt reference the Infrastructure Layer. I suggest you read "Domain Driven Design Quickly", which is available for free from InfoQ.

Look at the diagram below, which illustrates my point.

Thanks

In DDD where to keep custom exceptions (application exceptions)? In Infrastructure layer?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜