mercredi 6 février 2019

Unit of work repository pattern for static class

Task assigned to me is to refactor a code but should't modify static access modifier of class. I am trying to implement service layer , unit of work , repository pattern . static repository code is below , how can i implement unit of work and repository pattern for a static class? i like to implement a solution applying solid principles and unit testable application.

static class

 using System;
  using System.Data.SqlClient;

  namespace Contoso
  {
    public static class UsersRepository
    {
        private static string ConnectionString = @"Data Source=(local);    Database=Users;User Id=sa;Password=password;";

        public static User Load(int userId)
        {
            User user = new User();

            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT * FROM Users WHERE UserId = " + userId,
                connection);
            var reader = command.ExecuteReader();

            while (reader.Read())
            {
                user.Name = reader["Name"].ToString();
                user.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
                user.Country = reader["Country"].ToString();
            }

            connection.Close();

            return user;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire