gpt4 book ai didi

c# - 工作单元的依赖注入(inject)

转载 作者:行者123 更新时间:2023-11-30 15:54:16 25 4
gpt4 key购买 nike

我有一个问题,我的工作单元在调用时不会创建 AppSettings 实例。 Unitofwork 用于我的存储库数据层。

出现这个错误:

An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object. Core.UnitOfWork..ctor() in UnitOfWork.cs, line 24

Stack Query Cookies Headers NullReferenceException: Object reference not set to an instance of an object. Core.UnitOfWork..ctor() in UnitOfWork.cs + _connection = new SqlConnection(App.GetConnectionString()); Core.Service.UserService.Login(User entity) in UserService.cs + using (var uow = new UnitOfWork(/connStr/)) SRTManagement.Controllers.LoginController+d__6.MoveNext() in LoginController.cs + var _user = service.Login(user);

启动.cs

    public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IAppSettings,AppSettings>();
services.AddMvc();
}

IAppSettings.cs

namespace Core.Etc
{
public interface IAppSettings
{
string GetConnectionString();
}
}

AppSettings.cs

namespace Core.Etc
{
public class AppSettings : IAppSettings
{
public readonly string _connectionString;

public AppSettings(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}

public string GetConnectionString()
{
return _connectionString;
}
}
}

UnitOfWork.cs

namespace Core
{
public class UnitOfWork : IUnitOfWork
{
private IDbConnection _connection;
private IDbTransaction _transaction;
private IUserRepository _user;
private IRoleRepository _role;
private IAppSettings App;
private bool _disposed;
private bool _token;

public UnitOfWork()
{
_connection = new SqlConnection(App.GetConnectionString());
_connection.Open();
_transaction = _connection.BeginTransaction();
_token = false;
}

public IUserRepository UserRepository
{
get { return _user ?? (_user = new UserRepository(_transaction)); }
}

public IRoleRepository RoleRepository
{
get { return _role ?? (_role = new RoleRepository(_transaction)); }
}

public bool Success()
{
return _token;
}

public void Commit()
{
try
{
_transaction.Commit();
_token = true;
}
catch
{
_transaction.Rollback();
_token = false;
throw;
}
finally
{
_transaction.Dispose();
_transaction = _connection.BeginTransaction();
ResetRepositories();
}
}

private void ResetRepositories()
{
_user = null;
_role = null;
App = null;
}

public void Dispose()
{
DisposeConn(true);
GC.SuppressFinalize(this);
}

private void DisposeConn(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if(_transaction != null)
{
_transaction.Dispose();
_transaction = null;
}
if(_connection != null)
{
_connection.Dispose();
_connection = null;
}
}
_disposed = true;
}
}

~UnitOfWork()
{
DisposeConn(false);
}
}
}

最佳答案

IAppSettings 没有被注入(inject)到您的 UnitOfWork 中,因此当您调用它时它将为 null

public class UnitOfWork : IUnitOfWork {
private IDbConnection _connection;
private IDbTransaction _transaction;
private IUserRepository _user;
private IRoleRepository _role;
private IAppSettings App;
private bool _disposed;
private bool _token;

public UnitOfWork(IAppSettings App) {
this.App = App;
_connection = new SqlConnection(App.GetConnectionString());
_connection.Open();
_transaction = _connection.BeginTransaction();
_token = false;
}
//Remove the rest of the code for brevity
}

假设 UnitOfWork 也已注册到服务集合。

public void ConfigureServices(IServiceCollection services) {
services.AddTransient<IAppSettings, AppSettings>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddMvc();
}

我还建议重新考虑当前的设计,避免将 UoW 与实现问题紧密耦合,例如 SqlConnection

如果继续使用 ADO,则考虑使用 IDbConnectionFactory 抽象。

public class MyDbConnectionFactory : IDbConnectionFactory {
private readonly IAppSettings appSettings;

public MyDbConnectionFactory(IAppSettings appSettings) {
this.appSettings = appSettings;
}

public IDbConnection CreateConnection() {
return new SqlConnection(appSettings.GetConnectionString());
}
}

这将使 UoW 重构为

public class UnitOfWork : IUnitOfWork {
private IDbConnection _connection;
private IDbTransaction _transaction;
private IUserRepository _user;
private IRoleRepository _role;
private bool _disposed;
private bool _token;

public UnitOfWork(IDbConnectionFactory factory) {
_connection = factory.CreateConnection();
_connection.Open();
_transaction = _connection.BeginTransaction();
_token = false;
}

//Remove the rest of the code for brevity
}

使用常规服务注册

public void ConfigureServices(IServiceCollection services) {
services.AddTransient<IAppSettings, AppSettings>();
services.AddTransient<IDbConnectionFactory, MyDbConnectionFactory>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddMvc();
}

关于c# - 工作单元的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50672488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com