I have a few classes which have a property which is unique to that object, for example for the User class the property Username should never be duplicated.
NHibernate doesn’t seem to support loading of an object by unique key which is then cached in the same way that Load() or Get() does. Am I correct when I say that?
If I am then I will just have to role my own via possibly an extension method along the lines of LoadByUniqueIndex(lambda property, object key).
Yes, you are right, NH doesn’t get entities directly from the cache for anything except the id. Note that everything except the id could potentially change and needs to be looked up in the database.
Be careful when caching. “Premature caching is the root of all evil” or whatever. Seriously, if you are not sure that you get notable performance problems, don’t write your own cache.
If you consider to have a static cache that lives as long as the application runs, I can tell you that you better avoid it at all. It will not properly work with a reasonable amount of effort.