To start, I know this can be done but as i was not involved in setting it up or implementing it i did not see how it was done.
At a former job, when i was helping support a web-application (MVC or Web-App), we had your usual EF design pattern. But when it came time to consume the EF Objects all we had to do was declare a property in the Page/Controller and we had access to the Repository/Service.
We did not have to do anything as far as declaring an explicity var prop1 = IOC.Resolve<Type>;
after declaration, it would be auto populated. I assumed this was dependency injection but have not seen any write ups on how to make this possible.
Any help would be greatful.
Edit 2014-04-13
Attempted to do the Property Injection within the Global.asax file like such
protected void Application_Start( object sender , EventArgs e ) {
App.Initialize( null );
if( HttpContext.Current != null ) {
var pg = base.Context.Handler as Page;
if( pg != null ) {
App.InjectProperties( pg );
}
}
}
I have tried do the if....
logic in Application_BeginRequest
with no success on either.
App.cs
public class App {
public static void Initialize( string log4netPath ) {
var container = new WindsorContainer();
container.Kernel.ComponentModelCreated += ( s => {
if( s.LifestyleType == LifestyleType.Undefined ) {
s.LifestyleType = LifestyleType.PerWebRequest;
}
} );
container.Install(
new DomainInstaller() ,
new RepositoryInstaller() ,
new ServiceInstaller()
);
container.Install( new SiteInstaller() );
if( log4netPath != null || string.IsNullOrWhiteSpace( log4netPath ) )
container.AddFacility( new LoggingFacility( LoggerImplementation.Log4net , log4netPath ) );
CWC.Init( container );
}
public static void InjectProperties( Page pg ) {
var type = pg.GetType();
foreach( var prop in type.GetProperties() ) {
if( CWC.IsInitialized ) {
try {
var obj = CWC.Resolve(prop.PropertyType);
prop.SetValue( pg , obj , null );
} catch( System.Exception ) {
//do nothing
}
}
}
}
}
DomainInstaller.cs (pretty much all the Installer classes are setup this way):
public class DomainInstaller : IWindsorInstaller {
#region IWindsorInstaller Members
public void Install( Castle.Windsor.IWindsorContainer container , Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store ) {
container.Register( Types.FromAssembly( Assembly.GetExecutingAssembly() )
.Where( t => t.Namespace.StartsWith( "Woodsoft.Domain" ) )
.WithService.FirstInterface().LifestylePerWebRequest()
);
}
#endregion
}
So i think i may have found my problem, but i am unsure how to implement the solution, as both my Pages and MasterPages will contain properties that will need to be injected from my EF data-framework. Below is an example of an MVC implementation, but Page and MasterPage object do not have an immediate common derived Interface that i can use like i could for Controllers.
Another project, MVC pattern:
public class SiteInstaller : IWindsorInstaller {
#region IWindsorInstaller Members
public void Install( IWindsorContainer container , IConfigurationStore store ) {
container.Register(
Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient()
);
}
#endregion
}
Any help to modify this Installer from MVC to WebForms? WIth the intention of MasterPage's and Page's both having Properties that will need to be injected from the Windsor Container.