简体   繁体   中英

asp.net core C# how can I pass value in a repository pattern

I am new to C# and asp.net core and I am trying to create a simple repository pattern and pass a string value but I'm getting the error

An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'HomepageRepository' while attempting to activate 'WebApplication.Controllers.HomeController'. GetService

This is my code

public static class DBConnection 
{

    public static string  connectDB(this string connectionString) 
    {
        return connectionString = "pass value of this string";
    }
 }

HomePageRepository : I would like to pass the value of connectionString here

public class HomepageRepository : IHomepageRepository
{
    private string connectionString;

    public HomepageRepository()
    {
        connectionString= connectionString.connectDB();
    }

    public string Streams()
    {
        return connectionString;
    }
}

Then pass this to my controller like this

namespace WebApplication.Controllers
{
    public class HomeController : Controller
    {
        public HomepageRepository _repository;

        public HomeController(HomepageRepository repository)
        {
            _repository = repository;
        }

        public string Streams()
        {
            _repository.Streams();

            return "hello";
        }
    }
}

Again I am very new to C# and this code above gives only a runtime error . All I am trying to do is pass the connectionString value in DBConnection to HomePageRepository then pass it into a controller in HomeController . Any suggestions would be great.

I if you want pass connection string to connectDB method please refere below code sample. but better option is to wrapper DB with Interface and initialized with DI

You don't need DB extension method modify Home Repository as below

public HomepageRepository(string dbconnectionString)
    {
        connectionString = dbconnectionString;
    }

Then add Below entry to Startup Class's ConfigureServices method

 services.AddSingleton<IHomepageRepository>(new HomepageRepository ("dbstring"));

You have to register the service in the startup class. Here's an example from the MusicStore sample app:

namespace MusicStore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddSingleton<ISystemClock, SystemClock>();
            ...
        }
    }
}

Full example, here: https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Startup.cs#L80


Edit:

As DavidG pointed out, I missed part of the question.

You can't register plain strings with the out-of-the-box DI container. However, you can register a type with a single method that provides that string. Something like a ConnectionStringProvider

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM