1. Preface
Recently, XComponent source code, fromCommonentityToXcode, And thenXcoderToXcode. I feel that I have made great progress, my vision is more open, and I can do many things more smoothly.
Big Rock5Several video tutorials were recorded around the month. One of them is "using object containers to decouple business modules and management platforms ".XThe components are very good. I have listened to this video before and after.4Of course, there are some new discoveries and experiences every time you listen. To solve the problemXcoderSome items found in the source code: interface operations.
2. xcode
Introduction to object containers
XcodeThe object container is used inXcodeBackground andNewlife. commonentityThere are a lot of use, which is one of the reasons why the background is easy to expand. You only need to inherit the corresponding entity class(Automatic Registration takes precedence over internal classes)In the base classCodeAll operations are performed through interfaces, so that the inherited classes are preferentially used by the system. The management platform and business platform are connected to the object container through interfaces.Object container registration interface priority:
1. object container registration takes precedence over the interface specified in the configuration file
2. After the configuration file is not found, allProgramSet to find the class to implement this interface, the priority is second only to the configuration file.
3. The internal implementation class has a lower priority,1,2If none of them are found, the default registration order is adopted.
The object container is registered in the static constructor. The service container base class is a generic base class, so that as long as it inherits the base class and then adds a static constructor, you can register the classes of Implemented interfaces in the program.
The above is a summary of the stone, which may not be easy to understand. The following example shows how it was studied last night.2The result of an hour.
3. xcoder
Interface operation instance
XcoderIn the code generator, you can save the database modelXMLFile, and can be directly loadedXMLFile to modify the model. However, some of them are inconvenient. I want to create an interface for directly adding models. This involves a problem. How can I instantiateIdatatableObject, ViewXcoderThe source code of has found the following:
1 Private Void Btnaddtable_click ( Object Sender, eventargs E) 2 { 3 If (Tables = Null | Tables. Count < 1 ) Return ; 4 5 Type type = tables [ 0 ]. GetType (); 6 If (Type = Null ) Return ; 7 8 Idatatable table = Typex. createinstance (type) As Idatatable; 9 If (Table = Null ) Return ; 10 11 Tables. Add (table ); 12 Table. ID =Tables. count; 13 Table. tablename = " Newtable " + Table. ID; 14 Table. Description = " Create a table " + Table. ID; 15 16 Settables (tables, tables. Count- 1 ); 17 }
The key code is: Type type = tables [0]. GetType (); hereDirectly obtain the type of the table in the current database, and then:
And idatatable table = Typex. createinstance (type) as idatatable; here, an interface is materialized and then assigned a value.
XcoderIt is advantageous to do so internally. This operation is performed only after the model is imported.IdatatableThe class has been determined, because there is an object container, seeXcodeservice
1 /// <Summary> Xcode service object provider </Summary> 2 Class Xcodeservice: servicecontainer <xcodeservice> 3 { 4 Static Xcodeservice () 5 { 6 VaR Container = Container; 7 Container. register <idatatable, xtable> () 8 . Autoregister <idatarowentityaccessorprovider, datarowentityaccessorprovider> () 9 . Autoregister <ientitypersistence, entitypersistence> () 10 . Autoregister <imodelresolver, modelresolver> (); 11 12 Dbfactory. Reg (container ); 13 14 Entityaccessorfactory. Reg (container ); 15 } 28 # Region Use 29 /// <Summary> Create model data table </Summary> 30 Public Static Idatatable createtable () 31 { 32 Return Container. Resolve <idatatable> (); 33 } 34 }
container. register (); is xcodeservice Register in static constructors idatatable default implementation class xtable , the following users The createtable () method returns a idatatable object, which can be used elsewhere in the system xcodeservice createtable method creation Idatatable the object is displayed.
Why is it not created like this in the top code?IdatatableWhat about objects? There are too many codes here, just a little bit, because the first code is already after the model is imported. During the Model import process, it has been determinedTablesThe Code is as follows:
1 /// <Summary> Import Model </Summary> 2 Public Static List <idatatable> Import (string XML) 3 { 4 If (String. isnullorempty (XML )) Return Null ; 5 6 Return Modelhelper. fromxml (XML, createtable ); 7 }
The method here is in the Dal class, and the second parameter of the fromxml method is a delegate. The createtable passed in here is to create the default idatatable instance. It is very simple to see his code, is the internal method to call xcodeservice once:
1 /// Create a data table object 2 internal static idatatable createtable () { return xcodeservice. createtable () ;}
It should be clear here,XcoderThe operations on the table inIdatatableWithout designing specific implementation classes, suchXtableIn external programs, you can use your ownTableClass, implementationIdatatableInterface.XcodeInternal implementation classes are preferred.
Finally, let's talk about it. This does not solve my problem. You may notice it here, Xcodeservice Class is private, Dal Of Createtable The method is Internal In this way, the external assembly cannot be accessed. If I want to obtain Idatatable Object, but it cannot be done, unless you rewrite Idatatable But this is too troublesome, since Xcode With this function, I don't know why Dal Of Createtable Set Public Are there any other considerations?