lundi 3 juin 2019

What is the most modern / best designpattern for database access in .net?

At the moment I just use the asp.net DI to register my DBContext from the WebAPI, then I try to categorisize my database acces (like Users / Orders etc.) and just write them down like this:

 public class UserActivityRepo
{
    UserRepo _userRepo;
    IntranetContext _context;

    public UserActivityRepo(IntranetContext context, UserRepo userRepo)
    {
        _userRepo = userRepo;
        _context = context;
    }

    /// <summary>
    /// Log a user activity
    /// </summary>
    /// <param name="username">The ad username</param>
    /// <param name="graphQlQuery">The graphQLQuery to log</param>
    /// <returns>async Task</returns>
    public async Task Log(string username, string graphQlQuery)
    {
        var user = await _userRepo.GetUser(username);

        _context.UserActivities.Add(new UserActivity()
        {
            UserId = user.UserId,
            Timestamp = DateTime.Now,
            GraphQlquery = graphQlQuery
        });

        _context.SaveChanges();

    }
}

I feel like this will lead to very overfilled classes and not clean code.

What is the best / most modern approach for a dataccess layer in terms of design pattern?

Aucun commentaire:

Enregistrer un commentaire