Simple Spring.NET example

xiaoxiao2021-03-06  41

Simple Spring.NET example

Spring.net is the .NET transplant version of the Spring framework on Java, current version of RC2, contains full-featured IOC containers, and subsequent versions will contain AOP, ASP.NET, Remoting, and data access. IOC transfers the specific implementation of the interface of the interface, no longer use hardcodes, changed from the IOC container, and the IOC container reads the configuration file, and then instantiate the specific implementation of the interface by reflection, reaching the call caller and called called The decoupling of the person is more thorough than the plant mode. More information about IOC can refer to article http://www.martinfowler.com/articles/injection.html

Related Resources Download: http://www.springframework.net/ Program can also be downloaded from here http://sourceforge.net/projects/springnet/

This example demonstrates animal grazing, dogs eat meat, sheep, eating grass, examples may have improper, just for demonstration, please do not drill the horns

1. New project, reference Spring.core;

2, new two interfaces: IANIMAL and IFOOD

Public interface ianimal {string Eat (int qty); // How much eat}

Public interface ifood {string name // Food Name {Get; set;

String unit // Meter unit {GET;}

}

3, new two animals: Dog and Sheep

Public class dog: ianimal {private ifood _food; private string _name

Public Dog (String Name) // Dog Name {_Name = Name;}

Public ifood food // {set {_food = value;} get {returno;}}

Public String Eat (INT Qty) // Eat {Return String.Format ("DOG {0} ATE {1} {2} {3}", _ name, qty, _food.Unit, _food.name);}}

Public class sheep: ianimal {... // With Dog, if you like, you can refactor to use inheritance :)

Public String Eat (INT Qty) {Return String.Format ("CAT {0} ATE {1} {2} {3}", _ name, qty, _food.Unit, _food.name);}}

4. New two configuration files: app.config and sheep.config

App.config content:

an Example That DemonStrates Simple Ioc Features. wangcai < Property Name = "name"> Fresh Pig Meat

Sheep.config content: an Example That Demonstrates Simple Ioc Features. Dolly Green Grass

5, create a new form, put two buttons, a text box

Using spring.context; using spring.context.support;

Joining method: private void display (ketplicationContext CTX) {Try {iAnimal a = ctx.getObject ("Ananimal") AS IANIMAL; this.textBox1.text = a.eat (3);} catch (exception ex) {this.textBox1 .Text = ex.Message;}}

Buttons Event: Display (ConfigurationSettings.getconfig ("Spring / Context") AS IapplicationContext);

Display (New XMLApplicationContext ("Sheep.config"));

Attachment: If you need the original procedure, please go to the fuel tank csharpdpdp@163.com, delete it, put it in the deleted folder, password is CSHARPDP.

If you run out, you're an error and display ..spring.core strong name ... please run SN -VR *, 65E474D141E25E07

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.036, SQL: 9