Unity + SharpRepository
最近在學習使用 SharpRepository,剛好專案中有使用 IoC Container - Unity, 所以需要把兩者做一個結合
不過,實際使用了 SharpRepository.Ioc.Unity
這個 Package,卻發現這個專案並沒實作到與 Unity 結合的部分,實際看了一下程式碼,才發現作者做了以下的註解:
using InjectionFactory I can get access to the container but I don't seem to ahve access to a context in the other Ioc's there is a context where I can get access to the type being resolved and get the generic arguments which is what i need
所以實際上裡面的程式碼直接就拋出 NotImplementedException
後來經過幾番確認與測試,最後終於找到方式了~ 程式碼如下:
1 | var RepositoryConfigurationName = "repository"; |
2 | // 結合 SharpRepository 與 Unity |
3 | RepositoryDependencyResolver.SetDependencyResolver(new UnityDependencyResolver(Container)); |
4 | Container.RegisterType(typeof(IRepository<>), |
5 | new PerResolveLifetimeManager(), |
6 | new InjectionFactory((c, t, n) => |
7 | { |
8 | var genericParams = t.GetGenericArguments(); |
9 | return RepositoryFactory.GetInstance(genericParams[0], RepositoryConfigurationName); |
10 | })); |
11 | Container.RegisterType(typeof(IRepository<,>), |
12 | new PerResolveLifetimeManager(), |
13 | new InjectionFactory((c, t, n) => |
14 | { |
15 | var genericParams = t.GetGenericArguments(); |
16 | return RepositoryFactory.GetInstance(genericParams[0], genericParams[1], RepositoryConfigurationName); |
17 | })); |
18 | |
19 | // 20140716 補充多主鍵的 Repository |
20 | Container.RegisterType(typeof(ICompoundKeyRepository<,,>), |
21 | new PerResolveLifetimeManager(), |
22 | new InjectionFactory((c, t, n) => |
23 | { |
24 | var genericParams = t.GetGenericArguments(); |
25 | return RepositoryFactory.GetInstance(genericParams[0], genericParams[1], genericParams[2], RepositoryConfigurationName); |
26 | })); |
重點就在註冊 Type 的類型上面,如果只有 IRepository<> 表示是單一的 Generic Type Argument,如果是 IRepository<,> 就表示有兩個 Generic Type Arguments~
老實說,真的很少人知道 C# 的 Generic Type 是這樣的~~~