Infrastructure for enterprise development-factory services

Source: Internet
Author: User
The main function of factory services is to simplify the creation of specific factory tasks and make factory access Code And the factory creation code. Factory-like services are based on the abstract factory model and then integrated on it.
Before entering the topic, we need to clarify some concepts and set up the context to facilitate the subsequent description. Then, we will discuss it in this context.

First, there are two basic definitions: Family and series. (These two concepts are self-developed and I don't know what are commonly used terms. If you know them, please leave a message to tell me :))
(1) category refers to a specific type of item. For example, clothing is a kind of category, and food is a kind of category.
InProgramIn design and implementation, a family corresponds to an abstract factory, and our current system may involve multiple families.
(2) series-style refers to a style in a family. For example, the clothing brand can be regarded as a style, and a brand corresponds to a style.

To make it easier to understand, you can see the inheritance structure of ifacloud: ///   <Summary>
/// Ifacloud abstracts the basic interface of the factory interface. All Abstract Factory interfaces are inherited from this interface.
///   </Summary>
Public   Interface Ifacloud
{
///   <Summary>
/// Family name. You can determine the categoryname at the abstract factory level.
///   </Summary>
String Categoryname { Get ;} // For example, "clothing" or "food"

///   <Summary>
/// The series name and style name must be determined at the specific factory level.
///   </Summary>
String Stylename { Get ;} // For example, Nike and Li Ning
}

// Abstract Factory ifoodfactory, the specified
Public interface ifoodfactory: ifacloud
{< br> Apple getapple ();
rice getrice ();
}

//The specific factory chinesefoodfactory, style (series) has been determined
Public InterfaceChinesefoodfactory: ifoodfactory
{
}

Public InterfaceJapanesefoodfactory: ifoodfactory
{
}

Next, we need to provide the basic assumptions of the current system.
(1) A new category will not be introduced ). As long as a new family is introduced, the system must make major changes. Because the original system knew nothing about the new category.
(2) The system will not mix different styles in the same family. For example, there won't be a person wearing Li Ning's sportswear, But Nike's shoes :)

Next, we will be able to discuss factory-like services. The category Field Service has two main purposes:
(1) If the system used the style a series products in category A, change the system to style B, you should modify the configuration (for example, Change Factory A to factory B), or modify the relevant lines of code.
(2) Hide the difference between a remote factory and a local factory. In other words, the system does not need to care whether the referenced factory instance is local or remoting. You can also change the configuration to configure the original local object as a remote object without affecting our system.

To obtain a specific factory instance, You need to obtain information such as the name and location of the specific factory type, which is encapsulated by factoryinformation. ///   <Summary>
/// Factoryinformation is required when the corresponding factory is created through reflection
/// (1) If it is not remote, typename is the name of the specific factory type. If it is remote, typename usually abstracts the factory interface type, such as typeof (ifoodfactory ).
/// (2) The assemblyname parameter provides support for adding a new series to an existing family. You can place the new series in an independent DLL, and then modify the configuration,
/// Allows the system to use the objects of the new series.
///   </Summary>
Public   Class Factoryinformation
{
Private   String Factoryid; // Unique category factory id
Private   String Typename; // Type name of the category Factory
Private   String Assemblypath; // Accessories of the factory
Private   String Location; // Http: // remoteserver1/classfactory. Rem
Private   Bool Isincurrentassembly =   True ; // Whether it is in the current EXE accessory

Public Factoryinformation ( String Factory_id, String Type_name, String Asspath, String Loc, Bool Isincurassem)
{
This . Factoryid = Factory_id;
This . Typename = Type_name;
This . Assemblypath = Asspath;
This . Location = Loc;
This . Isincurrentassembly = Isincurassem;
}

# Region Property
# Region Isremoting
Public   Bool Isremoting // Remote or not
{
Get
{
If (( This . Location =   Null ) | ( This . Location. Trim () =   "" ))
{
Return   False ;
}

Return True;
}
}
# Endregion

# Region Isincurrentassembly
Public   Bool Isincurrentassembly // In the current accessory?
{
Get
{
Return   This . Isincurrentassembly;
}
}
# Endregion
# Endregion
}


The above Code omitted some simple attributes. The rest can be fully explained through annotations. It is necessary to point out that each factory has a unique factoryid. According to this identifier, we can require the class factory service infrastructure to create the specified factory object for us.

Factory service is a static class with the following appearance: Public   Class Classfactoryservice
{
Public   Static   Void Initialize (ifactoryinfogetter figetter );
Public   Static Ifactory getfactory ( String Factoryid );
}

There are only two static methods, one for initialization and the other for obtaining factory objects.
An ifactoryinfogetter parameter is required for initialization. ifactoryinfogetter is used to obtain factoryinformation of the specified factory. Generally, all factoryinformation is stored in the configuration file. The ifactoryinfogetter interface is defined as follows:Public InterfaceIfactoryinfogetter
{
Factoryinformation getfactoryinformation (StringFactoryid );
}

Classfactoryservice. initialize is generally called when the system is started. In this way, you can use the factory services normally. Let's take a look at the implementation of the key getfactory method and see how it achieves the two purposes of the class factory Service mentioned above. Public   Static Ifactory getfactory ( String Factoryid)
{
If (Classfactoryservice. factoryinfogetter =   Null )
{
Return   Null ;
}

Factoryinformation factoryinfo = Classfactoryservice. factoryinfogetter. getfactoryinformation (factoryid );
If (Factoryinfo =   Null )
{
Return   Null ;
}

Type factorytype =   Null ;
// Remote
If (Factoryinfo. isremoting)
{
Factorytype = Type. GetType (factoryinfo. typename ); // Factorytype is usually an abstract factory interface type.
Return (Ifactype) activator. GetObject (factorytype, factoryinfo. Location );
}

// Local, in the current accessory
If (Factoryinfo. isincurrentassembly)
{
Factorytype = Type. GetType (factoryinfo. typename );
Return (Ifactype) activator. GetObject (factorytype, Null );
}

// Local, not in the current accessory, but the target accessory has been loaded
Factorytype = Type. GetType (factoryinfo. typename );
If (Factorytype ! =   Null )
{
Return (Ifactype) activator. GetObject (factorytype, Null );
}

// Local, not in the current accessory, and the target accessory is not loaded
Assembly destassembly = Assembly. loadfrom (factoryinfo. assemblypath );
If (Destassembly =   Null )
{
Return   Null ;
}

Factorytype=Destassembly. GetType (factoryinfo. typename );
Return(Ifactype) activator. GetObject (factorytype, factoryinfo. Location );
}


Annotations have already explained everything that happened. The most important thing is to use simple reflection and remoting technologies. At this point, our task has been completed, but some precautions need to be raised:
(1) When a factory category is published on a remote server, the client can obtain its type information in the following three ways:
A. Deploy the interface assembly of the remote object to the client, that is, the client can obtain its abstract factory interface information.
B. Deploy the actual factory class to the client.
C. Deploy a group of "Empty classes" on the client, which implement the abstract factory interface and inherit from marshalbyrefobject. It can be completed through soapsuds.exe. (Recommended)

(2) If the class factory is configured as remoting, all the items produced by the class factory must be stored in financialbyrefobject.

I have already implemented so many factory-like services, and there are still many ideas that have not been implemented. I will discuss these new ideas with you later.

Home Directory for enterprise development infrastructure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.