Example of a simple ASP.NET MVC + NHibernate + Fluent with proper session handling? - nhibernate

I'm new to all of these technologies. I would like to see a simple (not over the top) example of how you would set up a project with these technologies. The most important being the proper NHibernate session handling (using HttpContext). Or we can build off of what I already have.
I've seen several examples of one piece or another but nothing with all of these technologies mixed. I'm having a tough time tying them together.
Right now I have an NHibernateHelper class (<-- see the pastebin link) that someone provided me as an example. I've modified it slightly. I think I need to do some stuff in my Global.asax.cs file but I'm not sure exactly what. I need to somehow initialize the NHibernateHelper by passing its constructor an Assembly... but what kind of Assembly? This is where I get really lost. What am I missing?

In your example, The Assembly parameter is the Assembly that contains all Mapping files (hbm.xml) or Mapping classes (fluent nhibernate).
I'do like to recommend you to read this Tutorial. This is how i usually do, create a new HttpModule that opens a new session once per request and binds it to the web context. At the end of the request the session is closed. This is another example of the same implementation,
although the post is written in pt-BR, the code is in english.

Related

Can the Properties (JavaBeans) already be used with Codename One?

I am interested in using Properties in my CodenameOne project, namely because properties can be observed. I searched and found this blog post which starts by announcing
We [Codename One committers] committed properties as a deprecated API
but then the blog post seems to tell it still could be in active development see
The code below is preliminary and the syntax/classes might change without warning
The code presented in the blog post is not testable on my project. Indeed the following code does not work :
public class User implements PropertyBusinessObject {
// Do stuff
}
because the PropertyBusinessObject interface does not exist in my project. How could the PropertyBusinessObject interface be defined, what should it extend ? And by the way are Properties already available ?
Thank you very much for helping me sorting this out in my mind!
Sure they've been available for a while. Just use Update Client Libs in the Codename One settings under the basics section. Once you do that you will have the latest libraries.
When you create a new project in the IDE it uses the libraries it has locally not necessarily the latest.

Unmarshaling SOS DescribeSensor response via JSONIX yields incomplete object

I am attempting to use jsonix to unmarshal xml response from an SOS DescribeSensor request. In the broader scope I am going to be using jsonix to unmarshal all responses from SOS, particularly 2.0. I noticed that the response uses SML or SensorML namespace and so I added the extra module dependencies and sub-dependencies (namely GML_3_1_1, SWE_1_0_1, IC_2_0, SMIL_2_0, SMIL_2_0_Language, and of course SensorML_1_0_1). Before I added these I noticed the return was a generic json (see first screenshot, particularly near sml:physicalsystem). After I added the dependencies I got an error in my console during part of the unmarshaling process which I do not understand (see second screenshot). Here is a link to the xml response from the server for reference. https://drive.google.com/file/d/0B8LdnPVJpHz7M3VGb0FZc2lQcjQ/view?usp=sharing. I would really like to understand if this has anything to do with the ordering of the modules when I create the context though I believe it is fine. Once the solution to this is discovered I have two follow up questions.
Is it reasonable to expect (in general) that using the modules built from the ogc-schemas on the highsource github page should allow me to handle all responses via jsonix? i.e. every element will always be mapped to a defined type. I know these schemas/mappings are very complicated.
Are there any other tools I can use to verify the modules or validate them against schemas to make life easier rather than tracking down elements on an individual basis or tracing through various module files when jsonix seems to parse incorrectly?
Thanks in advance - Richard3d
var context = new Jsonix.Context([XLink_1_0, GML_3_2_1, IC_2_0, SMIL_2_0, SMIL_2_0_Language, GML_3_1_1, SWE_1_0_1, SensorML_1_0_1, OWS_1_1_0, SWE_2_0, SWES_2_0, WSN_T_1, WS_Addr_1_0_Core, OM_2_0, ISO19139_GMD_20070417, ISO19139_GCO_20070417, ISO19139_GSS_20070417, ISO19139_GTS_20070417, ISO19139_GSR_20070417, Filter_2_0, SOS_2_0]);
Disclaimer: I am the author of jsonix and main dev of ogc-schemas.
First of all, you're on the right track, stay on it.
Yes, if you have all the required mappings then you should get a "nice" JSON with all the properties with specific types, cardinatilities etc.
The goal of Jsonix is to provide bi-directional XML<->JSON conversion with deterministic structure, types and cardinalities.
The goal of OGC Schemas is to provide JAXB and Jsonix mappings for all of the OGC Schemas.
So togethere these two should allow to transform any OGC XMLs from/to JSON.
"Generic JSON" was actually just DOM. If a property allows DOM and Jsonix does not have mapping for certain element, it is just taken as DOM. You were just missing SensorML mappings.
You're right the structure of schema dependencies is very complex. But this is something we should take to OGC. :) It's a bit crazy that you need, like, a dozen of schemas to read sensor data. I was actually intending to build automatic loading of dependencies but did not yet implement this feature.
The next GML_3_1_1.AbstractFeatureType problem is probably this issue. Try changing the order of mappings (move GML_3_1_1 to the earlier places). Actually the order of mappings should not be significant, but, well, there's a bug.
Tools to cross-check - no, probably not. My approach is to do roundtrip tests (unmarshal-marshal-unmarshal-check equality). From experience, there are normally a couple of caveats at the start, but then it works by design. Of course there are bugs in Jsonix and there may be problems with mappings, but this gets sorted out.
Also feel to create a support project here:
https://github.com/highsource/jsonix-support
For instance https://github.com/highsource/jsonix-support/s/sos.
Here's an example of such a support project:
https://github.com/highsource/jsonix-support/tree/master/l/lightstalker89
I need this because just downloading XML from Google Drive (a) takes me effort to set up the support project (b) legally dangerous as I have not idea where this XML comes from and if I have rights/license to add these files to my test suites.

ISessionFactoryKeyProvider missing from CommonServiceLocator when upgrading NHibernate from 2.1 to 3.0

I have a ASP.NET MVC application that uses Spring.NET 1.2 for dependecy injection and NHibernate 2.1 for ORM. It has been running on MVC 3, using S#arp Architecture in a slightly modified version which we have been compiling ourselves, something I am now striving to get rid of. (I.e. I am going to start using referenced SharpArch .dlls instead of pure source code.)
The initialization of Spring is done using tips taken from here, as well as using the SpringServiceLocatorAdapter found here. NH is initialized using SharpArch's NHibernateSession.Init, which is given our two assemblypaths. The mapping is done with HBM XML files.
The decision to move to NH 3 led to a serious case of DLL Hell, due to which I of course had to update FluentNH, NH Validator, SharpArch, Spring, and Castle Windsor binaries. To get things working a lot of manual labor was needed, changing references and doing minor code fixes. This was all expected.
Now I've got the software running well enough that Spring is initialized and the DI done correctly. But when a repository tries using it's Session object, I get the following error.
Error creating object with name '' :
no services of type
'SharpArch.NHibernate.ISessionFactoryKeyProvider'
defined
I've been trying to figure out the cause of this for the best part of a day now.
It seems to me like SpringServiceLocatorAdapter is obsolete, but I can find no information regarding that. Also, simply removing the use of it results in repositories' Sessions being null and thus NullReferenceExceptions. But as far as I know the problem could just as well be the way NH is initialized.
Hopefully someone can point me in the right direction. I'm having a hard time since I'm not the one who setup the system to begin with, and lists of breaking changes for the version upgrades are hard to find.
Thanks a lot! I will of course provide more information if needed.
You need to register an implmentation of ISessionFactoryKeyProvider with spring, the default implementation is DefaultSessionFactoryKeyProvider, in windsor, the statement would look like this:
container.Register(
Component.For(typeof(ISessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));

how to get knowing more about a class behaviour without looking at its manual?(a fundamental question about how to dive more into OOP)

I'm practicing OOP for 2 years (for real) and I know how to consume objects and packages and I'm developing stuffs mostly using C# .
but I have a problem with consuming unknown objects and packages as an instance :
for now I am working on an enterprise like website and for part of our job we need to consume RSS. I decided to use "System.Xml.Xpath"
and my real problem is:
for using system.xml.xpath I should look at manual and read it carefully and I don't want to do that every time.A plain example of that is like following code :
XPathDocument xp = new XPathDocument(sites[2]);
XPathNavigator nav = xp.CreateNavigator();
XPathNodeIterator it = nav.Select(xpath3);
foreach (XPathNavigator n in it)
{
//get elements here
}
//another way of iterating elements is
while(it.movenext())
{
//it.current.Value;
}
for the "foreeach" part I got it from MSDN manual and I guess I could get this simple fact by looking at class structure.
but I don't know which structure I should look.
I know how to read tooltips and I'm familiar with things like : [] / collection / enum /generic / Ienumerable / Idisposable etc...
but I think there is something about reading class behaviors and I'm missing that part.
for make it more lucid :
I know whenever we have a class that inherited from IEnumerable so we can use foreach statement against that class to iterate it through
my real problem is I think classes are self described enough to not to look at manuals all the time but I don't know how/where to read those descriptions of classes so I need your advice to get more familiar with how to reading classes without looking at manuals.
best regards.
Classes can (and should) be documented with source code comments, and in many languages you can generate API documentation from these comments (in HTML, XML or other format). In Java it is called Javadoc; I don't know the C# term. If this is what you call "manual", then this is your primary source of information. Other than reading the source code comments and the code itself (which you often don't have access to, especially in the MS universe). If you don't find enough information in the API documentation, you can always try googling for more explanation, tutorials or usage examples.
Hope this helps; I am not entirely sure I understood your question though. If this is not the answer you are looking for, please clarify.

Entity Validation in Sharp Architecture Repository

I have created a new 1.6 Sharp Architecture project.
I have marked my only Entity with HasUniqueDomainSignatureAttribute and one string property marked DomainSignatureAttribute.
I create 2 entities with the same DomainSignature and I'm able to save them both thous having duplicates.
Am I missing some configuration? As I was under the impression that this would work out of the box.
Before saving you should manually check entity for validness. I don't know how it is done now, but in previos versions each entitity had a property IsValid.
Well yes, the Validation method IsValid() is on the Entities.
But from the documentation I get the impression that if using NHibernate and NHibernate.Validators the repositories should validate the entities before Insert and Update.
As in the documentation on nhforge.org
In SharpArch.Data.NHibernate.NHibernateSession following snippet is called each time Init() is used.
Those things make me wonder why it doesn't work.
I could roll my own, but that seems like waste if it's already there.
Can anyone point me in the right direction? Also the SchemaExport util should use the validators when generating the scripts.