Step by Step Guide to Cao Creation Through Sao Class Factories

xiaoxiao2021-03-06  62

Step by Step Guide to Cao Creation THROUGH SAO Class Factories.

Introduction

How do I instance a CAO object without shipping the source object? This is a question I hear often when people begin to work with CAO objects. I've heard this question often enough that I decided to put together an article describing step by step ( SO You Can Follow Along With Your OWN IMPLEMENTATION) ONE OF THE BETTER METHODS OF SOLVING This Problem. This Method Is Commonly Referred To As The Factory Pattern for CAO CREATION.

How to develop client activation objects (CAO) without issuing source code. Since this problem is often proposed, I decided to write an article to do a detailed introduction. I use the model of the class factory.

Remoting

Remoting is one of the great innovations of the new .NET framework. It allows for extremely powerful and easy to set up component linking over local or wide area networks. Things that could take days to write and debug in DCOM, COM, ATL, MFC Take minutes with .net and remoting.

Remoting is a revolutionary progress. Recall the remote object of the COM era, the nightmare is far away.

Remoting has several flavors depending on what you need. For this particular article I will be focusing on Client Activated Objects. Client Activated Objects are stateful objects that reside on the server with a proxy on the client. There are a few principal ways to instance a Client Activated Object, however I will be covering the most desirable of these which is the Factory Pattern for CAO's. The Factory Pattern is an MS recommended way of creating CAO objects without needing to ship the fully implemented object.

I've become familiar with CAO's due to some requirements at work for objects that take a very long time to authenticate against a very old legacy system. Re-authentication on every call would not be practical so I created CAO objects to hold the authenticated state. (Doing this securely requires a bit more trickery than I have time to explain in this article, but I will include them in a future article dealing with remoting objects). I also used CAO objects heavily when I created the GCS Version Control System which used CAO objects to deal with multi-call sessions that would otherwise require large amounts of data to be resent on every connection with Singlecall objects. I am trying to redesign that system though to use Singlecall objects.Despite how useful CAO objects can be I Would Be Remiss if I Didn't State That You Should Avoid Using Cao Objects if Possible. Sao SingleCall Objects Are A Much Better Choice for MOST Applications And Offer Much More Flexibility and Scalability. .

Background: Common Remoting Types

Server Activated Objects: Stateless - SingleCall

This object type is the most prevalent and is recommended by Microsoft for use as the primary remoting type. Server activated Singlecall objects hold no state, which makes them ideal for use with clustered web servers, object pooling, and all other sorts of useful things. Since there..

Server Activated Objects: Stateful - Singleton

These objects are used when you need both a stateful object AND you need that objects data to persist over time and multiple instantiation. If 3 computers instance an SAO Singleton on a server, they will all get a reference to that same object. Singletons are commonly Used to Direct Access To Scarce Resources and Should Generally Be Avoided When Possible.Client Activated Objects: Stateful

CAO Objects are used when you need an object to be stateful throughout its lifetime to its caller. CAO Objects persist data to their caller, however they are different from SAO Singletons in that multiple instantiations of a CAO will get brand new instances of that CAO each Time.

The Problem: Cao Creation without Distributing Cao Source

There are 3 primary ways of creating a CAO Object that I know of. Creating a CAO requires either a copy of the CAO class on the client, a cumbersome class def created through soapsuds, or the presence of an SAO class factory.

Pros Cons CAO Class visible to client The simplest method of CAO class creation. You must ship your entire CAO implementation with the client. Almost always an undesirable situation. Soapsuds class def creation Allows you to keep the implementation for CAO class hidden on server. Extremely cumbersome. Every time you change anything you must regenerate new class def. SAO CAO class Factory Allows you to keep the implementation for CAO class hidden on server. Only requires a share dll to allow instantiation on client. Once setup, is extremely flexible to change And Update. a little more complicated trion.

Sao Cao Class Factory

SAO Class Factory creation of CAO objects involves instantiation of an SAO Object (usually Singlecall, although for more complicated tracking reasons some people choose Singletons) which then is used to create a CAO object on the server and pass a reference back to the client. This fairly straightforward sequence produces a CAO object reference on the client that requires none of the CAO class code to reside on the client.For this example I am going to create a very simple CAO from an equally simple SAO Singlecall class factory. to start with we First Need to Create A DLL WE WILL CALL 'Sharedll.dll'. Also Create Two Console Applications Called Client and Server. Added Client and Server. Add these All to the Same SOLES Easily.

After WE Are Done Our Relevant Class and Exe Breakdown Will Be As Follows.

Step by step

Now That I've Explained The Basics, Lets Get Down To The Code. I am Going to Walk You Through The Steps of Going from 0 to a Working Cao from an Sao Class Factory.

Step 1: setting up definitions

Create a solution and add a class library (I called mine ShareDLL), a console app called Client and a console app called Server. Build the solution, then go to the ref section of Client and Server and add a reference to the ShareDLL to both . Also go into the class files for the client and server and add the using ShareDLL (assuming you used this name for your namespace on the ShareDLL). This will allow both the Client and Server to see the class definitions we will store in the ShareDLL .

Add two abstract classes "SAOCAOClassFactoryDef" and "CAOClassDef" the class library and inherit them from MarshalByRefObject to. These two classes will serve as the basis for the common types that both the client and server see. The classes are marked abstract because we will only ever use them as a definition that points to the actual implementation which will be in the server.Go to the server and add two corresponding classes "SAOCAOClassFactory" and "CAOClass" that inherit from their corresponding Def parents. These will serve as the implementation classes WHERE The Actual Work Goes on.

// SaoCaoclassFactoryDef.cs in Project Sharedll

Public Abstract Class SaocaClassFactoryDef: MarshalByrefObject

{

Public Abstract Caoclassdef CreatecaClass ();

}

// SaoCaoclassFactory.cs in Project Server

Public class saocaoclassfactory: sharedll.saocaoclassFactoryDef {}

// Caoclassdef.cs in Project Sharedll

Public Abstract Class Caoclassdef: MarshalByrefObject

{

Public Abstract Int IncrementCounter (INT INCSIZE);

}

// Caoclass.cs in Project Server

Public Class Caoclass: Sharedll.caoclassdef {}

Step 2: Fill in FunctionAlicity of Sao and Cao

Now that we have our classes defined, lets fill in the functionality. We first need to add a function to the SAO object that creates the CAO object. We do this by adding a function CreateCAOObject to the SAO object that instances the object on the server . Remember to add the Abstract definition for the function to the sharedll.

Lets also add some functionality to the object so we can test what makes a CAO unique. I added a variable _Counter in the CAO to show that it does not lose state between calls. And I added a method (IncrementCounter) that returns the current Value of the counter.// SaoCaoclassFactory.cs in Project Server

Public Class SaoCaoclassFactory: Sharedll.saocaoclassFactoryDef

{

Public Override Caoclassdef CreatecaClass ()

{

Return New Caoclass (); // Class Factory Create

}

}

// Caoclass.cs in Project Server

Public Class Caoclass: Sharedll.caoclassdef

{

protected int _counter = 0; // Server Side Persistent Variable

Public Override Int IncrementCounter (Int IncSize)

{

_COUNTER = INCSIZE;

Return_counter;

}

}

Step 3: Serve Your Objects

Now our objects are ready to be used so lets serve them. First we create a channel (I chose port 8675). After that you need to register the object with the remoting configuration. This will start the object listening.Remember, we are not serving the actual CAO object. We are serving a Singlecall version of the SAO object that we will use to instance our CAO object. This is an important distinction so do not be spooked by seeing all the SAO types with no reference to our CAO type .

[Code based option] ---------------------------------------------------------------------------

// Server.cs in Project Server

ChannelServices.RegisterChannel (New Tcpchannel (8675));

TYPE saotype = type.gettype ("Server.saocaoclassFactory, Server);

RemotingConfiguration.registerWellknownServiceType (saotype, "saocaoclassfactoryuri",

WellknownObjectMode.singlecAl1;

[Config file option] -------------------------------------------

// Server.exe.config

// Server.cs in Project Server

RemotingConfiguration.configure (@ "../../ Server.exe.config"; // Change thisime

You Go INTO PRODUCTION

Step 4: Instantiate your Objects, INSTANTIATE

. Now that we have the object available lets see about instancing it on the client Instancing the CAO object is extremely simple compared to the server because we merely need to call a method on the SAO object to get an instance of a CAO object.Note: I do not provide a config file option for the client because with only abstract objects visible to the client (by design) there is not any way to instantiate them with new. there are ways around this but they are a bit kludgy and get Away from the spirit of this article.

// Client.cs in Project Client

String url = "TCP: // localhost: 8675 / saocaoclassfactoryuri";

Console.writeline ("CREATING SAO.");

SaoCaoclassFactoryDef CF = (Sharedll.saocaoclassFactoryDef) Activator.GetObject (

TypeOf (Sharedll.saocaoclassFactoryDef), URL);

Console.WriteLine ("Creating Cao from Sao Class Factory!");

Caoclassdef mycao = cf.createcaoclass ();

Console.writeline ("Cao Created SuccessFully.");

// Output is 2

Console.writeline ("first call" mycao.incrementcounter (2) .tostring ());

// output is 5 (Proof That Object Held Previous State)

Console.WriteLine ("Second Call" Mycao.IncremeTcounter (3) .tostring ());

And that is all you need! You now Have A Working Cao Object Taken from An Sao Class Factory. Run The Server First, THEN Run The Client, The Output Should Look As Seen Below. Good Luck!

Remarks

In trying to keep this article on point I have kept things as simple and concise as possible. I am writing another article that drills deeply into some of the more interesting aspects of remoting objects in general including lifetime management, security tricks, callbacks, etc that I left out of this one.I've tested all the code several times to make sure I did not includey any errors, however if you do find any mistakes or have any questions, feel free to email me allen@glacialcomponents.com. Lastly, i considered address VB Examples As Well AS C # But I Didn't Know if The. If ENOUGH People Feel IT Would Be Helpful I'll Add Them.1

Cao

07/25/2004

DOOBIE

WAS this article difficult to completion? [Link]

1

VersionDateChangeDownloadbuy1.110 / 09 / 2003Updated Examples, Updated References and text. RemotInGexample.zip

转载请注明原文地址:https://www.9cbs.com/read-114562.html

New Post(0)