Actually, depending on your needs, it might be a good idea to wrap all your
queries into
dedicated methods instead of allowing everyone to execute whatever query
they might like.
So instead of calling
HMUser[] hmUser = HMUser.FindAll( Expression.Eq("UserName", txtUsername),
Expression.Eq("Password", txtPassword.Text));
from your client code, do something like this:
class HMUser : HMBase<User>
{
...
public static IList<HMUser> FindUsersWithCredentials(string username,
string password)
{
HMUser[] users = HMUser.FindAll( // actually, FindOne makes more sense
here
Expression.Eq("UserName", username),
Expression.Eq("Password", password))
return new List<HMUser>(users);
}
}
this will make it easier to change the implementation later, e.g. if you
ever decide to make
it a real project, you would want to store hashed passwords instead of
cleartext. Then you
will only need to change this method instead of all the client calls.
regarding exception, make sure that your ActiveRecordBase<> class is always
specialized
with real AR type which will be persisted (in this case HMUser, and not
HMBase). The problem
was that AR was initialized with ActiveRecordBase<HMBase> so it would
internally try to
create instances of HMBase to return the from the methods, and since HMBase
is an abstract
type this had no chance of working. Even if you remove 'abstract' modifier
from the class it
will fail: you essentialy have HMUser derived from ActiveRecordBase<HMBase>
so during initialization
it looks for HMUser properties in HMBase class and fails.
On Tue, Jul 7, 2009 at 12:37 PM, Jiggr <[email protected]> wrote:
>
> Perfect!
>
> Thanks a lot Zdeslav! Everything works perfectly!
>
> I think it is a little bit annoying that all NHibernate methods return
> collections as arrays. I love lists and always convert it to that
> which gives me a lot of extra boring typecasts everywhere.
>
> On 6 Juli, 20:41, Zdeslav Vojkovic <[email protected]> wrote:
> > Right, there was an extra '}' when I was pasting from your post.
> >
> > To avoid casting to HMUser[] try modifying your hierarchy like this:
> >
> > [ActiveRecord]
> > abstract public class BaseHM<T> : ActiveRecordBase<T>
> > {
> >
> > }
> >
> > [ActiveRecord]
> > public class HMUser : BaseHM<HMUser>
> > {
> > }
> >
> > then HMUser.FindAll will return HMUser[]. This will also fix your
> exception.
> >
> >
> >
> > On Mon, Jul 6, 2009 at 6:10 PM, Jiggr <[email protected]> wrote:
> >
> > > Thanks Zdeslav!
> >
> > > The code closest to your code I can write without having compiling
> > > errors is this:
> >
> > > HMUser[] hmUser = (HMUser[]) HMUser.FindAll(
> > > Expression.Eq("UserName", txtUsername),
> > > Expression.Eq("Password", txtPassword.Text));
> >
> > > I must typecast it to HMUser[] because HMUser.FindAll will return
> > > BaseHM. And when I run this code I got an exception in runtime that I
> > > have described previously in this thread.
> >
> > > And I have added all my classes to the Initialize method in my
> > > global.asax. Have also tried your suggestion. :(
> >
> > > ---
> > > You have accessed an ActiveRecord class that wasn't properly
> > > initialized. The only explanation is that the call to
> > > ActiveRecordStarter.Initialize() didn't include HMBackend.BaseHM
> > > class
> > > ---
> >
> > > On 6 Juli, 11:49, Zdeslav Vojkovic <[email protected]> wrote:
> > > > try this:
> >
> > > > HMUser[] hmUser = HMUser.FindAll( Expression.Eq("UserName",
> > > txtUsername),
> > > > Expression.Eq("Password", txtPassword.Text)});
> >
> > > > read this criteria as "Expression.Eq("UserName", txtUsername) AND
> > > > Expression.Eq("Password", txtPassword.Text)"
> >
> > > > if you want logical disjunction (fancy name for OR) you need:
> > > > Expression.Or(
> > > > Expression.Eq("UserName", txtUsername),
> > > > Expression.Eq("Password", txtPassword.Text)
> > > > ) // obviously wrong for this use case
> >
> > > > regarding exception, have you registered all active record types
> (also
> > > the
> > > > derived ones)?
> > > > Here is how you can register all AR types inside the assembly:
> >
> > > > ActiveRecordStarter.Initialize(typeof(BaseHM).Assembly,
> > > > ActiveRecordSectionHandler.Instance);
> >
> > > > On Sun, Jul 5, 2009 at 7:45 PM, Jiggr <[email protected]> wrote:
> >
> > > > > I am trying to do a method that checks if the username and password
> to
> > > > > a user is correct from a login form.
> >
> > > > > I have wrote this code to get all users with all that username and
> > > > > password that the user has entered from the web form.
> > > > > ---
> > > > > HMUser[] hmUser = (HMUser[]) HMUser.FindAll(new
> > > > > NHibernate.Expression.ICriterion[] {
> > > > > new NHibernate.Expression.EqExpression("UserName",
> > > > > txtUsername),
> > > > > new NHibernate.Expression.EqExpression("Password",
> > > > > txtPassword.Text)
> > > > > });
> > > > > ---
> >
> > > > > For me it seems to be ok. There are lot of characters to do a task
> > > > > like that, does it exists a better way to do the same thing? Of
> course
> > > > > I can write "using NHibernate.Expression;" and things like that in
> the
> > > > > top of the file to get rid of all namespaces.
> >
> > > > > When I call the code above I always get this exception:
> > > > > ---
> > > > > You have accessed an ActiveRecord class that wasn't properly
> > > > > initialized. The only explanation is that the call to
> > > > > ActiveRecordStarter.Initialize() didn't include HMBackend.BaseHM
> > > > > class
> > > > > ---
> >
> > > > > I dont understand why. :( Because in my global.asax-file in the
> > > > > Application_Start-method I have added BaseHM to my Initialize call.
> >
> > > > > I can describe my system quick; all my business object inherits
> from
> > > > > HMBase.
> >
> > > > > For example this is my HMBase.cs:
> > > > > ---
> > > > > ...
> >
> > > > > namespace HMBackend
> > > > > {
> > > > > [ActiveRecord]
> > > > > abstract public class BaseHM : ActiveRecordBase<BaseHM>
> > > > > {
> > > > > ...
> > > > > }
> > > > > }
> > > > > ---
> >
> > > > > And this is my HMUser.cs
> > > > > ---
> > > > > ...
> >
> > > > > namespace HMBackend
> > > > > {
> > > > > [ActiveRecord]
> > > > > public class HMUser : BaseHM
> > > > > {
> > > > > ...
> > > > > }
> > > > > }
> > > > > ---
> >
> > > > > Is there an connection between the exception and my inheritance?-
> Dölj
> > > citerad text -
> >
> > > > - Visa citerad text -- Dölj citerad text -
> >
> > - Visa citerad text -
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---