Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES

ou can share code within your application by putting it Yinto classes within private assemblies. Organizing your

code with assemblies promotes reuse of code within your application so that you do not have to write the same code in several places. To update code, change it in only one place.

When you create applications on the .NET platform, you need to choose between creating components in private or shared assemblies. Creating components in private assemblies provides the simplest deployment strategy, which consists of just copying your application files to the destination where the application is to run. You did not have the capability of Xcopy deployment with Windows development before .NET.

The classes inside your private assemblies can contain public members that your client applications can access. These members include public properties, methods, and events. With properties, you can use get and set (read and write) to control what access the client has (read, write, or read/write). The properties that you implement with get and sets use protected members of the class to store property states, thereby enabling you to validate before setting or getting a property. Also, if you remove either of the get or the set, you can make a Write-only or Read-only property respectively.

To create a private component, you first start with a Class Library project in the Visual C# Projects templates list. A Class Library Application template is similar to the Console Applications template, except that the class file is scoped as Public and contains a constructor.

CREATE AN ASSEMBLY

Rename the class name to Photo.

Declare protected string variables for the category, filename, and title of the file.

ˇ Save the file.

Á Add a public function Photo for constructor logic.

Initialize the protected variables.

° Add a public function that returns the full description including the category, title, and filename formatted as a string.

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

You can simplify your code by using public fields for properties on your objects. Although public fields do come at a cost, you do not have any control over read/write access of the property, and validation can not be done before the property is replaced.

Example:

using System;

namespace PhotoAlbum

{

public class Photo_ex{ public Photo(){

// Constructor logic goes here.

}

public string GetFullDescription() { return "Catergory is " + Category +

"and title is " + Title +

"for the file " + FileName;

}

public string Category; public string FileName; public string Title;

}}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

· Create a public

Repeat steps 10 and 11

 

± Click Build Build

The server component is

 

 

 

property for the Category.

for the filename and title.

PhotoAlbum.

built.

 

Create the get and set

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

functions for the property.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CONTINUED

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

255

C#

CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES

ou can create applications rapidly by creating clients Yapplications that use your existing assemblies.

applications. Building applications with components has been proven effective in the development community. After creating your component, you can leverage that component in a client application.

In the case of private components, you need to include the component as part of the client application. Private Assemblies is not the same concept as components with COM, in the sense that the component is not shared with other applications. You will see, however, that sharing code across applications is possible (see page 260). The benefit you get from private assemblies that was also provided with

COM is having the ability to distribute the application into separate projects, enabling a team to work on separate parts of the application and later piece it together in a build of the application.

A private assembly can be used by any client application type: Console, Windows, or ASP.NET Web application. After you create the project, you set a reference to the component DLL, which has the assembly information built in. Next you reference the component’s namespace with the using statement. Then in code, you programmatically create an instance of the component and use its functionality.

CREATE A CLIENT

 

Rename the namespace

Create a new variable of

· Call the

 

to PhotoAlbum.

the type of object you want

GetFullDescription

 

Rename the class to

to create.

method to output the photo’s

 

° Set the category, filename,

properties to the console.

 

Client.

 

Set a debug stop.

the

ˇ Add the Main function.

and title for the Photo.

.

Á Save the file.

 

Click Project Add

 

 

 

 

Reference.

 

 

 

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

You can use collections to work with a group of the same classes. Collections are a common OOP approach to creating applications. The following code can be added to the project created in the numbered steps below to build a collection of photos. Add a new class to the project, call the class Photos.cs and then recompile.

Example:

namespace PhotoAlbum {

using System; using System.Collections; public class Photos : IEnumerable {

private ArrayList phtList; public Photos()

{phtList = new ArrayList(); } public void Add(Photo pht)

{phtList.Add(pht); }

public void Remove(int phtRemove)

{phtList.RemoveAt(phtRemove); } public int Count

{get{ return phtList.Count;} } public IEnumerator GetEnumerator()

{return phtList.GetEnumerator(); }

} }

PhotoAlbum.dll

The Add Reference dialog box appears.

± Click Browse.

¡ Click to select the bin\Debug directory for the server component created.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click the component.

 

A message appears showing

 

 

£ Click Open.

 

the properties set for the file.

 

 

 

 

¢ Click OK.

 

 

 

Press F5 to save, build, and run the console application.

CONTINUED

257

C#

CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES

fter creating your application with private assemblies, Ayou can use Xcopy deployment to install the

assembly. With private assemblies, you do not need to register components that the application uses. The components are discovered during the JIT compiling of the components. The issues with the Registry and DLL Hell go away. When using private assemblies for your component, the components deploy to the application directory by default and become visible only to the containing application. Because the components are discovered during JIT compiling, you can make updates on the components by just copying over the existing assemblies without unregistering and re-registering.

The process of deploying an application that only uses private assemblies is very simple. Just copy the client application and its dependencies from the output directory, which by default VS .NET builds to the bin\debug directory, and paste it to the destination client. In the case of the sample task below, you have one EXE file and one DLL to copy, and you deploy to another location on your PC’s hard drive. You can modify the directions given and deploy to another PC’s hard drive. In some cases, you can utilize a config file to help with locating dependencies. Because the dependent DLL is in the same directory as the client EXE, you do not need a config file.

DEPLOY AN APPLICATION

¤ Click the server and the client.

Right-click the file and click Copy.

Folder

New

 

Browse to a directory to

ˇ Right-click the directory

 

 

deploy the application.

window and click New

 

 

Folder.

 

 

Á Rename the folder

 

 

to an appropriate name.

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

Configuration files can provide paths that specify directories where the runtime should search for assemblies. See the <probing> element for an example of a redirection path. You can also redirect one assembly version to another by using the <bindingRedirect> element. The following example demonstrates how you can redirect to a newer version.

Example:

<configuration>

<runtime>

<assemblyBinding xmlns="urn: schemas-microsoft-com:asm.v1">

probing privatePath= "bin;binother\sub_bin "/>

<dependentAssembly>

<assemblyIdentity name= "yourAssembly"

publickeytoken=

"23ab4ba49e0a69a1"

culture=”en-us” />

<bindingRedirect oldVersion= "1.0.0.0"

newVersion="2.0.0.0"/>

</dependentAssembly>

/assemblyBinding>

</runtime>

</configuration>

Paste

Paste the files to the newly created folder.

° Open the command prompt and use the cd command to get to the deployment directory.

· Run the client application.

A message appears showing the properties set for the file.

259

Соседние файлы в папке c#