Use a property with a custom named backing field

3 views
Skip to first unread message

Mohamed Meligy

unread,
Mar 27, 2010, 1:43:55 PM3/27/10
to nhusers
I want to have a property like:

private DateTime _tweetUtcDate;
public DateTime TweetDate
{
    get
    {
        return _tweetUtcDate.ToLocalTime();
    }
    set
    {
        _tweetUtcDate = value.ToUniversalTime();
    }
}

I do this in order to do the UTC change as you see in the getter/setter.  That's why I want to keep the field name like this.

I can easily map only the field like:

<property access="field" name="_tweetUtcDate" type="System.DateTime">
  <column name="TweetUtcDate" sql-type="smalldatetime" not-null="true" />
</property>

But I still want to use the property "TweetDate" in queries, you know, like when I call LINQ queries, I want to be able to include the property name in select/where/orderby, etc..

Again, I could use something like "field.camelcase-underscore", but, again, I want to keep the naming to express the use.

So, is that doable? Can I use a backing field with a specific field name, not following specific naming convention?

* * Or is there a better way to achieve this easily ?

Regards,
--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies - TDG - Applications)
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

E-mail: eng.m...@gmail.com
Weblog: http://gurustop.net

Fabio Maulo

unread,
Mar 27, 2010, 3:16:16 PM3/27/10
to nhu...@googlegroups.com
yes it is.
You should implements your custom IPropertyAccessor and use it in the mapping.
After do that try to ask yourself why the private field should "express the use" and the public property shouldn't.


2010/3/27 Mohamed Meligy <mohamed...@gurustop.net>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Mohamed Meligy

unread,
Mar 27, 2010, 6:14:52 PM3/27/10
to nhu...@googlegroups.com
Because there is nothing special about the use of the property, you can use the property just like any other local (default) DateTime object you'd instantiate, so, it's nothing special.
This is different in the case of the field because it's not a local DateTime object, but a UTC DateTime, which needs to be expressed.

I thought it'd be some XML attribute in the mapping where I define the identifier of the field. Thanks for telling about property accessor.

Regards,
--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies - TDG - Applications)
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

E-mail: eng.m...@gmail.com
Weblog: http://gurustop.net


Robert Rudduck

unread,
Mar 28, 2010, 12:28:28 PM3/28/10
to nhu...@googlegroups.com
You are probably better off just putting a comment on the property name or a note in the class then going to all that work to change a name. Work with the tools not against them.

- Robert

Fabio Maulo

unread,
Mar 28, 2010, 1:45:21 PM3/28/10
to nhu...@googlegroups.com
2010/3/28 Robert Rudduck <rob...@rpowered.net>

 Work with the tools not against them.


Or find a tool that fit to you without change yourself ;) 
--
Fabio Maulo

Mohamed Meligy

unread,
Mar 28, 2010, 2:00:59 PM3/28/10
to nhu...@googlegroups.com
@Robert
Yeah, I totally agree. That was my thought when I read Fabio's first reply. My NH questions are usually to learn more than to overcome the very specific situation. Thanks a lot for the advice.

--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies - TDG - Applications)
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

E-mail: eng.m...@gmail.com
Weblog: http://gurustop.net


--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages