-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathStorage.cs
More file actions
62 lines (52 loc) · 1.97 KB
/
Storage.cs
File metadata and controls
62 lines (52 loc) · 1.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright © 2017 Dmitry Sikorsky. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using ExtCore.Data.Abstractions;
using ExtCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace ExtCore.Data.EntityFramework;
/// <summary>
/// Implements the <see cref="IStorage">IStorage</see> interface and represents implementation of the
/// Unit of Work design pattern with the mechanism of getting the repositories to work with the underlying
/// Entity Framework storage context and committing the changes made by all the repositories.
/// </summary>
public class Storage : IStorage
{
/// <summary>
/// Gets the Entity Framework storage context.
/// </summary>
public IStorageContext StorageContext { get; private set; }
public Storage(IStorageContext storageContext)
{
if (!(storageContext is DbContext))
throw new ArgumentException("The storageContext object must be an instance of the Microsoft.EntityFrameworkCore.DbContext class.");
this.StorageContext = storageContext;
}
/// <summary>
/// Gets a repository of the given type.
/// </summary>
/// <typeparam name="T">The type parameter to find implementation of.</typeparam>
/// <returns></returns>
public TRepository GetRepository<TRepository>() where TRepository : IRepository
{
TRepository repository = ExtensionManager.GetInstance<TRepository>();
if (repository != null)
repository.SetStorageContext(this.StorageContext);
return repository;
}
/// <summary>
/// Commits the changes made by all the repositories.
/// </summary>
public int Save()
{
return (this.StorageContext as DbContext).SaveChanges();
}
/// <summary>
/// Asynchronously commits the changes made by all the repositories.
/// </summary>
public async Task<int> SaveAsync()
{
return await (this.StorageContext as DbContext).SaveChangesAsync();
}
}