Today, I encountered a problem that I couldn't understand when I wrote WebService. When I used Castle Query to execute hql, I couldn't return the System. String type. My code is as follows:
Public static string [] GetOrderRank (DateTime stime, DateTime etime)
{
Try
{
SimpleQuery query = new SimpleQuery (typeof (string ),
"Select o. Num12530 from Order o group by o. Num12530
Where o. UpdateDT between: start and: end ");
Query. SetParameter ("start", stime );
Query. SetParameter ("end", etime );
Return (string []) ExecuteQuery (query );
}
Catch (ActiveRecordException er)
{
Throw er;
}
}
Debugging program error, error Description: "You have accessed an ActiveRecord class that wasn' t properly initialized. the only explanation is that the call to ActiveRecordStarter. initialize () didn't include System. string class"
The basic meaning is: "The class without Initialization is accessed in the program. The ActiveRecordStarter. Initialize () method does not contain the class to be initialized (System. String )". I don't understand how the string type can be initialized first. Castle itself is. net open-source framework, and string is a type of IL, the compiler can directly identify, think about how to use System in the program. int32 is no problem. I have ruled out this possibility (it is absolutely not necessary to initialize the string data type first ).
At the beginning, I did not expect how to solve this problem. I directly changed the string type to the object type, and the program can run normally. I always calculated the tone, haha! However, the program is a little slow, because there are too many box and unbox operations, so I decided to look at it again.
So I checked the Castle document SimpleQuery () and reloaded it four times (the Castle version I used is RC2), as shown below:
- Public SimpleQuery (System. Type returnType, Castle. ActiveRecord. Queries. QueryLanguage queryLanguage, System. String query, params object [] positionalParameters)
- Public SimpleQuery (System. Type targetType, System. Type returnType, Castle. ActiveRecord. Queries. QueryLanguage queryLanguage, System. String query, params object [] parameters)
- Public SimpleQuery (System. Type returnType, System. String query, params object [] positionalParameters)
- Public SimpleQuery (System. Type targetType, System. Type returnType, System. String query, params object [] parameters)
I used the third overload version in the above program. I directly asked it to return the System. String program and reported an error. So I thought that I did not specify the target type for HQL operation?
I used another overloaded version of SimpleQuery (). The changes are as follows:
Public static string [] GetOrderRank (DateTime stime, DateTime etime)
{
Try
{
SimpleQuery query = new SimpleQuery (typeof (Order), typeof (string ),
"Select o. Num12530 from Order o group by o. Num12530
Where o. UpdateDT between: start and: end ");
// Omitted
}
Catch (ActiveRecordException er)
{
Throw er;
}
}
After System. Type returnType in SimpleQuery () is specified, the string [] Type can be returned directly! I'm so excited.
My understanding is: because I did not specify the target type operated by my HQL Statement (System. type targetType). Castle ActiveRecord treats the String Type as a user-defined model. It is different from the string Type in C #, and all prompts do not initialize this String. In fact, the String model does not exist at all. I didn't check the source code of Castle. I had to study it. I don't know if my explanation is correct. At least, I will solve this problem. I hope a friend can give me more explanations. I will go back and check it out.
To sum up, writing an article is really not easy. It takes a lot of time. However, I will continue to write it.
God help those who help themselives!
Henry