Murali Murugesan
Murali Murugesan

Reputation: 22619

Using Dependency Injection on a method in Global.asax

Created a ASP.Net MVC3 application with DI container and using StructureMap. Everything works in controller method. But how can i do for a global.asax method?

Here I set the dependency resolver , Application_Start of global.ascx

    protected void Application_Start()
    {
    DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
    LoadApplicationMetaData(?,?,?);
    }

      private void LoadApplicationMetaData(IMetaDataService metaService, ITenantService tenantService, ICacheStorage store)
    {
        store.Add("TenantMeta", tenantService.GetAllTenants());

    }

    public class TenantService : ITenantService
    {
    private readonly ITenantRepository TenantRepsitory;

    public TenantService(ITenantRepository _tenantRepository)
    {
        TenantRepsitory = _tenantRepository;
    }
    }

In the this below line how can i do loosely coupling for a method call.

  **LoadApplicationMetaData(?,?,?); what should be passed** 

Note: TenantService class is expecting ITenantRepository

Upvotes: 0

Views: 2298

Answers (1)

Ben Foster
Ben Foster

Reputation: 34800

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

You clearly have a reference to the StructureMap container (your variable container) so just call container.GetInstance<IMetaDataService>() etc.

Upvotes: 1

Related Questions