I am developing a .Net Web Forms project structure and I decided to use Unity as a DI framework for the application.
MSDN-Resolving in Asp.Net states that in order to inject dependencies in my project I need to build up the initial object which is created outside of the DI container. That being said we come to the question.
The attribute annotations such as
[Dependency]
are classes extending theAttribute
class. In order to use them, another namespace must be included in the declaring class, thus making our class dependent on theMicrosoft.Practices.Unity.DependencyAttribute
class. So now, even though our class might not be aware of the implementation of IMyInterface that it uses, it has to be aware of the concrete implementation of the Dependency class? What is it that I am missing here? If we were to change the DI framework, we would need to remove all the annotations throughout the project.Is there a way to avoid this kind of declaration (configuration files or what-not) outside of the container?
EDIT --> Code here
/*This is the abstract base class where I want the dependency injection to occur*/
public abstract class BasePage : System.Web.UI.Page
{
private IMyService _dtService;
public IMyService DtService
{
get { return _dtService; }
set { _dtService = value; }
}
}
The Default.aspx code behind
public partial class _Default : BasePage
{
public _Default( )
{
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
DataClass dt = DtService.GetDataById(2);
lblWhatever.Text = dt.Description;
}
}
}
Global code behind
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
IUnityContainer myContainer = HttpContext.Current.Application.GetDIContainer();
myContainer.RegisterType<IMyService,MyServiceClient>(new
InjectionConstructor("MyServiceWsEndpoint"));
// I have tried this with BasePage too
myContainer.RegisterType<_Default>(new InjectionProperty("DtService"));
}
}
And the module
public class UnityHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
public void Dispose() { }
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
IHttpHandler currentHandler = HttpContext.Current.Handler;
/*This does not work*/
HttpApplicationStateExtensions.GetDIContainer(HttpContext.Current.Application).BuildUp(
currentHandler.GetType(), currentHandler);
/* While this works*/
HttpApplicationStateExtensions.GetDIContainer(HttpContext.Current.Application).BuildUp<_Default>((_Default)currentHandler);
var currentPage = HttpContext.Current.Handler as Page;
if (currentPage != null)
{
currentPage.InitComplete += OnPageInitComplete;
}
}
}
The code inside the module is reached every time. The line Does INDEED work when I use the [Dependency]
Attribute.