Ashish
Ashish

Reputation: 21

Static factory method does not return object instance in MVC Controller

We are migrating from .aspx web application to MVC app.

Below is the problem while migrating:

I have a third party dll which contains static factory method to return instance by given Interface name.

Controller code:

IBus objBus = (IBus) ObjectFactory.GetInstance("IBus") as IBus;

Here, ObjectFactory is from third party dll with definition as below (no more code details are available on GetInstance method).

public class ObjectFactory
{
    public ObjectFactory();
    public static object GetInstance(string interfaceName);
}

GetInstance method works perfectly in .aspx application - objBus is created successfully. But the same line (Controller code) when executed in MVC controller returns objBus as null.

Please suggest what could be the problem. I guess it may be due to difference in architecture and/or page life cyle of .aspx and mvc apps. Any suggestions on the problem are greatly appreciated.

Upvotes: 0

Views: 134

Answers (1)

Derek Hackett
Derek Hackett

Reputation: 200

I think you should reach out to your third party DLL provider and ask for documentation on how to work with ASP.NET MVC.

If I was you I would think about dropping that DLL using Unity for you dependency injection for an MVC Project. All you need to do is in the Application_Start method in the Global.asax use the UnityConfig and DependecyResolver to inject you instances of class in to any of your controller methods.

If you need details check out these great articles.

http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html

http://dotnetslackers.com/articles/aspnet/Using-Microsoft-Unity-in-ASP-NET-MVC.aspx

http://www.codeproject.com/Articles/99361/How-To-Use-Unity-Container-In-ASP-NET-MVC-Framewor

Upvotes: 0

Related Questions