ASP.NET MVC: Why the Account Model sample use MembershipProvider instead of just Membership
I'm a little confused. In many tutorials, including some in MSDN and ASP.NET website, it's almost always about Membership. But when examining the AccountModel class that's created by default, it's about an objet called _provider of type MembershipProvider.
First, I wonder why they just don't use Membership instead of MembershipProvider? secondly (and most irritating), why when I try to use _provider.GetAllUsers(), I'm asked to provide also parameters stuff such as pageindex, pagesize, etc. But, when I use Membership.GetAllUsers(), I don't have to supply anything.
What the point of开发者_运维技巧 having both Membership and MembershipProvider with slightly different methods?
EDIT
Those are some of the documents I have used to learn how to use Membership. I'm talking about my books.
ASP.NET tutorial,
4GuysFromRolla,
An other 4GuysFromRolla,
MSDN
Thanks for helping
MembershipProvider
is an abstract class, meaning you can't create a new instance of it, rather, you must create your own custom class that inherits from it and then implement the functions.
Having a base class as a constructor means you can pass in any class that inherits from it. As others have said, this helps when you're testing. You can pass in a fake implementation or a mock of MembershipProvider
to test all the functionality of your controller/class.
If you just use membership instead of a provider, you would have to supply a configuration file with a mock provider for your tests. In this way, you simply create a fake membership provider and use it when you create the MembershipService instance. Membership is just a wrapper around the default MembershipProvider. So as @bzlm said, it is more testable to use the provider rather than the Membership static class.
精彩评论