C
C#ā€¢2y ago
antimatter8189

ā” How does one hold a value in a private field in a controller?

So basically controller gets created for every request right? Problem is i have this field:
private Lesson _CurrentUserLesson
private Lesson _CurrentUserLesson
And i set it once the user creates a new lesson. However if you call another endpoint that uses that variable it becomes null, static wont do either as i read. So how do you approach this?
58 Replies
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Web servers are generally stateless You'd normally pass a lessonId or such parameter with subsequent requests And then load the lesson to do whatever it is you need to do with it
antimatter8189
antimatter8189OPā€¢2y ago
yeah but wont i have to make a DB call each time?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Yeah (unless you cache it)
antimatter8189
antimatter8189OPā€¢2y ago
I see
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
That's completely normal, though
antimatter8189
antimatter8189OPā€¢2y ago
So basically only services and managers go into the Di and into the service?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
You've lost me there I'm afraid
antimatter8189
antimatter8189OPā€¢2y ago
and a few consts well
private readonly IGuidGenerator _guiGenerator;

//Services And Managers - Utilizing Dependency Injection.
private readonly AnswerComparisonManager _answerComparisonManager;
private readonly AzureBlobStorageManager _azureBlobStorageManager;
private readonly GoogleApiManager _googleApiManager;
private readonly ILessonRepository _lessonRepository;
private readonly OpenAiExtraction _openAiExtraction;

//Private Members
private Lesson _currentLesson;
private LessonPhase _currentPhase;
private LessonState _lessonState;
private MySupportedLanguageCodes _dstLanguageCode;
private MySupportedLanguageCodes _srcLanguageCode;
private readonly BotSpeakSentences _botSpeakSentences = new();
private List<Lesson> _userLessons;

private const int _tryAgainAnswerAttemptsThreshold = 2;
private const int _maxNumberOfAnswerAttempts = 5;
private const int _numberOfPhasesUsedForExplaining = 5;
private const int _maxNumberOfExplanationRequestsPerPhase = 3;
private readonly IGuidGenerator _guiGenerator;

//Services And Managers - Utilizing Dependency Injection.
private readonly AnswerComparisonManager _answerComparisonManager;
private readonly AzureBlobStorageManager _azureBlobStorageManager;
private readonly GoogleApiManager _googleApiManager;
private readonly ILessonRepository _lessonRepository;
private readonly OpenAiExtraction _openAiExtraction;

//Private Members
private Lesson _currentLesson;
private LessonPhase _currentPhase;
private LessonState _lessonState;
private MySupportedLanguageCodes _dstLanguageCode;
private MySupportedLanguageCodes _srcLanguageCode;
private readonly BotSpeakSentences _botSpeakSentences = new();
private List<Lesson> _userLessons;

