Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:19+00:00 2026-05-28T23:34:19+00:00

I have a feeling that my use of an interface is incorrect. I know

  • 0

I have a feeling that my use of an interface is incorrect. I know that an interface is a contract that the concrete class has to adhere to.

So I will explain the problem I am trying to solve and maybe someone can point me in the right direction.

I am building an app that returns a page for any request, I have three page types, Cms, Product and Category.

All three have to implement the following interface:

public interface IPage
    {
        PageType PageType { get; set; }
        PageContent Content { get; set; }
        Meta Meta { get; set; }
    }

These properties are requeried whatever the page type.

a page may have extra properties depending on their type, for example a category page could be like so:

public class CategoryPage : IPage
    {
        public PageType PageType { get; set; }
        public PageContent Content { get; set; }
        public Meta Meta { get; set; }

        public List<Product> Products { get; set; }
    }

At the moment I have a page service that will return a page for the requested url.

Based on the PageType it knows what type of page to return.

The problem is that pageService returns an IPage so that it can return any of the page types.

This is a problem as not all my concretes just implement the interface, in the case of a category page it also has a List, which as you would expect I cant access unless I cast to the concrete type.

But is there a way I can return a generic page type and have the receiver know what concrete it is?

I’m sure that how I am doing it that the moment is not the best way and would like some direction and advice as to how I can solve this little problem.

Thanks

Update

I have settled for a cast.

I’m sure there must be a better way of handling a situation where several classes use some base properties but also implement their own. when you get one of these classes from a service you need to know what you have got back so you can work with the relevant properties..

Or maybe what I am trying to do here is just plain wrong and I need to take another approach. I think I will push on with what I have for now but keep thinking about it.

Update 2

I have changed the way I am doing this so I have no need for a cast, I have a PageType enum that I use to identify the type of page that is being worked with.

This coupled with a Ipage that inherits everything needed seems to be a nice enough solution and removes the need for a cast.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:34:19+00:00Added an answer on May 28, 2026 at 11:34 pm

    You can always check if the object you have a reference to is of a particular type using the is keyword;

    if(obj is Class1) {
    

    That said, if your design requires you to know concrete types, there is most likely something wrong with the design itself. If there are differences in behavior between the classes, implement the differences inside the classes instead of having to cast them to implement it outside of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an SDL problem, however I have the strong feeling that the problem
I have a website that has a number of URLs that people use to
I have a feeling that this query is pretty easy to construct, I just
I have a feeling that I am going to ask a stupid question, yet
I somehow have the feeling that modern systems, including runtime libraries, this exception handler
Have a couple questions, using GSON. I have a feeling that GSON might not
I have a feeling that this probably is not possible using strictly CSS, but,
I have a feeling that the Visual Studio SDK is targeted heavily towards the
I have a feeling that Request.ServerVariables(AUTH_USER) and User.Identity.Name return the same string while using
I have a Service class with a method called GetProducts(). That encapsulates business logic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.