1

I am designing an application with the following layers

  • Data Layer(w/ generic repository on top of EF 4.1 )
  • Service Layer - all business logic goes here
  • ASP.NET MVC 3 website and ServiceStack.NET web services

I am trying to implement a custom membership provider to leverage my service/repositories

My initial though was to call the service layer methods from within the provider but, of course, I can not use DI(via Ninject) as the Membership is handled by the framework and prevents me from using constructor injection.

I have tried instantiating an instance of my UserService class within the initialize method in the provider via:

userService = (UserService)Activator.CreateInstance(typeof(UserService));

But given that user service depends on a repository being injected by Ninject, this does not work as the repo never gets injected.

What am I missing here? what is the easiest way to get around this issue? Should I be coming at this from an entire different angle?

EDIT: Here is my user service as requested

public class UserService : IUserService
{
    private readonly IUserRepository userRepository;
    private readonly IUnitOfWork unitOfWork;

    public UserService(IUserRepository userRepository, IUnitOfWork unitOfWork)
    {
        this.userRepository = userRepository;
        this.unitOfWork = unitOfWork;
    }


    //methods(AddUser, etc.)...

}
stephen776
  • 9,134
  • 15
  • 74
  • 123
  • DI only exists in your top layer right? You don't need DI in the rest of your layers. By top I'm refering to your MVC application. – Jamie Dixon Oct 05 '11 at 14:23
  • I am actually using DI in all the layers to inject dependencies between layers. Service Layer classes are dependent on Repositories, so the service layers are built around interfaces and the concrete repos are injected. The DI is only setup in top layer in the global.asax, if thats what you mean? Is my method bad practice? – stephen776 Oct 05 '11 at 14:26
  • Actually, my entire architecture is modeled after this sample: http://efmvc.codeplex.com/ Only I am using Ninject instead of Unity – stephen776 Oct 05 '11 at 14:28
  • The architecture sounds fine, I just wanted to check that you're only doing the actual injection part (using the DI framework) in your MVC layer and not in the other layers also. – Jamie Dixon Oct 05 '11 at 14:31
  • Can you show us the code of your UserService please. – Jamie Dixon Oct 05 '11 at 14:34
  • I don't see what's stopping you from injecting your IUserService. – Jamie Dixon Oct 05 '11 at 15:36
  • From what I understand the membership provider class is instantiated by .NET before Ninject has a chance to inject dependencies, preventing the use of constructor injection. – stephen776 Oct 05 '11 at 15:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4040/discussion-between-jamie-dixon-and-stephen776) – Jamie Dixon Oct 05 '11 at 16:27
  • http://stackoverflow.com/questions/2753424/dependency-injection-with-custom-membership-provider/4124385#4124385 – Mark Oct 05 '11 at 16:50
  • Thanks mark...tried property injection to no avail. – stephen776 Oct 05 '11 at 16:52
  • I am wondering if it makes sense to skip the service/repo layer for authentication purposes and let the custom provider access the objectcontext of EF directly? – stephen776 Oct 05 '11 at 16:53
  • [This is](http://www.planetgeek.ch/2012/02/08/asp-net-provider-injection-with-ninject-3-0-0/) a solution that worked for me. – mmacneil007 Jul 08 '12 at 14:08

1 Answers1

0

If your core problem is testing and making your DI work, then one option is to facade the membership bits and create an interface off the facade. You can then set up the unit tests and even use some type of IoC container (DI) library.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
  • I may have one in some older code I did, but I am not sure I still have it. I will look, but it will be this weekend. I do have exmaples of other types of facades, if the concept is enough to get rolling, however. – Gregory A Beamer Oct 10 '11 at 14:53