I have a minimal API in .NET Core 6.0 using ADO.NET in a repository pattern

352 Views Asked by At

I'm utilizing a Minimal API with a Service which calls a repository in .NET Core 6.0. I want to be able to pass a connection string to my repository.

Here's the tree of my project:

enter image description here

The idea is the program has the endpoints which call the service which then calls the repository.

I'm trying to get the connection string from the repository.

In program.cs, I'm using a builder as follows:

var builder = WebApplication.CreateBuilder(args);
...
var app = builder.Build();

Methods that look like this:

app.MapPost("/create",
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = 
"Administrator")]
(Movie movie, IMovieService service) => Create(movie, service))
.Accepts<Movie>("application/json")
.Produces<Movie>(statusCode: 200, contentType: "application/json");

And methods being used by these functions that look like this:

IResult Create(Movie movie, IMovieService service)
{
    var result = service.Create(movie);
    return Results.Ok(result);
}

Then there's some methods on the service which look like this:

namespace MinimalJwt.Services
{
    public class MovieService : IMovieService
    {
        private readonly MovieRepository movieRepository = new MovieRepository();

        public Movie Create(Movie movie)
        {
            movie = movieRepository.CreateNewMovie(movie);
            return movie;
        }
    }
}

And finally The way I'm trying to make my repository which looks like this right now

namespace MinimalJwt.Repositories
{
    public class MovieRepository : IMovie
    {
        public IConfiguration _configuration;
        
        public MovieRepository(IConfiguration config)
        {
            this._configuration = config;
        }

        public IList<Movie> GetMovies()
        {
            List<Movie> movies = new List<Movie>();

            using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("MoviesContextDb")))
            {
                SqlCommand cmd = new SqlCommand("spGetAllMovies", con);
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    var movie = new Movie()
                    {
                        Id = Convert.ToInt32(rdr["Id"]),
                        Title = rdr["Title"].ToString(),
                        Description = rdr["Description"].ToString(),
                        Rating = Convert.ToDouble(rdr["Rating"])
                    };

                    movies.Add(movie);
                }

                return (movies);
            }
      }
}
  

The idea is for the repository to be able to use the connection string inside of appsettings.json.

Any help is much appreciated.

Thanks in advance.

1

There are 1 best solutions below

0
On

Your appsettings.json files should be look like this. Then your code should work.

{ 
  "ConnectionStrings": {
    "MoviesContextDb": "Connection String details"
  }
   //.....other configurations.........
}