Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide dependency injection via StructureMap for a custom role provider with WCF?

We're going to be using a custom role provider with WCF. The overridden method GetRolesForUser will require the use of an already existing RoleRepository.

Now, with a run-of-the-mill class, we'd construct it using StructureMap and the RoleRepository dependency would be injected via the constructor.

However, it's WCF that does the constructing of the the custom role provider class and that's 'done' declaritavely via the roleManager attribute in the web.config.

I don't really want to hard-wire the RoleRepository depndency into the custom role probvider class but it's looking like I'll have to.

Any ideas?

like image 289
user129345 Avatar asked Jan 08 '10 09:01

user129345


1 Answers

The RoleProvider and related types are legacies of ASP.NET which are infamous for not being DI-friendly. They require a default constructor and there are no hooks offered to initialize them. It sucks, but that's the way it is.

In such situations, the best remedy is to implement the RoleProvider as a Humble Object. In other words, the RoleProvider must wire up all dependencies, but from there, it delegates all implementation to your own open and extensible API.

like image 63
Mark Seemann Avatar answered Oct 22 '22 11:10

Mark Seemann