|
8 | 8 |
|
9 | 9 | namespace UnitOfWork.NET.Classes |
10 | 10 | { |
11 | | - public class UnitOfWork : IUnitOfWork |
12 | | - { |
13 | | - private readonly ObservableCollection<Assembly> _assemblies; |
14 | | - private readonly IContainer _container; |
15 | | - |
16 | | - public UnitOfWork() |
17 | | - { |
18 | | - _assemblies = new ObservableCollection<Assembly>(); |
19 | | - |
20 | | - var cb = new ContainerBuilder(); |
21 | | - |
22 | | - cb.Register(t => this).AsImplementedInterfaces().AsSelf().As<UnitOfWork>(); |
23 | | - cb.RegisterType<Repository>().AsSelf().As<IRepository>().PreserveExistingDefaults(); |
24 | | - cb.RegisterGeneric(typeof(Repository<>)).AsSelf().As(typeof(IRepository<>)); |
25 | | - cb.RegisterGeneric(typeof(Repository<,>)).AsSelf().As(typeof(IRepository<,>)); |
26 | | - cb.RegisterGeneric(typeof(Repository<,,>)).AsSelf().As(typeof(IListRepository<,,>)); |
27 | | - |
28 | | - _container = cb.Build(); |
29 | | - |
30 | | - cb = new ContainerBuilder(); |
31 | | - |
32 | | - var fields = GetType().GetFields().ToArray(); |
33 | | - var properties = GetType().GetProperties().ToArray(); |
34 | | - |
35 | | - foreach (var type in fields.Select(t => t.FieldType).Union(properties.Select(t => t.PropertyType)).Where(IsRepository)) cb.RegisterType(type).AsSelf().AsImplementedInterfaces(); |
36 | | - |
37 | | - cb.Update(_container); |
38 | | - |
39 | | - foreach (var field in fields.Where(t => t.FieldType.IsAssignableTo<IRepository>())) field.SetValue(this, _container.ResolveOptional(field.FieldType)); |
40 | | - foreach (var property in properties.Where(t => t.PropertyType.IsAssignableTo<IRepository>())) property.SetValue(this, _container.ResolveOptional(property.PropertyType)); |
41 | | - |
42 | | - _assemblies.CollectionChanged += (sender, args) => RegisterAssembly(args.NewItems.Cast<Assembly>().ToArray()); |
43 | | - |
44 | | - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) _assemblies.Add(assembly); |
45 | | - } |
46 | | - |
47 | | - private bool IsRepository(Type t) |
48 | | - { |
49 | | - try |
50 | | - { |
51 | | - return t.IsAssignableTo<IRepository>() && !t.IsInterface && !t.IsAbstract && t != typeof(Repository) && ((t.IsGenericType && t.GetGenericTypeDefinition() != typeof(Repository<>) && t.GetGenericTypeDefinition() != typeof(Repository<,>) && t.GetGenericTypeDefinition() != typeof(Repository<,,>)) || !t.IsGenericType) && !_container.IsRegistered(t); |
52 | | - } |
53 | | - catch |
54 | | - { |
55 | | - return false; |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - private void RegisterAssembly(Assembly[] assemblyArr) |
60 | | - { |
61 | | - var cb = new ContainerBuilder(); |
62 | | - |
63 | | - cb.RegisterAssemblyTypes(assemblyArr.Where(t => t.GetTypes().Any(IsRepository)).ToArray()).Where(IsRepository).AsSelf().AsImplementedInterfaces(); |
64 | | - cb.Update(_container); |
65 | | - |
66 | | - foreach (var assembly in assemblyArr.SelectMany(t => t.GetReferencedAssemblies()).Select(Assembly.Load).Where(t => !_assemblies.Contains(t) && t.GetTypes().Any(IsRepository))) |
67 | | - _assemblies.Add(assembly); |
68 | | - } |
69 | | - |
70 | | - public void RegisterRepository<TRepository>() where TRepository : IRepository => RegisterRepository(typeof(TRepository)); |
71 | | - |
72 | | - public void RegisterRepositories(Type[] repositoryTypes) |
73 | | - { |
74 | | - var cb = new ContainerBuilder(); |
75 | | - |
76 | | - foreach (var repositoryType in repositoryTypes) |
77 | | - { |
78 | | - if (repositoryType.IsInterface || repositoryType.IsAbstract || _container.IsRegistered(repositoryType)) continue; |
79 | | - |
80 | | - if (repositoryType.IsGenericTypeDefinition) |
81 | | - cb.RegisterGeneric(repositoryType).AsSelf().AsImplementedInterfaces(); |
82 | | - else |
83 | | - cb.RegisterType(repositoryType).AsSelf().AsImplementedInterfaces(); |
84 | | - } |
85 | | - |
86 | | - cb.Update(_container); |
87 | | - } |
88 | | - |
89 | | - public void RegisterRepository(Type repositoryType) |
90 | | - { |
91 | | - RegisterRepositories(new[] { repositoryType }); |
92 | | - } |
93 | | - |
94 | | - private TRepository GetRepository<TRepository>() where TRepository : IRepository |
95 | | - { |
96 | | - RegisterRepository<TRepository>(); |
97 | | - |
98 | | - return _container.Resolve<TRepository>(); |
99 | | - } |
100 | | - |
101 | | - public TRepository CustomRepository<TRepository>() where TRepository : IRepository => GetRepository<TRepository>(); |
102 | | - |
103 | | - public IRepository<T> Repository<T>() where T : class => GetRepository<IRepository<T>>(); |
104 | | - |
105 | | - public IRepository<TSource, TDestination> Repository<TSource, TDestination>() where TSource : class where TDestination : class => GetRepository<IRepository<TSource, TDestination>>(); |
106 | | - |
107 | | - public IListRepository<TSource, TDestination, TListDestination> Repository<TSource, TDestination, TListDestination>() where TSource : class where TDestination : class where TListDestination : class => GetRepository<IListRepository<TSource, TDestination, TListDestination>>(); |
108 | | - |
109 | | - public virtual IEnumerable<T> Data<T>() where T : class => Enumerable.Empty<T>(); |
110 | | - |
111 | | - public virtual void Dispose() |
112 | | - { |
113 | | - _container.Dispose(); |
114 | | - } |
115 | | - } |
| 11 | + public class UnitOfWork : IUnitOfWork |
| 12 | + { |
| 13 | + private readonly ObservableCollection<Assembly> _assemblies; |
| 14 | + private readonly IContainer _container; |
| 15 | + |
| 16 | + public UnitOfWork() |
| 17 | + { |
| 18 | + _assemblies = new ObservableCollection<Assembly>(); |
| 19 | + |
| 20 | + var cb = new ContainerBuilder(); |
| 21 | + |
| 22 | + cb.Register(t => this).AsImplementedInterfaces().AsSelf().As<UnitOfWork>(); |
| 23 | + cb.RegisterType<Repository>().AsSelf().As<IRepository>().PreserveExistingDefaults(); |
| 24 | + cb.RegisterGeneric(typeof(Repository<>)).AsSelf().As(typeof(IRepository<>)); |
| 25 | + cb.RegisterGeneric(typeof(Repository<,>)).AsSelf().As(typeof(IRepository<,>)); |
| 26 | + cb.RegisterGeneric(typeof(Repository<,,>)).AsSelf().As(typeof(IListRepository<,,>)); |
| 27 | + |
| 28 | + _container = cb.Build(); |
| 29 | + |
| 30 | + cb = new ContainerBuilder(); |
| 31 | + |
| 32 | + var fields = GetType().GetFields().ToArray(); |
| 33 | + var properties = GetType().GetProperties().ToArray(); |
| 34 | + |
| 35 | + foreach (var type in fields.Select(t => t.FieldType).Union(properties.Select(t => t.PropertyType)).Where(IsRepository)) cb.RegisterType(type).AsSelf().AsImplementedInterfaces(); |
| 36 | + |
| 37 | + cb.Update(_container); |
| 38 | + |
| 39 | + foreach (var field in fields.Where(t => t.FieldType.IsAssignableTo<IRepository>())) field.SetValue(this, _container.ResolveOptional(field.FieldType)); |
| 40 | + foreach (var property in properties.Where(t => t.PropertyType.IsAssignableTo<IRepository>())) property.SetValue(this, _container.ResolveOptional(property.PropertyType)); |
| 41 | + |
| 42 | + _assemblies.CollectionChanged += (sender, args) => RegisterAssembly(args.NewItems.Cast<Assembly>().ToArray()); |
| 43 | + |
| 44 | + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) _assemblies.Add(assembly); |
| 45 | + } |
| 46 | + |
| 47 | + private bool IsRepository(Type t) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + return t.IsAssignableTo<IRepository>() && !t.IsInterface && !t.IsAbstract && t != typeof(Repository) && ((t.IsGenericType && t.GetGenericTypeDefinition() != typeof(Repository<>) && t.GetGenericTypeDefinition() != typeof(Repository<,>) && t.GetGenericTypeDefinition() != typeof(Repository<,,>)) || !t.IsGenericType) && !_container.IsRegistered(t); |
| 52 | + } |
| 53 | + catch |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void RegisterAssembly(Assembly[] assemblyArr) |
| 60 | + { |
| 61 | + var cb = new ContainerBuilder(); |
| 62 | + |
| 63 | + cb.RegisterAssemblyTypes(assemblyArr.Where(t => |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + return t.GetTypes().Any(IsRepository); |
| 68 | + } |
| 69 | + catch |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + }).ToArray()).Where(IsRepository).AsSelf().AsImplementedInterfaces(); |
| 74 | + cb.Update(_container); |
| 75 | + |
| 76 | + foreach (var assembly in assemblyArr.SelectMany(t => t.GetReferencedAssemblies()).Select(Assembly.Load).Where( |
| 77 | + t => |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + return !_assemblies.Contains(t) && t.GetTypes().Any(IsRepository); |
| 82 | + } |
| 83 | + catch |
| 84 | + { |
| 85 | + return false; |
| 86 | + } |
| 87 | + })) |
| 88 | + _assemblies.Add(assembly); |
| 89 | + } |
| 90 | + |
| 91 | + public void RegisterRepository<TRepository>() where TRepository : IRepository => RegisterRepository(typeof(TRepository)); |
| 92 | + |
| 93 | + public void RegisterRepositories(Type[] repositoryTypes) |
| 94 | + { |
| 95 | + var cb = new ContainerBuilder(); |
| 96 | + |
| 97 | + foreach (var repositoryType in repositoryTypes) |
| 98 | + { |
| 99 | + if (repositoryType.IsInterface || repositoryType.IsAbstract || _container.IsRegistered(repositoryType)) continue; |
| 100 | + |
| 101 | + if (repositoryType.IsGenericTypeDefinition) |
| 102 | + cb.RegisterGeneric(repositoryType).AsSelf().AsImplementedInterfaces(); |
| 103 | + else |
| 104 | + cb.RegisterType(repositoryType).AsSelf().AsImplementedInterfaces(); |
| 105 | + } |
| 106 | + |
| 107 | + cb.Update(_container); |
| 108 | + } |
| 109 | + |
| 110 | + public void RegisterRepository(Type repositoryType) |
| 111 | + { |
| 112 | + RegisterRepositories(new[] { repositoryType }); |
| 113 | + } |
| 114 | + |
| 115 | + private TRepository GetRepository<TRepository>() where TRepository : IRepository |
| 116 | + { |
| 117 | + RegisterRepository<TRepository>(); |
| 118 | + |
| 119 | + return _container.Resolve<TRepository>(); |
| 120 | + } |
| 121 | + |
| 122 | + public TRepository CustomRepository<TRepository>() where TRepository : IRepository => GetRepository<TRepository>(); |
| 123 | + |
| 124 | + public IRepository<T> Repository<T>() where T : class => GetRepository<IRepository<T>>(); |
| 125 | + |
| 126 | + public IRepository<TSource, TDestination> Repository<TSource, TDestination>() where TSource : class where TDestination : class => GetRepository<IRepository<TSource, TDestination>>(); |
| 127 | + |
| 128 | + public IListRepository<TSource, TDestination, TListDestination> Repository<TSource, TDestination, TListDestination>() where TSource : class where TDestination : class where TListDestination : class => GetRepository<IListRepository<TSource, TDestination, TListDestination>>(); |
| 129 | + |
| 130 | + public virtual IEnumerable<T> Data<T>() where T : class => Enumerable.Empty<T>(); |
| 131 | + |
| 132 | + public virtual void Dispose() |
| 133 | + { |
| 134 | + _container.Dispose(); |
| 135 | + } |
| 136 | + } |
116 | 137 | } |
117 | 138 |
|
0 commit comments