Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
<!--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />-->
</configSections>
<!--<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
--><!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL--><!--
--><!-- otherwise BasicConfigurer.Configure is used --><!--
--><!-- log4net configuration file is specified with key configFile--><!--
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>-->
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net" >
<description>An example that demonstrates simple IoC features.</description>
<object id="car"
type="SpringSample.Car, SpringSample">
<constructor-arg index="0" ref="engineInstance"/>
<property name="Engine" ref="engineInstance"/>
</object>
<object id="engineInstance"
type="SpringSample.ElectricEngine, SpringSample">
</object>
</objects>
</spring>
<!--<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level - %message%newline" />
</layout>
</appender>
--><!-- Set default logging level to DEBUG --><!--
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
--><!-- Set logging for Spring to INFO.
Logger names in Spring correspond to the namespace --><!--
<logger name="Spring">
<level value="INFO" />
</logger>
</log4net>-->
</configuration>
using Spring.Context;
using Spring.Context.Support;
using System;
namespace SpringSample
{
class Program
{
// Get the following via nuget:
// PM> Install-Package Spring.Core -Version 1.3.2
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
Car myCarCreatedWithSpring = (Car)ctx.GetObject("car");
myCarCreatedWithSpring.Drive();
}
}
public class Car
{
IEngine Engine { get; set; }
public Car(IEngine engine)
{
this.Engine = engine;
}
public void Drive()
{
Console.Write("Driving with a {0}hp engine. *Whooooooooosh*", Engine.Horsepower);
}
}
public interface IEngine
{
int RPM { get; set; }
int CurrentGear { get; }
int OilLevel { get; }
int Horsepower { get; }
}
public class ElectricEngine : IEngine
{
public int RPM { get; set; }
public int CurrentGear { get; private set; }
public int OilLevel {get { return 0; } }
public int Horsepower { get { return 120; } }
}
public class DieselEngine : IEngine
{
public int RPM { get; set; }
public int CurrentGear { get; private set; }
public int OilLevel { get { return 8; } }
public int Horsepower { get { return 250; } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment