1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
class
TaxCalculator
{
private
decimal
_rate = 0.125M;
public
decimal
Rate
{
set
{ _rate = value; }
get
{
return
_rate; }
}
public
decimal
CalculateTax(
decimal
gross)
{
return
Math.Round(_rate * gross, 2);
}
}
|
1
2
3
4
5
6
|
WindsorContainer container =
new
WindsorContainer(
new
XmlInterpreter());
TaxCalculator calculator = container.Resolve<TaxCalculator>();
decimal
gross = 100;
decimal
tax = calculator.CalculateTax(gross);
Console.WriteLine(
"Gross: {0}, Tax: {1}"
, gross, tax);
Console.ReadKey();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<
configuration
>
<
configSections
>
<
section
name
=
"castle"
type
=
"Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"
/>
</
configSections
>
<
br
>
<
castle
>
<
components
>
<
component
id
=
"taxcalc.service"
type
=
"Windsor.Test.TaxCalculator, Windsor.Test"
>
</
component
>
</
components
>
</
castle
>
</
configuration
>
|
1
2
3
4
5
|
<
component
id
=
"taxcalc.service"
type
=
"Windsor.Test.TaxCalculator, Windsor.Test"
>
<
parameters
>
<
Rate
>0.25</
Rate
>
</
parameters
>
</
component
>
|
1
2
3
4
5
|
public
DateTime[] Holidays
{
get
{
return
_holidays; }
set
{ _holidays = value; }
}
|
1
2
3
4
5
6
7
8
9
10
11
|
<
component
id
=
"holidays.service"
type
=
"Windsor.Test.HolidayService, Windsor.Test"
>
<
parameters
>
<
Holidays
>
<
array
>
<
item
>2007-12-24</
item
>
<
item
>2007-12-25</
item
>
<
item
>2008-1-1</
item
>
</
array
>
</
Holidays
>
</
parameters
>
</
component
>
|
1
2
3
4
5
|
public
Dictionary<
string
,
string
> Aliases
{
get
{
return
_aliases; }
set
{ _aliases = value; }
}
|
1
2
3
4
5
6
7
8
9
10
11
|
<
component
id
=
"aliases.service"
type
=
"Windsor.Test.HolidayService, Windsor.Test"
>
<
parameters
>
<
Aliases
>
<
dictionary
>
<
entry
key
=
"dog"
>duck</
entry
>
<
entry
key
=
"ate"
>broke</
entry
>
<
entry
key
=
"homework"
>code</
entry
>
</
dictionary
>
</
Aliases
>
</
parameters
>
</
component
>
|
1
2
3
4
|
1
2
3
4
5
|
<
configuration
>
<
properties
>
<
myProperty
>Live</
myProperty
>
</
properties
>
</
configuration
>
|
1
2
3
4
5
|
<
component
id
=
"whatConfig.service"
type
=
"Windsor.Test.HolidayService, Windsor.Test"
>
<
parameters
>
<
Configuration
>#{myProperty}</
Configuration
>
</
parameters
>
</
component
>
|
1
2
3
4
5
6
7
8
9
10
|
<
component
id
=
"reader.file1"
type
=
"IoC.Tutorials.Part8.FileReader, IoC.Tutorials.Part8"
>
<
parameters
>
<
FileName
>file1.txt</
FileName
>
</
parameters
>
</
component
>
<
component
id
=
"reader.file2"
type
=
"IoC.Tutorials.Part8.FileReader, IoC.Tutorials.Part8"
>
<
parameters
>
<
FileName
>file2.txt</
FileName
>
</
parameters
>
</
component
>
|
1
2
3
4
|
WindsorContainer container =
new
WindsorContainer(
new
XmlInterpreter());
FileReader defaultReader = container.Resolve<FileReader>();
FileReader file1Reader = container.Resolve<FileReader>(
"reader.file1"
);
FileReader file2Reader = container.Resolve<FileReader>(
"reader.file2"
);
|
1
|
<component id=
"taxcalc.service"
type=
"Windsor.Test.TaxCalculator, Windsor.Test"
lifestyle=
"transient"
/>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
interface
IEncoder
{
string
Encode(
string
source);
}
public
class
NullEncoder : IEncoder
{
public
string
Encode(
string
source)
{
return
source;
}
}
public
class
SillyEncoder : IEncoder
{
private
char
[] _mixedUp =
"YACBDFEGIHJLKMONPRSQTUWVXZ"
.ToCharArray();
<br>
public
string
Encode(
string
source)
{
string
upperSource = source.ToUpper();
char
[] encoded =
new
char
[source.Length];
for
(
int
i = 0; i < encoded.Length; i++)
{
encoded[i] = MapCharacter(upperSource[i]);
}
return
new
string
(encoded);
}
<br>
private
char
MapCharacter(
char
ch)
{
if
((ch >=
'A'
) && (ch <=
'Z'
))
{
return
_mixedUp[ch -
'A'
];
}
return
ch;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
class
MessageSender
{
private
readonly
IEncoder _encoder;
private
readonly
string
_from;
public
MessageSender(
string
from, IEncoder encoder)
{
_from = from;
_encoder = encoder;
}
public
void
SendMessage(
string
to,
string
body)
{
Console.WriteLine(
"to: {0}\r\nfrom: {1}\r\n\r\n{2}"
, to, _from, _encoder.Encode(body));
}
}
|
1
2
3
4
|
WindsorContainer container =
new
WindsorContainer(
new
XmlInterpreter());
MessageSender sender = container.Resolve<MessageSender>();
sender.SendMessage(
"hammet"
,
"castle is great!"
);
Console.ReadKey();
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<
component
id
=
"encoder.silly"
service
=
"Windsor.Test.IEncoder, Windsor.Test"
type
=
"Windsor.Test.SillyEncoder, Windsor.Test"
/>
<
component
id
=
"encoder.null"
service
=
"Windsor.Test.IEncoder, Windsor.Test"
type
=
"Windsor.Test.NullEncoder, Windsor.Test"
/>
<
component
id
=
"messageSender"
type
=
"Windsor.Test.MessageSender, Windsor.Test"
>
<
parameters
>MessageSender
</
parameters
>
</
component
>
|
1
2
3
4
5
6
7
|
<
component
id
=
"messageSender"
type
=
"Windsor.Test.MessageSender, Windsor.Test"
>
<
parameters
>MessageSender
<
encoder
>${encoder.null}</
encoder
>
</
parameters
>
</
component
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public
class
SmsServiceFactory
{
private
string
_userName;
private
string
_password;
private
int
_retryAttempts = 3;
<br>
public
SmsServiceFactory(
string
userName,
string
password)
{
_userName = userName;
_password = password;
}
public
int
RetryAttempts
{
get
{
return
_retryAttempts; }
set
{ _retryAttempts = value; }
}
<br>
public
ISmsService CreateService()
{
SmsService service =
new
SmsService();
SmsService.SmsConfig config =
new
SmsService.SmsConfig();
config.SetCredentials(_userName, _password);
config.RetryAttempts = _retryAttempts;
service.SetConfig(config);
return
service;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<
castle
>
<
facilities
>
<
facility
id
=
"factorysupport"
type
=
"Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"
/>
</
facilities
>
<
components
>
<
component
id
=
"smsService.Factory"
type
=
"Windsor.Test.SmsServiceFactory, Windsor.Test"
>
<
parameters
>
<
userName
>joe</
userName
>
<
password
>secret</
password
>
</
parameters
>
</
component
>
<
component
id
=
"smsService.default"
type
=
"Windsor.Test.ISmsService, Windsor.Test"
factoryId
=
"smsService.Factory"
factoryCreate
=
"CreateService"
/>
</
components
>
</
castle
>
|
1
2
3
|
WindsorContainer container =
new
WindsorContainer(
new
XmlInterpreter());
ISmsService smsService = container.Resolve<ISmsService>();
smsService.SendMessage(
"+465556555"
,
"testing testing...1.2.3"
);
|