XPO Development Guide
When the programmer develops an application, some data will always process, in many cases, you want to use some way to store them. There are many ways to implement, such as plain text files, XML files are entered into the database, and can even be some external data containers. However, if it is to process data that requires high performance reliability queries, you must use a relational database (such as Oracle, SQL Server).
On the other hand, we have to consider the programming language used by the application. Today's language is usually object-oriented, as used in .NET Framework, such as VB .NET, C #, and more. In these object-oriented languages, it is very likely to establish a realistic constructor and mapping of the program. At the analysis phase of IT engineering, an object-oriented model may be generated, such as a class diagram. All data we want to handle in the real world, all or more classes in object-oriented models. The characteristics of each item are converted into a particular class attribute (for example, the Name property of the Customer class). The object-oriented model mentioned above, in VB .NET, C # or other languages containing object-oriented features, the result is that the class set is instantiated as an object. For example, you have created an instance of a Customer class, and set this object's Name property to "John Doe" so you have a Customer object that represents John Doe on behalf of the real world.
These objects are stored in memory, so if the application exits or the computer is turned off, these objects disappear. In most cases, you probably want to store these objects in a place so that the data will not disappear, for example, on the hard disk. The use of XMLSerializer and XMLWRITER in .NET Framework can be easily implemented. The specific approach is: sequence the object to XML and write XML to the hard disk. This job is great to store data, even if it is a complex object tree, can also be serialized, then deserialized from XML, re-being an object. However, if you store a lot of data in an XML file, query data will become very slow. For example, you want to find a John Doe customer in 1000 serialized object pool, you need a reading until you find the need for the object. If you need high-performance query a lot of data, in most cases you want to use a relationship data management system (RDMS), such as Microsoft's SQL Server. In such a system, data is stored in an index table, which is associated with each other; a high performance complex query is possible. So we need to map a model to another again, this time is an object-oriented model map to a relationship model. After doing this, use the object model in our programming environment, and putting real data into a relational database, it is possible. This is where the O / R mapping system (object / relational mapping system) can play a role. An O / R mapping system is located between object-oriented models and relationship models, so it converts objects into relational data.
Reply to: kailler2002 (kriss) () Reputation: 93 2004-08-16 12:42:00 Score: 0
I have studied and using ORM for a year, using more than ten kinds of Framework
Objectspace in .NET 2.0 is also such a product
I think more mature is Obj.Net, OPF.NET and XPO.
But OJB.NET uses XML description, it feels more troublesome
Eventually XPO
And complete reference to a system sensation is really very convenient (especially when binding to WinForm, the input modification of the data is simple)
But in the end, I still gave up ORM and decided to return to the traditional SQL era.
Because XPO does have some performance issues, especially data getting more
There is also a lot of lists in practical applications, usually coming from a lot of tables (or objects).
If you use ObjectMapping, you will also read many unrelated data.
Hasty is right. The purpose of O / R mapping is that object-oriented systems can leave the relational database, using programmers without having to put too much energy on data access. HiBernate, who read Java, know that true O / R mapping, operates business objects (business entities or classes) in your code, is equal to the operation of the database. As for the stored procedure, you cannot correspond to an object in the system. If you join the business process during your stored procedure, the hierarchy is not very clear. Transactions in the stored procedure are replaced by other transaction processing. Or your intention is not to do an O / R mapping, but a data access layer?
Main topic: I have made an O / R mapping program, put my design idea to you: Helloparker (dark crystal)
First, you need a Southam Tong Database to manage all the sustainable objects. I call it Container. In my program, it is objectmapping.objectContainer, 嗬嗬, this idea original source is J2EE CMP (Of course, I don't have her). This name is called Container, responsible for root database communication, but also manages all sustainable objects, if you want her to help you find MM in your mind, you have to do this: mm mymmm = container.getObject (Typeof (mm) PrimaryKey) (This is the pull of ObjectSpaces, then do what to do with your mm ..., final, container.store (), OK, your MM has been persisted.
Second, persistent objects. How to last? Anyway, I have been in front, if you want to save an object's data, you will write insert, delete, update, so you don't have to use it, because all of my continuous object classes are a guy called objectmapping.orbase.orObjectBase. Children, he is an abstract class, there is no practical example (people are created by God) There is a magical feature in their genes in their own. - As long as you use this subclass according to a certain way (code You can use Visio to generate directly), then, as long as you pay attention to the object itself, such as mm.boyfriend = me, mm.kiss (me) ... 嗬嗬, the rest is handed over to Container, you are MM Anything to do is now a record, don't rely.
Again, there is a collection of persistent objects to help manage objects. This stuff is ObjectMapping.orbase.orObjectCollectionBase. This Dongdong is simple, such as: me.mms.add (PRETYMM), ME. MMS [99] .KISS (ME.MMS.Remove (Dragon): (. In order to facilitate everyone's next reading, I briefly introduced some key technologies mainly used in my program, if you are Master, help me check if there is any wrong place, if you don't quite understand, then you will read two stools. 1. Reflection If there is no reflection, I don't want to be a shit, anyway, I don't want to do so complicated Dongdong. The following is an explanation on the MSDN: By reflecting the class and system.Type, you can get information about the loaded assembly and the types defined in it (such as classes, interfaces, and value types). You can also Use the reflection to create a type instance at runtime, then call and access these instances For details: MS-Help: //ms.msdnqtr.2003feb.2052/cpguide/html/cpcondiscoveringTypeInformationAtRuntime.htm
Reflection is very useful, for example, I know an object of an object when I run, and then I want to get the attribute value. From the previous COM, I have a list of bacheloms, which is simpler in .NET, simple - mm.gettype (). GetProperty ("BoyFriend"). SetValue (mm, me, null), OK
Another very useful stuff in the reflection is Attribute,
2.XML, today's world, don't know how to make XML, not much to say.
3. Weak reference This is also a very useful stuff. Imagine such a situation, you have an ArrayList collection to reference to a variety of objects, later, ArrayList referenced by the Dongdong exceeded the role range, GC can be easy Let it treat it as garbage, but your arraylist also stays like a baby, it is a waste of resources! If you use WeakReference, it will be much. Here is a weak reference: http://www.9cbs.net/develop/read_article.asp? Id = 19987
The following is my design.
The first is ObjectContainer, this stuff is already briefly explained, now I am sure it's responsibilities: 1. Live with the database: Crud (Create, Retrieve, Update, delete is all its, good. 2. Management Sustaining Object: Since you get the Dongdong through Container, then Container should be responsible for it. The object is continuously continuous, and you should also be responsible for cleaning garbage after you don't have to use it.
The following is a few member functions I defined public orObjectBase CreteObject (Type Type, Object Primarykey) As the name, this function is used to create a sustainable object (please note, I don't have new MM, I don't want to have a new mm in the future. In
Public OrObjectBase getObject (Type Type, Object Primarykey)
This function is used to get something already, pay attention, only return one.
public ORObjectCollectionBase GetObjects (Type type, object condition) This function is to give a set, all satisfy condition stuff will be returned in the result, if the result of the condition is not met, ORObjectCollectionBase.Count == 0, such ORObjectCollectionBase mms = container .GetObjects (TypeOf (mm), "The most beautiful and love me" in the world), estimate, mms.count == 0: <. Public void setRelation (Object SecondObject, Object SecondObject, String Name, Bool Exist) This function is used to set the relationship between objects and objects, such as SetRelation (ME, MM, "Lianren", True, now MM is my GF. !
In addition, I believe me, I will definitely let the programmer rarely used the above functions (Getxxx exceptions, often use it), the programmer does not have to care for the setRelation at all. Another point, do you notice PrimaryKey? It is an Object type, which means that your primarykey can be string or a custom type (嗬嗬, is also more stronger)
Finally there is an important method of public void store (), nothing else, let everyone continue.
The main stuff in Container is above, and there are some details that there are also some details that they are saying. I still have to explain it when I use it. ============================================================================================================================================================================================================= =============================== Well, go to get off work, prepare to go home, write a little nonsense.
I want to write this thing, because I used EJB, CMP, really so convenient, and later .NET, I found no this stuff. So I thought I was doing myself, no matter what, I finally got a more similar to it. Now this thing is basically used, I feel very happy, but I feel that there is still a great improvement. I am a frog in the bottom of the bottom, this Dongdong may do it, but I still want to give it a lot, I hope to be better than I do better, I hope to be more .NETs. Friends, everyone will progress. Also, it is a bit description for the code. Because it is very busy, there is no time to write a lot of comments, so I want to see a friend, please forgive me, I will explain one by one, and the other code is inevitable. (Some possibly design) I hope that everyone can help me to remind me.
Oh, my email is helloparker@163.com next to talk about OOBJECTBASE, this class is all the base classes that require sustainable stuff, it is an abstract class, never have instances (someone has seen God? ?), But he has some very strange features that make it easy for future generations. He has the following main ways: protected objecty, objectty, object thevalue, protected objecty, objectty, object thevalue, these two methods should be put together, they are natural, before A method is used to set the attribute value, and the latter is used to get the attribute value (嗬嗬, is a nonsense), please take a look, they are all modified with protected, what will it? That means that your future generation can inherit this method, and others can't access it! Imagine if you use these two ways instead of a general GET, SET method, then what can you do? Only you know. Anyway, I use these two ways to achieve the sustaining effect. For example, you have a class mm, you need to continue, then public class mm: orObjectBase {public mm (ObjectMapping.objectContainer Container, statusenum status): base (container, status) {} // This is the primary key private string _id; public; public String id {get {return (string) getValue ("id", _ id);} set {_id = (string) setValue ("id", _ value);}} // This is the relationship field private boy _boyfriend; public Boy BoyFriend {Get {Return (Boy) getValue ("BoyFriend", _ BoyFriend;} set {_boyfriend = (boy) setValue ("BoyFriend", _ boyfriend;}}}
do you understand? The mystery is in getValue and the two methods of SetValue! As for why getValue and setValue have to make such a form, I will explain it later, still don't have to put your attention now.
Ok, orObjectBase is so simple, as long as you inherit him, and use getValue, setValue, your class can be continuous, there is nothing to worry.
The following is the wheel to orObjectCollectionBase, and it is also an abstract class. If there is a couple between your objects, or more related words, he should use him, he will help you do everything. You must inherit him first: public class mmcollection: orObjectCollectionBase {
Public mmcollection (Objectmapping.ObjectContainer Container, orObjectBase Tank, System.Collections.Arraylist Objs): Base (container, tank, objs) {}} If you are too lazy, this is ok, if you are very diligent, write this index Let the return type becomes mm, so it is fine, nothing else. OROBJECTCOLLECTIONBASE implements an ILIST interface, that is, you can use foreach to access, or to help him to top to DataGrid. . . ILIST can do (嗬嗬, interface is good), you need to specify this collection for your continuous flower class, so that when returning multiple objects, Container knows which collection class should be used. (If it is a lazy person, specify the unified collection class forget it, it is not an index to make a mandatory type conversion) 嗬嗬, now, do you think of what method to specify a collection class? I am using attributes, like this: [CollectionClass (mmcolction)] public class mm: orObjectBase {... Corrected top: Public class mm: orObjectBase {public mm (ObjectMapping.ObjectContainer Container STATUSENUM STATUS: BASE (container, status) {} // This is the primary key private string _id; public string id {get {return (string) getValue ("id", _ id);} set {_id = (string) setValue ("ID", value); // It turns out _ID = (string) setValue ("id", _ value);}}
// This is the relationship field private boy _boyfriend; public boyfriend {get {return (boy) getValue ("boyfriend", _ boyfriend;} set {_boyfriend = (boy) setValue ("boyfriend", value); // It turned out to be _BOYFRIEND = (BOY) SetValue ("BoyFriend", _ boyfriend;
}}}
Oh, the above has already introduced the three main types of usage, and it should now be the time to finish the idea. My idea is very simple: each entity class (borrowing EJB term) is roughly divided into such a few categories: 1. Fields indicating the actual data are mapped to the corresponding fields in the specific table in the database, also It is a field that actually continuously. These sub-sections should be some basic types. (Or, should it be a custom type? I think, but I don't have it, because it is too lazy.
2. Scriptures These fields should also be the same as the first field, but it can uniquely identify an object. In addition, in order to make programmers can have more flexible rooms, I design the primary key field into either basic type and can be a custom type.
3. The relationship between the relationship field classes and classes can be One2one, One2MANY, Many2one, Many2Many, which can be orObjectBase (One2MANY, or Many 2one ONE) type (if you want Processing One 2one as a Many2MANY to handle I don't mind) 4. Other types of fields These fields can start some auxiliary roles, basically I don't care.
Ok, after these types of fields, I think about how to achieve my goals. At GetValue and SetValue, you must first distinguish these types of fields, then divide them into different cases, or modify data (of course, data should be stored in memory until you call Container.Store " )), Or give another duration or object collection. Moreover, let all the properties are accessed through getValue, setValue, how to make them two one hundred? Use reflection! Also know which property is accessible, so the first parameters of them are string lines, that is, the name of the attribute of access! Take a look at Container, as mentioned earlier, can he manage the object to be continuous, decide how to continuous and implement, so it must have a message to each object. How to do it? I think, relatively simple way is to let Container create objects yourself, so you can solve the problem (huh, think about EJB's way, also a bit of this taste).
But I also need an auxiliary role to help Container and getValue, which knows how the properties of the continuous object can be found in the database, which is the relationship between the relationship between which table in the database is on.
This best role is ObjectMapping.orbase.ormap, which contains information about each continuous class. And each sustained class is represented by ObjectMapping.orbase.ORTYPEINFO, which contains detailed information, such as the type of the primary key, what is the name of the primary key, which field mapped to which table in the database, and other What is the relationship between class, etc. The details of each mapping field are also indicated by ObjectMapping.orbase.orfieldInfo (including database field name, corresponding class field name, and data type) relationship fields are represented by ObjectMApping.orbase.orRelationInfo (including The type information of the other side of the relationship, the relationship table, the main key of the two classes, the mapping above the table, etc.) Oh, there is a point to explain that I treat each relationship field as a query (huh, EJB has a dedicated object inquiry language, although I want to implement the kind of query language on EJB-CMP, but I have no time, so I will be lazy, I'm too easy to pull, as long as I have to compile The principle did not sleep, huh, huh, since there is a query, there is of course an object to represent the query (OO years), is ObjectMapping.orbase.orQuery, see the second parameters of getObjects? The statement is: Object condition is actually, it is obedient puller, it only accepts a type, it is ORQUERY (Oh, in order to leave later). Here, compare the getObject with getObjects, the second parameters of getObject are also object types, but it is primarykey is not condition! Although ORMAP is a supporting role, but without him, it will be in the labyrinth of O / R! The relationship between ORMAP and the class associated with him will be briefly described. Unfortunately, 9CBS can not map, or I will express it again with a UML static picture, but I have to describe it with text. If I don't know how to say, if the term is wrong, Trouble. There is a HashTable in ORMAP, which contains all sustained class ORTYPEINFO (more easy to retrieve in Haxi, if there is something wrong, troublesome) he publicly opens only this HashTable. And ORTYPEINFO is made from ORQuery, OrReletioninfo, orfieldInfo (here a little regret, hereby) The character should correspond to a HashTable in ORTYPEINFO (still find convenient, but Hashkey's choice is a headache) Oh, this ORMAP also Yes, but I think it is very dissatisfied with him. The reason is that when I designing this stuff, I want to keep the information in the XML file like this, so I thought about how to correspond to how to do it. Team with XML files, but ignore its essence - one picture! This is also a lesson, and future design must avoid this.