Dating Community Development _ IOC Application

Source: Internet
Author: User

DatingCommunityDeveloper _ IOC application 2009-12-9

Let's take a look at the current large-scale system applications. What problems have you encountered when talking about scalability?

(1) dependencies between classes; (2) interface dependencies; (3) Factory mode based on configuration files and reflection;

The dependency between classes has long been a new problem. Anyone who has done development knows that such dependency

The consequences are very serious. If you have any questions, you can refer to the OO principles. Let's see how to solve this problem using IOC.ProgramSet dependency

Using system;

 

 
Namespace Test
{
Public class enhellogenerator
{
Public String gethellostring (string name)
{
Return string. Format ("Hello, {0}", name );
}
}
}

The sayhello class holds a reference to cnhellogenerator and prints the generated greeting string.

Using system;
Namespace Test
{
Public class sayhello
{
Private cnhellogenerator _ hellogen;
Public cnhellogenerator hellogenerator
{
Get {return _ hellogen ;}
Set {_ hellogen = value ;}
}
Public void sayhelloto (string name)
{
If (_ hellogen! = NULL)
Console. writeline (_ hellogen. gethellostring (name ));
Else
Console. writeline ("client. Hello is not initialized ");
}
}
}

Mainapp.exe is responsible for creating, assembling, and calling objects:

Using system;
Namespace Test
{
Public class mainapp
{
Public static void main ()
{
Sayhello = new sayhello ();
Sayhello. hellogenerator = new cnhellogenerator ();
Sayhello. sayhelloto ("Hello World ");
}
}
}
Here we can see how strong the dependency between different classes is. If each class is distributed in different programming sets, the dependency between Assembly packages!
Is there any solution? Since dependencies between classes lead to too tight coupling, we need to rely on interfaces according to the design pattern theory. However, people often find that relying solely on interfaces does not seem to completely solve the problem?
Using system;
Namespace Test
{
Public interface ihellogenerator
{
String gethellostring (string name );
}
Public interface isayhello
{
Ihellogenerator hellogenerator {Get; set ;}
Void sayhelloto (string name );
}
Public class sayhello: isayhello
{
Private ihellogenerator _ hellogen;
Public ihellogenerator hellogenerator
{
Get {return _ hellogen ;}
Set {_ hellogen = value ;}
}
Public void sayhelloto (string name)
{
If (_ hellogen! = NULL)
Console. writeline (_ hellogen. gethellostring (name ));
Else
Console. writeline ("client. Hello is not initialized ");
}
}
}
Similarly, if they are distributed to different programming sets, the essential problems are not solved. When the Assembly changes, re-compile the program.
So what can be done to solve the dependency between assemblies? Assembly is a word in the traditional industry. Each assembly is considered as an accessory and then produced through a factory.
The production port assembles the required components. So only one assembly ticket is enough. Even if the production port function changes, the demand for a certain accessory is changed. You only need to modify the Assembly List.
The following is the app assembly list.
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Sectiongroup name = "test">
<Section name = "objects" type = "test. confighandler, mainapp"/>
</Sectiongroup>
</Configsections>
<Iocincsharp>
<Objects>
<Object name = "sayhello" assembly = "sayhello. dll" typename = "test. sayhello">
<Property name = "hellogenerator" assembly = "hellogenerator. dll"
Typename = "test. cnhellogenerator"> </property>
</Object>
</Objects>
</Iocincsharp>
Using system;
Using system. IO;
Using system. configuration;
Using system. reflection;
Namespace Test
{
Public class sayhellofactory
{
Public static object create (string name)
{
Assembly assembly;
Object o = NULL;
Object P;
String rootpath = path. getdirectoryname (assembly. getentryassembly (). Location) +
Path. directoryseparatorchar;
Configinfo using info = (configinfo) configurationsettings. getconfig ("test/objects ");
// Obtain the Assembly member
Objectinfo info = Response info. findbyname (name );
If (info! = NULL)
{
Assembly = assembly. LoadFile (rootpath + info. assemblyname );
O = assembly. createinstance (info. typename );
Type T = O. GetType ();
For (INT I = 0; I <info. properties. Count; I ++)
{
Propertyinfo prop = (propertyinfo) info. properties [I];

Assembly = assembly. LoadFile (rootpath + Prop. assemblyname );
P = assembly. createinstance (prop. typename );
T. invokemember (prop. propertyname,
Bindingflags. declaredonly |
Bindingflags. Public | bindingflags. nonpublic |
Bindingflags. instance | bindingflags. setproperty, null, O, new object [] {p });
}
}
Return O; // return interface instance
}
}
}
Assembly. LoadFile () Used to load external files; Assembly. createinstance () Creates an object of the specified type based on the loaded assembly; T. invokemember (prop. propertyname, ...... bindingflags. setproperty, null, O, new object [] {p }) Use the reflection mechanism to set attribute values for the created object.
Using system;
Namespace Test
{
Public class mainapp
{
Public static void main ()
{
Isayhello sayhello = (isayhello) sayhellofactory. Create ("sayhello ");
// Assembly list.
If (sayhello! = NULL)
Sayhello. sayhelloto ("Hello World ");
Else
Console. writeline ("got an error! ");
}
}
}

 

 

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.