At the top level I have a Person class. Next I want .NET's MembershipUser class to inherit from it, because a member is a person. However I want to extend the MembershipUser class which means, I think, I need to create my OWN MembershipUser class which inherits from MembershipUser and then adds my own properties. But once I do that, I can no longer inherit from Person due to multiple inheritance rules (ASP.NET 2.0).
+1
A:
You can solve this by creating an interface, IPerson
and replacing your concrete Person
class with a class which inherits from MembershipUser
and implements IPerson
.
You could also keep your concrete Person
, create IPerson
and have your own class encapsulate a Person
instance and inherit from MembershipUser
while implementing IPerson
.
In either case, anywhere where you once used a concrete Person
type you should replace with IPerson
(such as method arguments).
interface IPerson
{
string LastName { get; set; }
// ...
}
class MyMembershipUser : MembershipUser, IPerson
{
private Person _person = new Person();
// constructors, etc.
public string LastName
{
get { return _person.LastName; }
set { _person.LastName = value; }
}
}
Alternatively you could continue using Person and have it encapsulate a MembershipUser instance (as part of a constructor) and include an explicit cast for Person to MembershipUser when needed...
class Person
{
private readonly MembershipUser _mu;
public Person(MembershipUser mu)
{
_mu = mu;
}
public static explicit operator MembershipUser(Person p)
{
// todo null check
return p._mu;
}
}
// example
var person = new Person(Membership.GetUser("user"));
Membership.UpdateUser((MembershipUser)person);
I would go with the interface implementation solution myself.
cfeduke
2009-09-11 20:01:24