简体   繁体   中英

I want to store class names in my configuration file (ASP.NET / C#)

I'm developing a service that'll be provide messages to a calling UI based on conditions that exist per message.

I have a json file that stores all the different possible messages the UI can receive and I expect that file to grow throughout the application's lifetime (ie I plan to add new message configurations)

The problem is each message has a different condition on whether it should be included in the response and these conditions rely on a downstream call being made to some DB or other service and then logic that processes those downstream calls to resolve whether a condition is true or false.

I want to have each message in my configuration file to have a value that resolves to a class in my code so that when the endpoint is called, I can just call a "resolve" method passing that "message configuration" ie the resolve method calls the class associated to that message (and it's condition resolution method) and makes all the necessary calls to downstream services.

Is there a way to have each of my messages have classes associated to them in my json configuration file? Am I missing a much easier way to implement this?

As I can see, when you need to add a new message to the application, you have to add it to the configuration file and additionally define a separate class with conditions. Right? If your answer is yes, message file (json) won't save you from builds and deployments over and over again. You can move your messages into classes that implement the interface. Then simply register them as a collection and process messages through that collection. The message and conditions will be resolved without any "magic" tricks. Of course, for a new message type, you have to declare a new class with the same interface, new message, and the appropriate logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM