简体   繁体   English

asp.net core C# 如何在存储库模式中传递值

[英]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 .我想要做的就是将DBConnectionconnectionString值传递给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.

相关问题 如何获取输入 onchange 事件以调用 c# 方法并将值传递给它 asp.net - How can i get input onchange event to call a c# method and pass a value into it asp.net 如何在C#asp.net三层体系结构的SqlCommand中传递“隐藏字段”值? - How can I pass the Hidden Field value in SqlCommand in C# asp.net Three Tier Architecture? 如何在我的 ASP.NET 核心 Web API 项目中使用工作单元模式和通用存储库模式? - How can I use unit of work pattern with generic repository pattern in my ASP.NET Core Web API project? 如何在 ASP.NET 核心中使用存储库模式进行排序? - How to sort using repository pattern in ASP.NET core? 我可以在asp.net核心上运行C#游戏吗? - Can I run a C# game on asp.net core? 如何在c#ASP.NET中传递此值? - How to pass this value in c# ASP.NET? 我需要分开业务对象和业务逻辑吗? C#中具有存储库模式的Asp.net MVC - Do I need to Separate Business Objects and Business Logic? Asp.net MVC with Repository Pattern in C# 如何在 ASP.net 核心 API C# 中返回具有存储库模式的自我父子? - How to return self parent and child with repository pattern in ASP net core API C#? 如何将 C# 类作为字符串 UserInput,并使其可用于 ASP.NET Core 运行时? - How can I take a C# Class, as a string UserInput, and make it available to the ASP.NET Core runtime? 如何在 C# 中使用 ASP.NET Core 连接外部认证服务器 - How can I connect with an external authentication server with ASP.NET Core in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM