'Question regarding saving entity to database

Lets say I have a method like:

 public string SetSessionData(dynamic data, string token)
 {
        var request = (data as JObject).ToObject<SetRunSessionDataRequest>();

        var session = runSessionRepository.GetRunSessionWithToken(token);

        if (!string.IsNullOrEmpty(request.Id))
            session.Id = request.Id;

And then I want to save the changes in the repository class, how is that done? will this work?

public string SetSessionData(dynamic data, string token)
{
     var request = (data as JObject).ToObject<SetRunSessionDataRequest>();
    
     var session = runSessionRepository.GetRunSessionWithToken(token);
    
     if (!string.IsNullOrEmpty(request.Id))
        session.Id = request.Id;
            
      runSessionRepository.saveSession(session);

Where the saveSession method in the repository class looks like this:

public void saveSession(session){ 
Session _session = session;
db.saveChanges();
}

So what I'm really asking is how do make the save in the repository class as I don't have a DB instance in the service class and do I even need to send the session variable? It feels pointless but if I don't how does it know what to save? Will it even save anything?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source