What is AppServices?

Mama says: "Avoid referencing concrete classes, rely on abstractions". What does it mean? Instead of directly using DateTime.Now, use an abstraction - make an interface, i.e. IDateTimeProvider.Now and inject it with your class instance, i.e. AlwaysHighNoonProvider. Advantage? You are no more bound to the system time. It enormous simplifies testing of your app.

AppServices can inject services into other objects, visual controls or components. It manages automatically the service dependency. It works with .NET Full 2.0 or .NET Compact 2.0 and above, or Silverlight 3 and above.

AppServices is also a lightweight service container. It's like the built-in IServiceContainer/IServiceProvider from System.ComponentModel but it's much easier to use. Its advantage is declarative configuration of services (attribute oriented). This makes referencing, injecting and using services very easy.

If you're a friend of SOA - Service Oriented Architectures, and you need a lightweight container for your services, try AppServices - It's free of charge for any purpose.

Why use AppServices?

There is MEF - Managed Extensibility Framework in .NET 4.0. But if you work with earlier versions of .NET there is no MEF. Apart from - MEF is much more than a service container. You don't need to buy a brewery to drink a beer and you don't need to learn MEF only to inject services to your app.

ObjectBuilder from CAB - Composite Application Blocks, can also configure services in a declarative way, but be honest and tell me - how many times you desperately debugged your CAB-App and didn't know what depends on what. CAB is overloaded with functionality.

What are requirements of AppServices?

AppServices works with .NET 2.0 and above, it's tested with Windows Forms, ASP.NET, WPF and Silverlight (WP7).

How does AppServices work?

1. Add services to the container. (i.e. in program.cs)

using Polenter.Services;
AppServices.Instance.AddService<ITimeProvider>(new MyTestTimeProviderWithConstantTime());
// ... add further services


2. Create your objects and make placeholders for your services.

public partial class Form1 : Form
{
    // ...
    [Service] 
    private ITimeProvider _timeProvider;
    // ...
}

It doesn't matter if your services are hold in properties of fields, or if they are private or public. They only must be marked with the ServiceAttribute.


3. Inject the services to your object. (You can do this in the constructor)

    public Form()
    {
        InitializeComponent();
        AppServices.Instance.InjectServices(this);
    }

From now on, all fields/properties which are marked with ServiceAttribute are injected with declared services.


If your services depend on each other, you can inject themself:

    AppServices.Instance.InjectServices();

During injection AppServices automatically detects dependencies between services and chooses the right injection order.

Last edited May 19, 2011 at 11:23 AM by polo, version 6