Castle Windsor is a mature Inversion of Control container available for .NET and Silverlight.
To Castle's official website
To Castle's Documentation Pages
Castle Windsor is available via NuGet
Use the "Manage NuGet Packages" and search for "castle windsor"
Use Package Manager Console to execute:
Install-Package Castle.Windsor
Now you can use it to handle dependencies in your project.
var container = new WindsorContainer(); // create instance of the container
container.Register(Component.For<IService>().ImplementedBy<Service>()); // register depndency
var service = container.Resolve<IService>(); // resolve with Resolve method
See official documentation for more details.
Castle.Windsor
package depends on Castle.Core
package and it will install it too
class Program
{
static void Main(string[] args)
{
//Initialize a new container
WindsorContainer container = new WindsorContainer();
//Register IService with a specific implementation and supply its dependencies
container.Register(Component.For<IService>()
.ImplementedBy<SomeService>()
.DependsOn(Dependency.OnValue("dependency", "I am Castle Windsor")));
//Request the IService from the container
var service = container.Resolve<IService>();
//Will print to console: "Hello World! I am Castle Windsor
service.Foo();
}
Services:
public interface IService
{
void Foo();
}
public class SomeService : IService
{
public SomeService(string dependency)
{
_dependency = dependency;
}
public void Foo()
{
Console.WriteLine($"Hello World! {_dependency}");
}
private string _dependency;
}