Re: Inheritance and Interfaces
- From: shapper <mdmoura@xxxxxxxxx>
- Date: Sat, 11 Apr 2009 17:48:40 -0700 (PDT)
On Apr 12, 1:20 am, "Peter Duniho" <NpOeStPe...@xxxxxxxxxxxxxxxx>
wrote:
On Sat, 11 Apr 2009 16:42:09 -0700, shapper <mdmo...@xxxxxxxxx> wrote:
Hello,
I am having a few problems on inheritance and interfaces:
1. Is it possible, or correct, to inherit a class from 2 classes?
Possible? No. Correct? Depends on who you ask. Multiple inheritance is
allowed in some other languages (including C++), and some people swear by
it. But, it also introduces complexities that others feel outweigh any
benefits that might be had.
In C#, the closest equivalent is to use interfaces to define whatever
functionality you want to implement in the class, and then have the class
implement those interfaces. You can only inherit one class, but you can
implement any number of interfaces.
2. Can I have the following structure?
MyModel > Contains properties
IMyInterface > Contains methods
MyClass > Inherits MyModel to have its properties and IMyInterface
to implement its methods.
That depends on what you mean by "structure". The obvious interpretation
is a "struct", and if so, the answer is "no". A struct can't inherit
another struct.
If you simply mean "a data structure", then as long as that data structure
is a class the answer is "yes". That said, note that an interface can
have properties as well. You should use inheritance when it makes sense;
that is, your class really _is_ a more specific version of the class it's
inheriting, and that class provides implementation you need.
If you're just trying to declare some set of class members, but there's
not a genuine "is a" relationship between your class and the proposed base
class, then just put everything in an interface and implement the
interface. If you need to use an existing implementation, use that
implementation via composition rather than inheritance (i.e. put an
instance of that implementation in your own instance, and delegate all the
interface members to that instance by calling the composited
implementation from your own implementation of the interface).
Pete
I am implementing Data and Business Layers to use in my MVC projects.
So I have for example: PostModel, IPostRepository and PostRepository.
PostRepository implements IPostRepository to work with PostModel
(Create, Update, Delete, etc)
Now I am trying to create similar functionality to a custom ASP.NET
Profile provider.
This is a little bit more complex. The profile provider I am using can
be seeing here:
http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx
And I came up with the following:
MODEL:
public class Profile : ProfileBase {
public String Name {
get { return base["Name"] as String; }
set { base["Name"] = value; }
}
// Other properties
}
INTERFACE:
public interface IProfileProvider {
Profile GetProfile();
Profile GetProfile(String username);
}
PROVIDER:
public class ProfileProvider : Profile, IProfileProvider {
public Profile GetProfile() {
return Create(HttpContext.Current.Profile.UserName) as Profile;
} // GetProfile
public Profile GetProfile(String username) {
return Create(username) as Profile;
} // GetProfile
}
ProfileProvider, as the ULR I provided, it will have a reference in
the Web.Config.
What do you think?
Is this "ok"?
Thank You,
Miguel
.
- Follow-Ups:
- Re: Inheritance and Interfaces
- From: Peter Duniho
- Re: Inheritance and Interfaces
- References:
- Inheritance and Interfaces
- From: shapper
- Re: Inheritance and Interfaces
- From: Peter Duniho
- Inheritance and Interfaces
- Prev by Date: Re: Inheritance and Interfaces
- Next by Date: Re: Inheritance and Interfaces
- Previous by thread: Re: Inheritance and Interfaces
- Next by thread: Re: Inheritance and Interfaces
- Index(es):
Relevant Pages
|