Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Funq container not working properly?

I'm getting a null reference exception when hitting my code that tries to access the database.

This is in global.asax and I have stepped through the debugger and this code is being executed.

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);
    }

}

When I hit this code (using (var db = DbConnectionFactory.OpenDbConnection())) I am getting a NullReferenceException.

Object reference not set to an instance of an object.

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;
    }
}

Am I to assume something is wrong with my Connection or is there something I'm not doing right to make Funq work?

like image 671
Mike Roosa Avatar asked Dec 20 '22 07:12

Mike Roosa


1 Answers

You have shown some Funq container configuration that is injecting dependencies into your AppUserRepository class. But you haven't shown how and where is this class used. Since your question is tagged with asp.net-mvc I assume that this happens inside an ASP.NET MVC controller action. Something along the lines of:

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);
    }
}

If you want to use a dependency injection framework in ASP.NET MVC you will have to write a custom dependency resolver which will do the injection into the controllers.

So let's go ahead and write one for 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>();
    }
}

and then we could register it in 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);
}

Also I would recommend you using constructor injection for your AppUserRepository class because the DbConnectionFactory is an absolutely required dependency for this class to work properly:

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;
    }
}
like image 92
Darin Dimitrov Avatar answered Jan 06 '23 17:01

Darin Dimitrov