-
Notifications
You must be signed in to change notification settings - Fork 1
/
_IntroducingSystemLazy.cs
47 lines (42 loc) · 1.65 KB
/
_IntroducingSystemLazy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Ch07.Domain;
using Common.Interfaces;
using System;
namespace Ch07.Examples
{
public class _IntroducingSystemLazy : IBaseExecutor
{
public void Run()
{
//ConstructionLogicEncapsulatedInsideConstructor();
//ConstructionLogicEncapsulatedInsideConstructorUsingLazy();
ConstructionLogicPassedDelegateToLazy();
}
#region Construction Logic Encapsulated Inside Constructor Examples
private void ConstructionLogicEncapsulatedInsideConstructorUsingLazy()
{
Console.WriteLine("Creating Lazy object");
Lazy<DataWrapper> lazyDataWrapper = new Lazy<DataWrapper>();
Console.WriteLine("Lazy Object Created");
Console.WriteLine("Now we want to access data");
var data = lazyDataWrapper.Value.CachedData;
Console.WriteLine("Finishing up");
}
private void ConstructionLogicEncapsulatedInsideConstructor()
{
DataWrapper dataWrapper = new DataWrapper();
}
#endregion
#region Construction Logic Passed as a Delegate Examples
private void ConstructionLogicPassedDelegateToLazy()
{
Console.WriteLine("Creating Lazy object");
Func<Data> dataFetchLogic = new Func<Data>(() => LazyUsingDelegate.GetDataFromDatabase());
Lazy<Data> lazyDataWrapper = new Lazy<Data>(dataFetchLogic);
Console.WriteLine("Lazy Object Created");
Console.WriteLine("Now we want to access data");
var data = lazyDataWrapper.Value;
Console.WriteLine("Finishing up");
}
#endregion
}
}