Giter Site home page Giter Site logo

autobox's Introduction

AutoBox - On the fly Dependency Injection and caching container

==================================================================

The tool dynamically injects dependencies based on convention. Also, let youcache repository calls using memcached(the most popular cross-platform caching system).

Getting Started

Lets take that you have a controller and the constructor looks like this :

public AccountCOntroller(IAccountService service)
{
	this.service = service;
}

AccountService is implemented in MyCoolWebSite.Services folder, in the global.ascx you first need to add the following line:

Container.Init();

Next you need to override the GetControllerInstance from DefaultControllerFactory. Since AutoBox is implemented using CommonServiceLocator you can directly include ServiceLocator.Current.GetInstance that will return the target controller with depencies properly injected.

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
        return ServiceLocator.Current.GetInstance(controllerType) as IController;
}

And at the end of the Global.ascx.cs you need to write this line, so that the request goes through AutoBox:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Now, moving forward there is a method in IProuductRepository lets say IProductReposiroty.GetAllProducts();

You just dont want to hit database all the time, unless invalidated. Therefore, you can further specify:

Container.Setup<ProductRepository>(x=> x.GetAllProducts()).Caches(TimeSpan.FromMinutes(10));

This lets AutoBox to store the data to memcache that will automatically invalidate after 10 minutes. However you can explictly specify the method that will invalidate it.

Container.Setup<ProductRepository>(x=> x.Create(Arg.Varies<Product>()).Invalidates(x => x.GetAllProducts());

Arg.Varies speicifies Any arguments. It can be also used for caching , this will act similar to VaryByParams.

Finally to make Memcached working you will need to have the following block in web.config (will be added automatically when installed from NUGet).

<?xml version="1.0" encoding="utf-8"?>
<configuration>

	<configSections>
			<section name="autoBox" type="AutoBox.AutoBoxSection, AutoBox" />
	</configSections>

	<autoBox cacheStore="localhost" cacheProvider="MemcachedProvider" />

</configuration>

Here you will notice that by default its pointing to localhost and MemcachedProvider. However it can be an external IP pointing to Amazon ElastiCache(released a few weeks ago).Here to note that memcached is a caching machanism and therefore the tool is agnostic of underlying cloud service that you may use.

To test caching locally, you can use the CouchBase Membase server (Not limited to), it gives you a nice web based GUI to monitor the cache usage, configure memory and clusters.

You can download it from this link:

http://www.couchbase.com/products-and-services/memcached

While under a router , i faced an issue in getting an IP, by default it useses IP_Addr.bat from %Program_Files%\Membase\Server\bin

But rather depending on it to resolve your IP which in my case my didnt work since my ip is dynamically assigned by router, you can update the follwoing line in service_register.bat:

Set IP_ADDR=127.0.0.1

Once you are done, you can run service_register.bat(from command line or just double click it) that will initalize and register the membase server as a local service.

Finally, you can double-click the "Membase Console" from desktop which will take you through an easy wizard.

Moving forward, there is a AutoBox.Specification project. You can use TestDriven.Net to verify that all the tests pass and you have configured things correctly.

This is an early preview, therefore there might be glitches and feel free to file them and raise questions.

Regards,

Mehfuz

autobox's People

Contributors

mehfuzh avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.