[英]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我是 C# 和 asp.net 核心的新手,我正在尝试创建一个简单的存储库模式并传递一个
string
值,但出现错误
An unhandled exception occurred while processing the request.
处理请求时发生未处理的异常。 InvalidOperationException: Unable to resolve service for type 'HomepageRepository' while attempting to activate 'WebApplication.Controllers.HomeController'.
InvalidOperationException:尝试激活“WebApplication.Controllers.HomeController”时无法解析“HomepageRepository”类型的服务。 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 HomePageRepository
:我想在这里传递connectionString
的值
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 .我再次对 C# 很陌生,上面的这段代码只给出了一个运行时错误。 All I am trying to do is pass the
connectionString
value in DBConnection
to HomePageRepository
then pass it into a controller in HomeController
.我想要做的就是将
DBConnection
的connectionString
值传递给HomePageRepository
然后将其传递给HomeController
的控制器。 Any suggestions would be great.任何建议都会很棒。
I if you want pass connection string to connectDB method please refere below code sample.如果您想将连接字符串传递给 connectDB 方法,请参考下面的代码示例。 but better option is to wrapper DB with Interface and initialized with DI
但更好的选择是用接口包装 DB 并用 DI 初始化
You don't need DB extension method modify Home Repository as below您不需要 DB 扩展方法修改 Home Repository 如下
public HomepageRepository(string dbconnectionString)
{
connectionString = dbconnectionString;
}
Then add Below entry to Startup Class's ConfigureServices method然后将下面的条目添加到启动类的 ConfigureServices 方法中
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:
以下是来自 MusicStore 示例应用程序的示例:
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完整示例,在这里: https : //github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Startup.cs#L80
Edit:编辑:
As DavidG pointed out, I missed part of the question.正如 DavidG 指出的那样,我错过了部分问题。
You can't register plain strings with the out-of-the-box DI container.您无法使用开箱即用的 DI 容器注册纯字符串。 However, you can register a type with a single method that provides that string.
但是,您可以使用提供该字符串的单个方法注册类型。 Something like a
ConnectionStringProvider
类似于
ConnectionStringProvider
东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.