Need help with DataContext

Jun 30, 2008 at 10:02 AM
Edited Jun 30, 2008 at 10:03 AM
Hi all!

I am using Unity in an Asp.net MVC project. I use some Repository classes to do database tasks. This is how I register my repository objects with Unity.

      container.RegisterType<System.Data.Linq.DataContext, DbDataContext>();
      container.Configure<InjectedMembers>().ConfigureInjectionFor<DbDataContext>(new InjectionConstructor());
      container.RegisterType<IListRepository, SqlListRepository>();
My controllers are dependent on these SqlRepository objects and I inject them with property injection using Dependency attribute.

Here is the problem:
My SqlRepository objects are in turn dependent on DbDataContext objects, here is a partial declaration from the SqlListRepository class:

public class SqlListRepository : IListRepository {
[Dependency]
private DbDataContext context { get; set; }
}

SqlRepository object are activated and injected into the controllers but, DbDataContext objects are not. "context" properties of all SqlRepository objects are all null.
I am sure I am doing something wrong, can someone please help me?

Thank you.
Jun 30, 2008 at 10:05 AM
Edited Jun 30, 2008 at 10:06 AM
Ok "private" is the problem here, it should be public. Problem resolved! :)
     [Dependency]
public DbDataContext context { get; set; }
}