gpt4 book ai didi

c# - 为什么我的 Funq 容器不能正常工作?

转载 作者:太空狗 更新时间:2023-10-30 00:15:19 27 4
gpt4 key购买 nike

我在访问试图访问数据库的代码时遇到空引用异常。

这是在 global.asax 中,我已经单步执行了调试器并且正在执行这段代码。

public class MvcApplication : System.Web.HttpApplication
{
public class DmsAppHost : AppHostBase
{
public DmsAppHost() : base("API", typeof(AppUsersService).Assembly) { }

public override void Configure(Funq.Container container)
{
var dbConnectionFactory = new OrmLiteConnectionFactory("Server=localhost;Port=5432;User Id=dms; Password=dms; Database=dms", PostgreSqlDialect.Provider);
container.Register<IDbConnectionFactory>(dbConnectionFactory);
container.RegisterAutoWiredAs<AppUserRepository, IAppUserRepository>();
}
}

protected void Application_Start()
{
new DmsAppHost().Init();

AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
}

}

当我点击此代码时(使用 (var db = DbConnectionFactory.OpenDbConnection())),我得到了一个 NullReferenceException。

对象引用未设置到对象的实例。

public class AppUserRepository : IAppUserRepository
{
public IDbConnectionFactory DbConnectionFactory { get; set; }

public AppUser GetAppUserByUsername(string username)
{
AppUser appUser;
using (var db = DbConnectionFactory.OpenDbConnection())
{
appUser = db.QuerySingle<AppUser>(new { username });
}
return appUser;
}
}

我是否应该假设我的 Connection 有问题,或者我是否没有做正确的事情来使 Funq 正常工作?

最佳答案

您已经展示了一些 Funq 容器配置,这些配置将依赖项注入(inject)到您的 AppUserRepository 类中。但是你还没有说明这个类是如何使用的以及在哪里使用的。由于您的问题被标记为 asp.net-mvc 我假设这发生在 ASP.NET MVC Controller 操作中。类似的东西:

public class HomeController: Controller
{
private readonly IAppUserRepository repo;
public HomeController(IAppUserRepository repo)
{
this.repo = repo;
}

public ActionResult Index(int id)
{
var model = this.repo.Get(id);
return View(model);
}
}

如果你想在 ASP.NET MVC 中使用依赖注入(inject)框架,你必须编写自定义 dependency resolver它将注入(inject) Controller 。

那么让我们继续为 Funq 写一个:

public class FunqDependencyResolver : IDependencyResolver
{
private readonly ContainerResolveCache cache;
public FunqDependencyResolver(Container container)
{
this.cache = new ContainerResolveCache(container);
var controllerTypes =
(from type in Assembly.GetCallingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(type)
select type).ToList();
container.RegisterAutoWiredTypes(controllerTypes);
}

public object GetService(Type serviceType)
{
return this.cache.CreateInstance(serviceType, true);
}

public IEnumerable<object> GetServices(Type serviceType)
{
return Enumerable.Empty<object>();
}
}

然后我们可以在Application_Start中注册它:

protected void Application_Start()
{
var container = new Container();
var dbConnectionFactory = new OrmLiteConnectionFactory("Server=localhost;Port=5432;User Id=dms; Password=dms; Database=dms", PostgreSqlDialect.Provider);
container.Register<IDbConnectionFactory>(dbConnectionFactory);
container.RegisterAutoWiredAs<AppUserRepository, IAppUserRepository>();
DependencyResolver.SetResolver(new FunqDependencyResolver(container));

AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
}

此外,我建议您为 AppUserRepository 类使用构造函数注入(inject),因为 DbConnectionFactory 是此类正常工作所必需的依赖项:

public class AppUserRepository : IAppUserRepository
{
private readonly IDbConnectionFactory dbConnectionFactory;
public class AppUserRepository(IDbConnectionFactory dbConnectionFactory)
{
this.dbConnectionFactory = dbConnectionFactory;
}

public AppUser GetAppUserByUsername(string username)
{
AppUser appUser;
using (var db = this.dbConnectionFactory.OpenDbConnection())
{
appUser = db.QuerySingle<AppUser>(new { username });
}
return appUser;
}
}

关于c# - 为什么我的 Funq 容器不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15841890/

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