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

  • SEARCH
  • Home
  • 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 8860509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:16:50+00:00 2026-06-14T15:16:50+00:00

Every entity class in my application must implement the following interface: public interface IEntity<T>

  • 0

Every entity class in my application must implement the following interface:

public interface IEntity<T> {
    T Id { get; set; }
}

Almost 100% of the time the type of T will be an int. However I have to deal with cases were the id may be a composite id. E.g. I could have the following implementations:

public class User : IEntity<int> {
    public int Id { get; set; }
    ...
}

public class Site : IEntity<int> {
    public int Id { get; set; }
    ...
}

public class UserSite : IEntity<UserSiteIdentifier> {
    public UserSiteIdentifier Id { get; set; }
    ...
}

// Note: IIdentifier doesn't have any members
public class UserSiteIdentifier : IIdentifier {
    public User User { get; set; }
    public Site Site { get; set; }
    ...

    public override ToString() {
        return User.Id + "|" + Site.Id;
    }
}

Now given an entity instance (where the type is unknown) I need to retrieve the id and convert it to a string. I could say:

object entity = ???;
string id;

if (entity is IEntity<int>)
    id = ((IEntity<int>)entity).Id.ToString();
else if (entity is IEntity<IIdentifier>)
    id = ((IEntity<IIdentifier>)entity).Id.ToString();

But this code doesn’t sit right with me as I have to repeat almost the same code just to handle composite id’s.

I’d appreciate it if someone could show me a cleaner solution. This application is still a prototype and is completely open to suggestions. Thanks

  • 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-14T15:16:50+00:00Added an answer on June 14, 2026 at 3:16 pm

    You can introduce IEntity interface:

    public interface IEntity<T>:IEntity {
        new T Id { get; set; }
    }
    
     public interface IEntity {
        object Id { get; }
    }
    

    There will be some inconvenience while implementing two properties which are supposed to return the same value but this can be overcome by introducing base class Entity<T> which will hide the object Id property.

    public class Entity<T>:IEntity<T> {
        public T Id { get; set; }
        object IEntity.Id { get{ return Id;}  }
    }
    

    then your problematic code could be:

    object value =...;
    var entity = value as IEntity;
    string id;
    if(entity != null)
        id = entity.Id.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi every one I have these classe @Entity @Table(name = login, uniqueConstraints={@UniqueConstraint(columnNames={username_fk})}) public class
Is it possible to refresh only one entity in entity framework designer? Every time
Every time when I'm trying to load the .dll in my application, I'm getting
I've got an architecture like the following: Data (Class Library that handles our Entity
Every time i reset & seed my database it wipes out the standard admin@example.com
Every example I've seen on how to deploy an MVC application to Windows Azure
every time I open my Solution in Visual Studio it tries to communicate and
Every time I bind an AMQP queue to an exchange it automatically seems to
So, I created my entity model in a separate class library. I had to
I am having a problem with Entity Framework in my MVC 3 application. I

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.