开发者

How do I wrap MembershipUser

How would i wrap MembershipUser so that the c开发者_如何学Call below returns. Reason for wrapping is I would like the return result to be an interface to I can mock the user.

public IMembershipUser GetUser(string username, bool userIsOnline)        
        {
            var user = Membership.GetUser(username, userIsOnline));

            //Do something 
            return WrappedUser;
        }

Thanks


I'm afraid you can't do this becuase..

System.Web.Security.MembershipUser type does not inherit from any special base-type and also it's not declared as partial, so there's no way you can do this.

But if you let us know what exactly you want to do, we can come up with alternate solutions.


You can achieve this by implementing your own SiteMembershipProvider. Here's an example snippet of an implementation I did a while back:

public class CustomMembershipUser : MembershipUser
{
    public UserItem UserItem { get; private set; }

    public CustomMembershipUser(string providerName, UserItem user)
        : base(providerName, user.UserName, "user_" + user.UserID, user.Email,
            null, null, true, !user.IsCurrent, DateTime.MinValue, 
            DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)
    {
        this.UserItem = user;
    }
}

public class SiteMembershipProvider : MembershipProvider
{
    ....

    private static MembershipUser GetMembershipUser(UserItem user)
    {
        return new CustomMembershipUser(_membershipProviderName,user);
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        // Load user
        return GetMembershipUser(foundUser);
    }

    ....
}

In Web.Config:

    <membership defaultProvider="SiteMembershipProvider">
        <providers>
            <clear/>
            <add name="SiteMembershipProvider" type="SiteMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" connectionStringName="myConnectionString"/>
        </providers>
    </membership>

This should default your entire site to use the new MembershipProvider called SiteMembershipProvider.

Have a look here or here for an example to implement the entire class.


You could create a class which implements your IMemberShipUser interface and internally stores a reference to the "real" MembershipUser.

The properties and methods on your wrapped class could then simply delegate to the real reference.

Edit - A super simple example below:

using System.Web.Security;
namespace Example
{
    public interface IMembershipUser
    {
        string UserName { get; }    
        string Email { get; }    
    }

    public class WrappedMembershipUser: IMembershipUser
    {
        private readonly MembershipUser realUser;

        public WrappedMembershipUser(MembershipUser realUser)
        {
            this.realUser = realUser;
        }
        public string UserName
        {
            get { return realUser.UserName; }
        }
        public string Email
        {
            get { return realUser.Email; }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