Click here to Skip to main content
15,881,089 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.2K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Hosting;
using Signum.Utilities;
using System.Reflection;
using System.Resources;
using System.Globalization;
using System.Collections.Concurrent;
using System.Collections;
using System.Text;
using System.Web.Mvc;

namespace Signum.Web.PortableAreas
{
    public class EmbeddedFilesRepository: IFileRepository
    {
        public readonly Assembly Assembly;
        readonly string namespaceName;
        public readonly string VirtualPath;

        readonly Dictionary<string, string> resources;

        public EmbeddedFilesRepository(Assembly assembly, string areaName) :
            this(assembly, "~/" + areaName + "/", assembly.GetName().Name + "." + areaName.Replace("/", "."))
        {
            
        }

        public EmbeddedFilesRepository(Assembly assembly, string virtualPath, string namespaceName)
        {
            if (assembly == null)
                throw new ArgumentNullException("assembly");

            if (string.IsNullOrEmpty(virtualPath))
                throw new ArgumentNullException("virtualPath");

            if (string.IsNullOrEmpty(namespaceName))
                throw new ArgumentNullException("namespaceName");

            this.Assembly = assembly;

            this.VirtualPath = virtualPath.ToLower();
            this.namespaceName = namespaceName.ToLower();

            resources = this.Assembly.GetManifestResourceNames()
                .Where(name => name.StartsWith(namespaceName + ".", StringComparison.InvariantCultureIgnoreCase))
                .ToDictionary(name => name.Substring(namespaceName.Length + 1), StringComparer.InvariantCultureIgnoreCase);
        }

        public ActionResult GetFile(string file)
        {
            string resourceName = GetResourceName(file);

            if (resourceName == null)
                throw new FileNotFoundException("{0} does not belong to this repository".Formato(file)); 

            string actualResourceName = resources.TryGetC(resourceName);

            if (actualResourceName == null)
                throw new FileNotFoundException("{0} not found".Formato(file));

            return new StaticContentResult(this.Assembly.GetManifestResourceStream(actualResourceName).ReadAllBytes(), file);
        }

        public bool FileExists(string file)
        {
            string resourceName = GetResourceName(file);

            return resourceName != null && resources.ContainsKey(resourceName);
        }

        private string GetResourceName(string file)
        {
            file = VirtualPathUtility.ToAppRelative(file).ToLower();

            if (!file.StartsWith(VirtualPath))
                return null;

            return file.Substring(VirtualPath.Length).Replace("/", ".");
        }

        public bool IsEmpty { get { return this.resources.IsEmpty();  } }

        public override string ToString()
        {
            return "EmbeddedResources {0} -> {1}".Formato(namespaceName, VirtualPath);
        }
    }



}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions