This project is read-only.

Controllers

Now that you have dependency injection running, you can start creating your controllers with arguments in the constructors, and if you've added the types as bindings, everything will be handled for you. To add additional bindings, you should create your own class/builder and add it to the list in the "How To" example.
Let's say you have your own service implemented that handles sending e-mails, and you need to send an e-mail in one of your controllers, this is what you need:

public class MyEmailService: IMailService {
	// ..Some implementation, crazy cool code!
}
public class EMailServiceBuilder : IDependencyBuilder {
	public void Configure(IDependencyContainer container) {
		container.Register<IMailService, MyEmailService>();
	}
	public void ValidateRequirements(IList<ApplicationRequirement> feedback) {
		// Maybe make sure the configuration of server and
		// username/password is present?
	}
}
public class EmailSendingController : Controller {
	public EmailSendingController(IMailService emailService) : base() {
	}
}

Simple, eh?

Other controllers

Now that everything is bootstrapped, you can inherit the BaseController and LocalizedBaseController classes instead of the regular Controller class. Both these classes resides in the MVCBootstrap.Web.Mvc.Controllers namespace. So what does this give you?

With the LocalizedBaseController you have access to the localization/globalization settings of the current request, meaning you can request texts and depend on them being properly formatted and in the right language (or any of the fallback languages).

With the BaseController you get access to the current, authenticated user (if any), and the BaseController also inherits from the LocalizedBaseController, so you can even give feedback to the user in his own language.

Examples:

public ActionResult Create() {
	// TODO: Just a sample, let's return an error message.
	// Localized text: Sorry {Username}, creating a new element failed.
	return CustomJson(new { message = this.GetLocalizedString("CreateFailed", new { Username = this.ActiveUser.Username }), status = false });
}

Sweet!

Last edited Jul 17, 2012 at 7:19 AM by steentottrup, version 3

Comments

No comments yet.