using fluent nhibernate, is there anyway to have a private property mapped

后端 未结 3 438
心在旅途
心在旅途 2021-01-06 23:53

i have a field that i want to map as it links to a column in the database but i don\'t want any external class to access this field directly from my code (its only used in p

相关标签:
3条回答
  • 2021-01-07 00:10

    Yes it is possible. You can use the Reveal.Member static methods to map a private or hidden property

    Map(Reveal.Member<string>("privateProperty"))
    
    0 讨论(0)
  • 2021-01-07 00:13

    I think this is what you're looking for.

    Fluent mapping private properties

    There’s been a point of contention for some users of Fluent NHibernate since the beginning, and that’s the lack of a method of mapping private and protected properties on their domain entities.

    The issue stems from our use of lambda expressions for static reflection of your entities, one of the appealing properties of Fluent NHibernate; by utilising expressions we’re able to protect your mappings from refactoring side-effects. However, lambda expressions can only reference properties that are public on an object, so that makes it difficult to use against protected or private properties.

    None of the solutions we have are ideal, we’ll be the first to admit that; but considering Fluent NHibernate was never designed to support these situations, and the limitations C# imposes on us, we’ve got some pretty reasonable choices. Each option comes with its own compromises, so it’s important you pick the method that has the compromises you’re more willing to accept; I’ll outline the pros and cons of each approach.

    0 讨论(0)
  • 2021-01-07 00:16

    This an example from my project. My domain classes have only public properties. No setters. The object is created through the constructor and data is manipulated using methods.

    Entity

    public class User: Entity
    {
        private string _openid;
        private string _email;
        private string _username;
        private int roleid;
    
        // needed for mapping
        protected User() { }
    
        // your normal constructor
        public User(string openid)
        {
            Validator.NotNull(string openid, "openid is required.");
            _openid = openid;
        }
    
        public string Email
        {
            get { return _email; }
        }
    
        public string Username
        {
            get { return _username; }
        }
    
        public string Openid
        {
            get { return _openid; }
        }
    
       // Here are some methods
       // ... 
    }
    

    Mapping Class

    public class UserMap : ClassMap<User>
        {
            public UserMap()
            {
                Table("user");
    
                // Reveal private attributes and map them
                Id(Reveal.Member<User>("_id")).Column("id");
                Map(Reveal.Member<User>("_username")).Column("username");
                Map(Reveal.Member<User>("_openid")).Column("openid_claimed_identifier");
                Map(Reveal.Member<User>("_email")).Column("email");
    
                // You need to create this mapping if you want to query using linq, 
                //see UserRepository below
                Map(x => x.Id, "id").ReadOnly();
                Map(x => x.Email, "email").ReadOnly();
                Map(x => x.Username, "username").ReadOnly();
                Map(x => x.Openid, "openid_claimed_identifier").ReadOnly();
            }
        }
    

    Repository

    public class UserRepository : Repository<User>, IUserRepository
        {
            public UserRepository(ISession session)
               : base(session)
            {
            }
    
            public User find_user_by_openid(string openid_claimed)
            {
                return base.FindBy(x => x.Openid == openid_claimed);
            }
    
            public User find_user_by_id(int id)
            {
                return base.FindBy(x => x.Id == id);
            }        
    
        }
    

    One important thing to remember: do not use underscores within attribute names. Such as:

       public class User: Entity
        {
            //...
            private int role_id;
        }
    

    Fluent NHibernate does not like it when you use a particular attribute to map References.

    0 讨论(0)
提交回复
热议问题