A custom tile repository
How to store your tiles on a different location

In this tutorial we will build our own repository to store our tiles in. The tiles will be stored in a seperate zip file. To achieve this we need to build:

  1. We need to build a resource locator.
  2. A factory to contruct resource locators.
  3. Configure our resource locator factory.

We start with the locator. A locator is used to retrieve resources for a certain path. It also embed functionality to handle relative resources.

public class ZipResourceLocator : IResourceLocator1
{
  private readonly ZipFile _file;
  private readonly String _prefix;

  public ZipResourceLocator(ZipFile file) : this(file, null){}

  public ZipResourceLocator(ZipFile file, String prefix)
  {
    _file = file;
    _prefix = prefix;
  }
  ...
  public byte[] GetData2(string name)
  {
    return _file[name];
  }

  public bool Exists2(string name)
  {
    return _file.Exists(name);
  }

    public string PreFixed3(string path)
{
    string prefixed = _prefix;
    if (!String.IsNullOrEmpty(prefixed))
    {
      prefixed = prefixed.EndsWith(".")
      ?
      prefixed
      :
      (prefixed + ".");
    } else {
      prefixed = "";
    }
    prefixed += path;
    return prefixed;
  }

  public IResourceLocator Update4(string path)
  {
    var newLocator = new ZipResourceLocator(_file, _prefix);
    return newLocator;
  }
}

  1. Any resource locator has to implement the IResourceLocator.
  2. The IResourceLocator demands some methods to be implemented. These are some examples. The most methods are there for acquiring resources.
  3. The PreFixed method prepends a path with an prefix. This way you'll get nicer paths in your application.
  4. The Update method can be used to implement relative paths. You could use the new path as a the prefix for your new locator. I did make this locator relative.

As you can see, writing your own locator isn't that hard. As you will see the factory isn't that hard either.

public class ZipLocatorFactory : IResourceLocatorFactory1
{
  private const string CONFIG_FILE = "tiles.xml";
  private static readonly ZipFile _zipFile;

  static ZipLocatorFactory()
  {
    _zipFile = new ZipFile();
  }

  public void Init(IConfiguration configuration){}

  public Stream GetConfiguration()
  {
    return GetLocator().GetStream(CONFIG_FILE);
  }

  public IResourceLocator GetLocator()
  {
    return new ZipResourceLocator(_zipFile);
  }

  public ITemplate Handle2(IEntry entry, bool throwException)
  {
    var locator = GetLocator();
    if (throwException || locator.Exists(entry.Value))
    {
      return new RefreshableResourceTemplate(locator, this, entry);
    }
    return null;
  }

  public DateTime? ConfigurationLastModified
  {
    get { return _zipFile.ResourceLastModified; }
  }
}

  1. Any resource locator has to implement the IResourceLocatorFactory.
  2. The Handle is most important method to implement. It creates a new locator an creates an resource template with it. You can create a refreshable template, RefreshableResourceTemplate or regular template, ResourceTemplate. Later on the "Update" method is used on the instantiated resource locator.

Only one thing remains. That is configuring the new locator factory.

...
<tilesConfiguration ConfigFilePath="my.tiles.xml"
            RefreshIntervalSeconds="15"
            ResourceFactory="ZipLocatorFactory,
                        CustomFileRepositories,
                        Version=1.0.0.0,
                        Culture=neutral,
                        PublicKeyToken=null"
/>
...

This is done in the Web.Config. Specify the full type qualifier of your factory into the ResourceFactory attribute and SharpTiles will use your locators.

Contact form fixed

I regret reporting that the contact form was broken for the last week. All mails send to us were corrupted(empty). So reported bugs in the last week are not received. I'm aware that the timing with regards to the monorail release isn't very handy. The contact form is fixed now. I apologize.

4 February, 2010

Monorail released M5

Monorail is released. The release also contains some minor fixes.

23 January, 2010

Monorail almost finished

Maikel Willemse is coding a Monorail view engine. It's available in the nightly release. A tutorial and a new release will be available shortly.

2 January, 2010

No more tiles.xml
(if you like)

A NSTL view engine is now available. You can do SharpTiles without having a tiles.xml. You will lose the power of dependency injection. If you are interested check out the tutorial page.

21 July, 2009

Developer M3 released

Fixed a couple of parse bugs. Cleaned up refreshing of resources, resource locations and registration of the TilesViewEngine. Wrote some tutorials
Ronald Dehuysser contributed the <tiles:insertTemplate/> and <tiles:putAttribute/> tag.

8 June, 2009

HtmlHelper wrapped II

Updated tag reference documentation.

5 April, 2009

Ready for MVC RC 2

SharpTiles is now ready to use with MVC RC 2.

6 March, 2009

HtmlHelper wrapped

The MVC HtmlHelper is now wrapped and can be used. For it's only available in the nightly build. When the documentation and new Tutorials are made a new release will be made.

22 December, 2008

Ready for Beta 1

SharpTiles is now ready to use with MVC Beta 1. The tutorial page will be updated soon.

3 November, 2008

We've got Tiles

It took me a bit longer than expected but Milestone 1 is reached. Tiles are now available.

23 July, 2008

First release

Finally, after some hard work my pet project is ready for its initial release. Here it is! Please give it some thoughts and feel free to add comments.

23 May, 2008