My favorites | Sign in
Project Logo
             
Search
for
Updated Nov 23, 2008 by steven.burman
GettingStartedWithMVC  

Getting Started With MVC

RpxLib is designed to easily intergrate into an ASP.Net MVC application. Particularly if you are already using a IOC container to handle the instantiation of your controllers.

Using dependency injection

1. Setup your settings

First create an implementation of IRPXApiSettings. This can simply delegate to the provided RPXApiSettings class through its constructor or you can create your own implementation.

public class MyRPXApiSettings : RPXApiSettings {
    public MyRPXApiSettings() : base("myApiBaseUrl", "myApiKey") {}
}

or

public class MyRPXApiSettings : IRPXApiSettings {
    public string ApiKey { get { return "myApiKey"; } }
    public string ApiBaseUrl { get { return "myApiBaseUrl"; } }
    public IWebProxy WebProxy { get { return null; /* or your proxy object */ } }
}

2. Register with the container

Register the settings class (which is a dependency for the service) and the service itself with your container. For example using Autofac...

builder.Register<MyRPXApiSettings>().As<IRPXApiSettings>();
builder.Register<RPXService>().As<IRPXService>();

3. Inject the service as a dependency to your controller

Set up the service as a dependency for your controller and then let the container work its magic.

public class AuthenticationController : ControllerBase {
    private IRPXService rpxService;
    public AuthenticationController(IRPXService rpxService) {
        this.rpxService = rpxService;
    }
}

Without dependency injection

1. Manually create the dependency in the controller

In the constructor of the controller you can manually construct your service dependency by doing the following...

public class AuthenticationController : ControllerBase {
    private IRPXService rpxService;
    public AuthenticationController() {
        var settings = new RPXApiSettings("myApiBaseUrl", "myApiKey");
        this.rpxService = new RPXService(settings);
    }
}


Sign in to add a comment
Hosted by Google Code