In DDD, where to store custom exceptions (application exceptions)? At the infrastructure level?

I am creating an application with the following architecture:

UI - Application - Domain - Infrastructure

I have an Application Layer that needs custom exceptions. Where do I keep these special exceptions? At the infrastructure level? The problem is that my application layer does not have a link to the infrastructure layer.

What is the right way?

Update:

Here is my code that throws an exception in the 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 in my application level because it uses it. I see no other way to transfer my ImageSettings (which should remain in the Infrastrucuture Layer) to the infrastructure level, can someone 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["allowedExtensions"] = value; } } /// <summary> /// Tamanho das imagens /// </summary> [ConfigurationProperty("imageSize")] public ImageSizeCollection ImageSize { get { return (ImageSizeCollection)this["imageSize"]; } } } 
+6
source share
3 answers

Do you have a layer that addresses cross-cutting issues (such as logging or dependency injection) and that all other projects in your solution refer to? If so, you should introduce these custom exceptions here. I believe that by “infrastructure layer” you really mean this end-to-end layer, but if so, it seems strange that your application layer does not refer to it.

Alternatively, you can save these exceptions at the application level, provided that these exceptions are used only by this layer and, possibly, the user interface layer.

0
source

This is most likely related to your previous question . Exceptions are part of the contract, which is determined by the application level and implemented by the infrastructure ( DIP and Onion architecture ). They must be defined in the Terms of Use and processed by the Application, but selected from the Infrastructure. For example, in your application code:

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

And in the 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); } } } 
0
source

Acaz Souza - You are incorrect in saying that the application layer should not refer to the infrastructure layer. I suggest you quickly read the “Domain Driven Design Quickly,” which is available for free for InfoQ.

Take a look at the diagram below, which illustrates my point.

thanks

Domain Driven Design - Layers

0
source

Source: https://habr.com/ru/post/898514/


All Articles