Re: Inheritance and Interfaces

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Does anyone need IDEfile PCBs?
    ... ProFile interface between an Apple II and an IDE drive when there ... IIRC the Profile was only supported by UCSD Pascal, while most people in EU ran DOS 3.3 and most in US had ProDOS. ... The IDEfile protocol converter has been designed for the Apple III and Lisa. ... There were no alternatives to the Profile and Widget drives. ...
    (comp.sys.apple2)
  • Problem setting media type for new profile
    ... I'm trying to create a custom profile with one video stream of Windows Media ... Then I query the profile manager for the IWMProfile3 interface. ...
    (microsoft.public.windowsmedia.sdk)
  • Re: network profiles - do they work for you? they are broken for me!
    ... system-config-network is broken? ... > I tried using network profiles on my Fedora Core 6 and Fedora 7 ... When I want a new profile, say for my home lan, using the ... -- Edit the copy of the lan interface (leaving the original ...
    (Fedora)
  • Re: NTP sever on an isolated Network
    ... After I disabled the AppArmor, looks like the ntp daemon tries to start the ... Listening on interface #0 wildcard, ... it would be nice to make users aware that their configuration ... actually interacts with installed AA profile. ...
    (comp.protocols.time.ntp)
  • Re: SQL 2005 Database Mail Setup
    ... First I would press the script icon from the interface. ... One of the stored procs takes the profile name ... then study the stored procs and go ...
    (microsoft.public.sqlserver.setup)