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 8008111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:02:18+00:00 2026-06-04T18:02:18+00:00

I am building a multi layered application. When i pass an object from my

  • 0

I am building a multi layered application.

When i pass an object from my presentation layer to my business layer, i want to cast it to an interface type, because the business layer does not know of the concrete class from the presentation layer.

    public ActionResult SubmitSurvey(SurveyAnswer answers)
    {

        ISurveyAnswer answer = (ISurveyAnswer)answers;

        bc.SaveSurvey(answer);

        return null;
    }

The class SurveyAnswer implements the interface ISurveyAnswer

When i check the type in the business layer:

 public void SaveSurvey(ISurveyAnswer answer)
        {
            String type = answer.GetType().Name;
        }

the GetType().Name method returns SurveyAnswer which is the concrete class from the presentation layer. Is it supposed to do that?

  • 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-06-04T18:02:20+00:00Added an answer on June 4, 2026 at 6:02 pm

    Casting does not change the type of the object. The object always remains the same as when it was created. Casting simply changes the interface through which you are accessing the object. It changes your “view” of the object, if you will. It’s not a problem, though. What you are doing is correct. All the UI layer cares about is the ISurveyAnswer interface, so as long as the object’s concrete type implements that interface, then all is well and will work as you expect. The beauty of doing so it that now the UI layer may be given any kind of object (including mock objects) and it won’t matter, as long as the object implements that same interface, the UI will work and won’t care. GetType will let you inspect the object to see what the actual type of the object is, but in most cases, it shouldn’t matter to the UI layer. As long as the provided object implements that interface and works properly, so will the UI.

    When any class or method asks that an object of some type is passed to it, it should ideally always ask for the most narrow implementation or base type as is possible. So for instance, if you have a method that takes a list of items, it could look something like this:

    void printList(List<Dog> dogs)
    {
        foreach(Dog dog in dogs)
            printDog(dog);
    }
    

    However, technically, the method doesn’t really need a List<> object, because all it’s doing is enumerating through the items in the list. It’s not using any of the members of the List<> type, itself, it’s just using the members of the IEnumerable<> interface, which is implemented by the List<> type. Therefore, it would be better in this case to code the method like this:

    void printList(IEnumerable<Dog> dogs)
    {
        foreach(Dog dog in dogs)
            printDog(dog);
    }
    

    Now, if inside the method, it not only needs to enumerate through the list, but also needs the total count of items in the list, then IEnumerable is not enough. But still, it wouldn’t need the full List<> type, it just needs the ICollection<> type (which is also implemented by List<>), like this:

    void printList(ICollection<Dog> dogs)
    {
        printTotal(dogs.Count);
        foreach(Dog dog in dogs)
            printDog(dog);
    }
    

    But if the method neededs to get the index of each item, ICollection<> isn’t enough–the method would need an IList<> type. For instance, this example shows how it might print the list of dogs and highlight every other one in the list:

    void printList(IList<Dog> dogs)
    {
        printTotal(dogs.Count);
        for(Dog dog in dogs)
            if (dogs.IndexOf(dog) % 2 == 0)
                printDog(dog);
            else
                printHighlightedDog(dog);
    }
    

    You’ll notice that in none of these examples did I have to cast the dogs argument into a different type. I typed the argument appropriately for what I needed, so I just use it through that type’s interface. In all of these examples, a List<Dog> object can be passed into the printList method without casting (because List<> implements all of those interfaces so it can be cast implicitly):

    List<Dog> dogs = new List<Dog>();
    dogs.Add(new Dog());
    printList(dogs);
    

    Keep in mind, this is a very simplistic example to illustrate a point. In real world scenarios, you very often may just ask for List<> or IList<> even if you don’t technically need everything they offer, for various reasons. You may know that the method may very likely will need IList<> functionality in the future, even though it only needs the IEnumerable<> functionality today. But, the principle is a very good one to keep in mind as you decide on the types for arguments to your methods.

    You almost never need to know the actual type of an object that is passed into a method. All you really need to know is that it implements a particular interface so that you can access its functionality through that particular interface.

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

Sidebar

Related Questions

I am building a multi-tier application that will have multiple smaller apps apart from
Building a multi-language application in Java. Getting an error when inserting String value from
I am building a multi-layered application that has an ASP.NET MVC web application. It
I'm building multi-project Application where some UserControl, the user control has a Entitymodel object
I am building a multi-tenant application with shared database and shared schema approach. So
I'm building an entire application out of immutable objects so that multi-threading and undo
I'm building multi tenant application with shared table structure using Microsoft SQL Server. I
I'm building a multi-tenant web application where for security concerns, we need to have
Happy Friday SO! I'm building a multi-WinForm application and am having some troubles. I
I am building up a multi-threaded application where I spawn three threads when application

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.