Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How and where to make classes for calculations and services?

$
0
0

In order for my controllers not to grow too lage, and for best practice, I need to make some folders with classes that perform operations on certain objects and types.

So I need to calculate some dates on an entity(a list of meetings) before I return it to the view. I make a class called "Calculations" in a folder which I then call from the controller.

1) Should I use dependency injection for this only if i have other controllers that also need to use this calculations class? As of now I've made a class with static methods, but i'm not sure what that even implies. Should I remove the static declaration? 

2) if I use dependency injection, should I use Singleton? I sometimes need to call it's methods twice from the same controller method, wouldn't that break the singleton philosophy(use once)? I probably don't understand the dependenc injection lifetimes ;P

3) Would this calculations class be defined as a service?

4) Tracking errors: How can I deliver an exception message(or any error message) that happens in the Calculations class to the controller? I can only return the type that is set. so I then have to make checks in the controller if the return is the type or null(or some value if it's immutable). That's not ideal. I want to convey information to the controller so the user can see it in the View. Sometimes I can return a string with that message along with the type. But I can't do that if the return type is just a List for example. This annoys me greatly, haha. 

Damn, my mediocre knowledge!

Any feedback Laughing


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>