private const int _tryAgainAnswerAttemptsThreshold = 2;
private const int _maxNumberOfAnswerAttempts = 5;
private const int _numberOfPhasesUsedForExplaining = 5;
private const int _maxNumberOfExplanationRequestsPerPhase = 3;
phaseshift
phaseshiftā€¢2y ago
consts? "in the DI" ?
antimatter8189
antimatter8189OPā€¢2y ago
this is what im talking about
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
So you've got a controller with all of those fields?
antimatter8189
antimatter8189OPā€¢2y ago
Its a service
phaseshift
phaseshiftā€¢2y ago
thats way way way too much if thats a controller
antimatter8189
antimatter8189OPā€¢2y ago
nah thats the LEssonAppSErvice so basically all the private members gotta go?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
For the most part, yeah
antimatter8189
antimatter8189OPā€¢2y ago
since i cant actually save em between requests unless they static
phaseshift
phaseshiftā€¢2y ago
yes. and please dont put static data on a controller šŸ˜…
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
At what kind of level are you expecting this 'service' to live? At the API level? At a lower domain level?
antimatter8189
antimatter8189OPā€¢2y ago
yeah its a work in progress unsure what you mean You mean the lifetime of the service? IDK? basically its an app like whatsapp but voice based so a user logs in and stats a lesson creates new ones on the fly etc
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
So is this a "domain" service as in any interface to your domain (a web API, a GUI, etc) can interact with it? Or is this service specific to your API?
antimatter8189
antimatter8189OPā€¢2y ago
Its not a domain service no, and there is no real controller really
antimatter8189
antimatter8189OPā€¢2y ago
API/Auto API Controllers | Documentation Center | ABP.IO
Once you create an application service, you generally want to create an API controller to expose this service as an HTTP (REST) API endpoint. A typical API controller does nothing but redirects method
antimatter8189
antimatter8189OPā€¢2y ago
šŸ˜„ it exposes the service as a controller
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Weird as hell but ok šŸ™‚ Remove everything that's a field that realistically depends on the request e.g. _currentLesson Depend on the classes required to implement whatever functionality it is And everything else should be a parameter coming from each request (However that's wired up using whatever ABP is) Whether as a parameter from the request itself or from a property of the session (e.g. user id)
antimatter8189
antimatter8189OPā€¢2y ago
Ok thats good because i do have acess to the currentUser class I was just tripping about all the DB calls and the cost associated with getting that big ass json each time because lesson is an aggregateroot it pulls all its realted data Phases answers questions etc
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
The cost of a database call generally pales in comparison to the HTTP roundtrip for the request anyhow
antimatter8189
antimatter8189OPā€¢2y ago
How many years you work with .net?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Err, 8?
antimatter8189
antimatter8189OPā€¢2y ago
oh thats nice
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
It's not, it means I'm getting old šŸ™‚
antimatter8189
antimatter8189OPā€¢2y ago
.net getting younger each year tho the amount of features šŸ˜„ another thing
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
I'm waiting for proper ADTs to come across from F# to C# then I'll be happy.
antimatter8189
antimatter8189OPā€¢2y ago
If i create a singelton with DI then does it live basically from the moment i start the server?
phaseshift
phaseshiftā€¢2y ago
no
antimatter8189
antimatter8189OPā€¢2y ago
and is the same for all requests till server shuts down
phaseshift
phaseshiftā€¢2y ago
when its first resolved
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Depends on how you register it
antimatter8189
antimatter8189OPā€¢2y ago
context.Services.AddSingleton<OpenAiManager>();
context.Services.AddSingleton<OpenAiManager>();
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Then like phaseshift says, it's created the first time it's resolved And then yes, the same instance will always be returned when it's resolved
antimatter8189
antimatter8189OPā€¢2y ago
Gotcha will have to change that then, OpenAi hallucinations are real
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
There's also an overload to register an instance If you really wanted to control the instantiation
antimatter8189
antimatter8189OPā€¢2y ago
with an interface you mean?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
var foo = new OpenAiManager();
services.AddSingleton(foo);
var foo = new OpenAiManager();
services.AddSingleton(foo);
antimatter8189
antimatter8189OPā€¢2y ago
oh yeah i dont need that lol abp got a preconfigure methode gets called first thing So it basically registers it as soon as the server starts
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Registers yes, instantiates when it is first resolved Generally you can treat the two as the same
antimatter8189
antimatter8189OPā€¢2y ago
Oh yeah youre correct
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
But the actual OpenAiManager constructor won't be called until something asks the service collection for one
antimatter8189
antimatter8189OPā€¢2y ago
yup makes sense
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Sounds like you've got it nailed
antimatter8189
antimatter8189OPā€¢2y ago
So basically
public async Task<BookCacheItem> GetAsync(Guid bookId)
{
return await _cache.GetOrAddAsync(
bookId.ToString(), //Cache key
async () => await GetBookFromDatabaseAsync(bookId),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
}
);
}
public async Task<BookCacheItem> GetAsync(Guid bookId)
{
return await _cache.GetOrAddAsync(
bookId.ToString(), //Cache key
async () => await GetBookFromDatabaseAsync(bookId),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
}
);
}
For each and every method? you always cache as much as you can i guess What dont you cache lets put it this way And thx alot šŸ˜„
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
Caching is an extremely nuanced topic What to cache, how long to cache it etc completely depends on the data You'd generally use a cache to implement the same interface as the thing that retrieves the data, though E.g. IGetBookFromDatabase would be the interface, GetBookFromSqlDatabase physically goes to the DB, CachedGetBookFromDatabase takes a cache and an IGetBookFromDatabase So the caching is transparent to the consumer The service doesn't care whether it comes from a cache or not, it just asks for a book
antimatter8189
antimatter8189OPā€¢2y ago
Yeah makes perfect sense thx again bud, have a great evening
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
You too, GL with your code Also if your cache is string keyed, make sure you identify it properly e.g. you don't want to store things with just their ids in case you store book id 1234 and then accidentally retrieve that book when trying to get user id 1234 šŸ™‚
antimatter8189
antimatter8189OPā€¢2y ago
yeah im using Guids everywhere to prevent such things You pro in efcore i guess?
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
I don't use EF at all I'm afraid
antimatter8189
antimatter8189OPā€¢2y ago
oh snap well a question for another time then thx alot šŸ˜„
SUPER MEGA T REX
SUPER MEGA T REXā€¢2y ago
No worries
Accord
Accordā€¢2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?