locked
Property created through inheritance of a generic class doesn't exist RRS feed

  • Question

  • In my project I keep several class libraries for implementation of common functions. One of them has a class LoganBaseObject<T> 

    This class adds common columns to tables I design with code-first (primary key column, when and by whom the data was created/edited, etc).

    I implement this class through inheritance on the classes that represent the tables in my database, however, I'm very confused as it works fine on one of these classes, but not at all on another (or so it appears). See below:

    Here's LoganBaseObject<T>

    namespace Logan.Base
    {
        public abstract class LoganBaseObject<T>
            where T : LoganBaseObject<T>
        {
            [Key]
            public Int64 PKey { get; set; }
    
            public LoganBaseObject()
            {
                PKey = 0;
            }
        }
    }

    Below are the classes where I've implemented this. The implementation for Role works fine, but the User class doesn't get a PKey property...

    namespace Logan.Web.Objects
    {
        public class Role : LoganBaseObject<Role>
        {
            public string RoleName { get; set; }
    
            public virtual ICollection<User> Users { get; set; }
    
            public Role()
            {
                RoleName = String.Empty;
                Users = new List<User>();
            }
        }
    
        public class User : LoganBaseObject<User>
        {
            public string Username { get; set; }
            public string EmailAddress { get; set; }
            public string Password { get; set; }
            public string Biography { get; set; }
            public virtual Role Role { get; set; }
            
            public virtual ICollection<Article> Articles { get; set; }
    
            public User()
            {
                Username = String.Empty;
                EmailAddress = String.Empty;
                Password = String.Empty;
                Biography = String.Empty;
                Articles = new List<Article>();
            }
        }
    }

    I don't see any difference in either class inside Logan.Web.Objects so I can't understand why the PKey property exists on Roles but not on Users.

    I'd really appreciate it if anyone could have a look here and help me to resolve this. I can't move forward developing the system if this isn't resolved.

    Thanks in advance!


    I wanted to change the world, but no-one would give me the source code... Logan Young

    Wednesday, June 25, 2014 8:51 AM

Answers

  • Actually I misunderstood my own problem...

    The problem is that the way my application is structured, the properties in the User and Role classes aren't exposed to my mvc website... Still working on understand how best to expose them


    I wanted to change the world, but no-one would give me the source code... Logan Young

    • Marked as answer by Caillen Wednesday, July 2, 2014 8:24 AM
    Wednesday, June 25, 2014 11:11 AM

All replies

  • I can see it exists

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Wednesday, June 25, 2014 9:20 AM
  • Actually I misunderstood my own problem...

    The problem is that the way my application is structured, the properties in the User and Role classes aren't exposed to my mvc website... Still working on understand how best to expose them


    I wanted to change the world, but no-one would give me the source code... Logan Young

    • Marked as answer by Caillen Wednesday, July 2, 2014 8:24 AM
    Wednesday, June 25, 2014 11:11 AM
  • maybe this can help you a little bit : http://msdn.microsoft.com/en-us/library/e78dcd75.aspx

    check this line in the link : IList<string> roDinosaurs = dinosaurs.AsReadOnly();

    regards

    joon

    Wednesday, June 25, 2014 12:34 PM
  • Hi LoganY,

    If you have any further questions about ASP.NET, please post in ASP.NET forums.

    Thank you!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 2, 2014 8:23 AM
  • Hi,

    Having your base class be generic in respect to <T> is completely pointless if you dont actually use T.

    Having a class being generic in respect to itself is also somewhat strange. Why not just simplify to this:

    public abstract class LoganBaseObject
        {
            [Key]
            public Int64 PKey { get; set; }
    
            public LoganBaseObject()
            {
                PKey = 0;
            }
        }
    

    Personally speaking, I would not even bother with this base class. In OO terms You are creating a hierarchical relationship between User and Role, but probably these concepts are orthogonal. You should use sub-class to model "is-a" relationships. In this case, I would much refer to see the base class replaced with an interface, like this

    public interface IKeyed
    {
       long Key { get; set }
    }
    (Even then, I would be troubled by having a public setter on a primary key.)

    Regards,
    Nick.

    Wednesday, July 2, 2014 10:36 AM