i'm developing an ASP.NET MVC site that actually use a custom profileProvider to retrieve info (like last page visited, max number of record to view in a grid, notification via mail etc) related to a single user stored in a custom database. I don't like this technique because profileProvider is not easely injectable, there is an alternative way to obtains the same functionality exposed by profile provider? I think that probably is possible to use an asp.net module but i'm not an expert.
2 Answers
I think what tartafe means is that providers (also your custom ProfileProvider) are instantiated and controlled by external code (System.Web.Configuration.ProvidersHelper).
The Dependency Injection pattern says any objects your custom provider relies on, should be passed in by a controller (or container). Cfr. Dependency Injection for Loose Coupling). Typically your controller will initialize a repository, datacontext, or other object and pass it in through the constructor or a property. But that is not possible with a custom provider since its 'controller' is sealed in System.Web.Configuration.
I think the question than becomes: is there any way to influence how ProvidersHelper instantiates and controls our custom providers?

- 1,271
- 14
- 28
-
Ok, I found an answer using Windsor [here](http://bugsquash.blogspot.com/2010/11/windsor-managed-membershipproviders.html). – flip Feb 13 '11 at 02:04
-
I've found a couple more implementations. [This](http://www.davidhayden.com/blog/dave/archive/2007/10/30/CreateCustomProfileProviderASPNET2UsingLINQToSQL.aspx) one uses ServiceLocator and [this](http://noahblu.wordpress.com/2009/02/19/custom-membershipprovider-using-repository-dependency-injection-pattern-magic/) StructureMap. All use some kind of IoC tool, which I think is overkill for ASP.Net since you can use Pages and UserControls for all other DI. Please let me know how you dealt with it since I'm struggling with the same issue. – flip Feb 15 '11 at 01:06
What exactly do you mean by it is not easily injectable?
If you just mean that you don't want it in your Controller, then create an action filter that adds the model of user info you need to view data.
If you mean that you don't want a System.Web dependency somewhere that you are using the profile information, then create an adapter interface like IProfileService
and implement it with a profile-provider wrapper.
If you don't mean either of the two things I guessed, then please try to explain, because the language is a bit unclear.

- 41,281
- 29
- 127
- 212
-
hi, profile provider have a constructor without parameter. So i can't easely use a dependency injection provider, i want make something that like profile provider permit to populate the object User of httpcontext, idea? – tartafe Feb 02 '11 at 17:47
-
That is not complicated to do with an Action Filter. What Dependency Injection service are you using, because there is no reason that you can't use DI with a ProfileProvider. – smartcaveman Feb 02 '11 at 18:21