Unity + ObjectDataSource

May 21, 2009 at 12:48 PM

Is it possible to use Unity for injection type in TypeName in ObjectDataSource?

May 22, 2009 at 9:44 AM

Kind of. What I'm doing in my ASP.NET application is as follows:

1. Set OnObjectCreating:

 <asp:ObjectDataSource ID="odsCurrencies" runat="server" OnObjectCreating="CreateUsingInjection" TypeName="Services.Interfaces.ICurrencyService, Services">

2. In your pabe code behind (or in your base page) create method CreateUsingInjection: 

protected void CreateUsingInjection(object sender, ObjectDataSourceEventArgs e)
{
   
var ods = sender as ObjectDataSourceView;
    if (ods != null)
    {
       
var type = Type.GetType(ods.TypeName);
       
if (type == null)
        {
           
throw new Exception("Cannot find type " + ods.TypeName);
        }
       e.ObjectInstance = 
ServiceLocator.Current.GetInstance(type);
   }
}

where ServiceLocator.Current.GetInstance is my DI container wrapper.

Hope this helps ;)

May 22, 2009 at 12:10 PM
Edited May 22, 2009 at 12:33 PM

Thanks!!

In my case, comes the following:

[Dependency]
public ISettingsService Service { get; set; }
...
protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
          e.ObjectInstance = Service;
}

Jun 23, 2009 at 2:51 PM

Hi,

I have used the code above (from plsc_rover) to achieve dependency injection with unity and an ObjectDataSource. However, it does not work if the ObjectDataSource is inside a UserControl. 

Is there a way to use Unity + ObjectDataSource + UserControl ?

I imagine I could probably set the property/dependency of my UserControl in the containing page by doing something like the following:

[Dependency]
public IExampleService service {get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    MyUserControl.service = service;
}

but i would like to avoid this if possible. Can anyone suggest a way to implement a more elegant solution using UserControls and Unity?

Thanks

Jun 23, 2009 at 3:22 PM

For it we use the BuildUpModule:

public class BuildUpModule : IHttpModule
{
        private IUnity _unityCtx;
        void IHttpModule.Init(HttpApplication context)
        {
            _unityCtx = context as IUnity;
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
        }
        void context_PreRequestHandlerExecute(object sender, System.EventArgs e)
        {
            Page currentPage = HttpContext.Current.CurrentHandler as Page;
            if (currentPage != null)
            {
                _unityCtx.Container.BuildUp(currentPage.GetType(), currentPage);
                currentPage.PreLoad += currentPage_PreLoad;
            }
        }

        void currentPage_PreLoad(object sender, System.EventArgs e)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                InjectUserControls(page);
            }
        }
        private void InjectUserControls(Control currentControl)
        {
            foreach (Control control in currentControl.Controls)
            {
                if (control is UserControl)
                {
                    _unityCtx.Container.BuildUp(control.GetType(), control);
                }
                InjectUserControls(control);
            }
        }
...

Jun 23, 2009 at 3:28 PM
Edited Jun 23, 2009 at 3:29 PM

Code in Global.asax:

 

public class Global : HttpApplication, IUnity
{

public static IUnityContainer Unity;
 protected void Application_Start(object sender, EventArgs e) { Unity = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers.Default.Configure(Unity); } public IUnityContainer Container { get { return Unity; } } } public interface IUnity { IUnityContainer Container { get; } }

 

Jun 24, 2009 at 8:43 AM

Thanks! Got it working. :)