Is this way acceptable to do a C# singleton for use in ASP.NET webforms legacy app:
public class FileSingleton
{
private static readonly Lazy<FileSingleton> lazy =
new Lazy<FileSingleton>(() => new FileSingleton( new FileRepository()));
public static FileRepository Repository { get; private set; }
public static FileSingleton Instance { get { return lazy.Value; } }
private FileSingleton(FileRepository repository)
{
Repository = repository;
}
}
I created a repository for dealing with files in Azure. It has some overhead, so I don't want to instantiate it on every request. My design is based off of this post.
protected void Application_Start(object sender, EventArgs e)
{
HttpContext.Current.Items.Add("fileRepository", FileSingleton.Instance);
}
Does that seem reasonable? Is there a better way to do this?