The content of this section
- Introduced
- Scenario 1: Adding directly
- Scenario 2: Using Components skillfully
- Example analysis
- Conclusion
Introduced
Through the previous 7 study, a bit boring ~ ~ ~ This article to learn a skill, we think together if I want to implement a FullName attribute in the customer class (that is, FirstName and LastName combination) How to do?
Scenario 1: Adding directly
"I know!" Modify the Customer class and add a FullName Property! namely customer.fullname! "
"Well, absolutely right ..."
This means that the FirstName and LastName two properties are modified in the customer class to be combined into FullName properties. In this case, if there are other classes (like vendor, Shiper) that use the FirstName and LastName two attributes, this requires a lot of business logic to be modified. So you're in big trouble, what's the way? ”
“.........”
Scenario 2: Using Components skillfully
NHibernate, the components (Component) and dynamic components are provided to help us do this thing. In fact, components are reused for different purposes in nhibernate. Here we use it to depend on the object .
Some of the properties of the,<component> element in the mapping file are mapped to some fields of the table corresponding to the parent class. Components can then define their own properties, components, or collections.
The following two images show the component and dynamic component two node mapping properties:
Look at these mapping properties:
- access ( Default property): NHibernate is used to access the properties of the policy
- class ( Default property type by Reflection): The name of the component (sub) class
- Insert: Whether the mapped field appears in the SQL INSERT statement
- Name: Property name PropertyName
- Update: Whether the mapped field appears in the SQL UPDATE statement
- < Property>: Establish a mapping between some properties of a component (child) class and a table field
- <parent> child elements: within a component class, you can have a reverse reference to an entity whose container is
The <dynamic-component> element allows a IDictionary to be mapped as a component, where the property name corresponds to the key in the dictionary. This is another usage of the component.
Knowing the knowledge above, we should think about the above question how to use the component to implement it.
Example analysis
We use a picture to show everything we say in this verse:
Let's get started!
1. New Name Class
namespacedomainmodel.entities{Public className{Public StringFirstname {Get;Set; }Public StringLastname {Get;Set; }Public StringFullname {Get{returnFirstname +" " + Lastname; } } }}
Simply put, this class is used to combine FullName attributes.
2. Modify the Customer class
namespacedomainmodel.entities{Public classCustomer{Public Virtual intCustomerId {Get;Set; }Public Virtual intVersion {Get;Set; }Public VirtualNameName {Get;Set; } }}
Modify the customer class, remove the original FirstName and LastName properties, and add the Name property. This is where name is an integral part of customer. It is important to note that, like the original FirstName and LastName properties, getter and setter methods need to be defined for the persisted property of name, but no interface or claim identifier fields need to be implemented.
3. Modify the customer mapping
<?XMLversion="1.0"encoding="Utf-8"?><hibernate-mappingxmlns="urn:nhibernate-mapping-2.2"Assembly="Domainmodel"namespace="Domainmodel"> <classname="Domainmodel.entities.customer,domainmodel"Table="Customer"> <IDname="CustomerId"column="CustomerId"type="Int32"Unsaved-value="0"> <Generatorclass="native"></Generator> </ID> <versionname="Version"column="Version"type="integer"Unsaved-value="0"/> <Componentname="Name"class="Domainmodel.entities.name,domainmodel"> < Propertyname="Firstname"column="Firstname"type="string"length=" -"Not-null="false"Unique-key="Uc_customername"/> < Propertyname="Lastname"column="Lastname"type="string"length=" -"Not-null="false"Unique-key="Uc_customername"/> </Component> </class></hibernate-mapping>
First define some properties of the component, specifying the property name and the class name of the component mapping. Then use the <property> child element to establish a mapping between the FirstName, LastName property of the name class and the table field. is not very simple ~ ~
In this case, the CustomerID, Version, Firstname, LastName fields are also in the Customer table. There is no need to modify the database table structure at all.
Here are two points to note:
- Just like all value types, a component does not support shared references. The value of the component is null and is semantics-specific. Whenever an object containing a component is reloaded, if all the fields of the component are empty, then NHibernate assumes that the entire component is empty. For the vast majority of purposes, this assumption is not problematic.
- The properties of a component can be NHibernate types (including collections, many-to-one associations, and other components). Nested components should not be considered as special applications. NHibernate tends to support the design of fine-grained object models.
4. How to Write
At this point, we need to modify or rewrite new methods to implement the logic we want.
PublicIList<Customer> Returnfullname (stringFirstNamestringLastName) {return_session. CreateQuery ("Select from Customer C where C.name.firstname=:fn and C.name.lastname=:ln") . SetString ("FN", FirstName). SetString ("LN", LastName). list<Customer> ();}
Now, we access the customer's FirstName, LastName properties, only need to be on the original basis through the name access, such as the above modification, to see the above image on how to access it, at a glance.
What if we want to add a customer? The code snippet looks like this:
CustomerName"yjing"};
5. Test method
With the method above, we write a test case to test this method: Look at the results test success, OK.
[Test]Public voidReturnfullnametest () {IList<Customer> Customers = _relation. Returnfullname ("Yjing","Lee");foreach(CustomerCinchCustomers) {Assert. AreEqual ("Yjing Lee", c.name.fullname); }}
Conclusion
This article describes a use of component techniques, through which components can improve our object model, and the database structure does not need to change. Through this technique, using components to map to rely on objects, can be very coherent introduction of nhibernate in the multi-table mapping relationships, collections and other content, these are the nhibernate in the highlights, even LINQ than it. From the beginning of the next chapter to learn the Shining spot in NHibernate.
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
NHibernate Tour (8): The dependent object for the skillfully used component