Skip to content

Instantly share code, notes, and snippets.

@developerdizzle
Created March 31, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developerdizzle/c96b1402b3ebe74de06a to your computer and use it in GitHub Desktop.
Save developerdizzle/c96b1402b3ebe74de06a to your computer and use it in GitHub Desktop.
Lazy-loading virtual proxy example
public interface IMyService
{
int GetData();
string GetMoreData();
}
public class MyService : IMyService
{
private int _data;
private string _moreData;
public MyService()
{
_data = 123;
_moreData = "Some more data";
}
public int GetData()
{
return _data;
}
public string GetMoreData()
{
return _moreData;
}
}
public class MyServiceProxy : IMyService
{
private IMyService _service;
private void InitializeServiceIfNecessary()
{
if (_service == null)
{
_service = new MyService();
}
}
public int GetData()
{
this.InitializeServiceIfNecessary();
return _service.GetData();
}
public string GetMoreData()
{
this.InitializeServiceIfNecessary();
return _service.GetMoreData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment