.net 核心持久层

.net core persistence layer

即时生成 } return 值;

    }

到目前为止一切顺利,但是当我尝试从我的控制器使用我的 DAL 时,我收到了这个错误

 public class UserValuesController : Controller
{

为 DAL class 编写 public 构造函数。

public class DAL
{
    private readonly IConfiguration config;

    public DAL(IConfiguration config)
    {
        this.config = config;
    }

    public List<Uservaluesfull> GetUservalues(string SID)
    {
        string PathwayConnString = config.GetConnectionString("PathwayConnString");

        //var SID = user.FindFirst("onprem_sid")?.Value;
        List<Uservaluesfull> values = new List<Uservaluesfull>();
        using (SqlConnection myConnection = new SqlConnection(PathwayConnString))
        {
            SqlCommand myCommand = new SqlCommand("usp_GetUserInfo", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter SIDParameter = myCommand.Parameters.Add("SID", SqlDbType.NVarChar, 100);
            SIDParameter.Value = SID;

            myConnection.Open();

            using (SqlDataReader oReader = myCommand.ExecuteReader())
            {
                while (oReader.Read())
                {
                    Uservaluesfull s = new Uservaluesfull();
                    DATA here

                    values.Add(s);
                }
                myConnection.Close();
            }
        }
        return values;

    }
}

控制器会像。

public class UserValuesController : Controller
{
    private readonly DAL _dal;
    public UserValuesController(DAL dal)
    {
        _dal = dal;
    }

    [HttpGet("[action]")]
    public List<Uservaluesfull> Get()
    {            
        var SID = User.FindFirst("onprem_sid")?.Value;
        return new _dal.GetUservalues(SID);
    }
}

同时创建接口 IDAL。

public interface IDAL
{
    List<Uservaluesfull> GetUservalues(string SID);
}

然后,在启动时添加这些class。

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IDAL, DAL>();
}