@@ -71,11 +71,11 @@ public AutofacMoqContainer() : this(new ContainerBuilder().Build()) {}
71
71
/// </returns>
72
72
public Mock < TService > Mock < TService > ( ) where TService : class
73
73
{
74
- var service = Try . Get ( ( ) => Create < TService > ( ) ) ;
74
+ TService service = Try . Get ( Create < TService > ) ;
75
75
var existingMock = service as IMocked < TService > ;
76
76
if ( existingMock != null ) return existingMock . Mock ;
77
77
78
- var mock = MoqRegistrationSource . Repository . Create < TService > ( ) ;
78
+ Mock < TService > mock = MoqRegistrationSource . Repository . Create < TService > ( ) ;
79
79
Update ( mock . Object ) ;
80
80
return mock ;
81
81
}
@@ -85,17 +85,25 @@ public Mock<TService> Mock<TService>() where TService : class
85
85
/// for all unregistered dependencies.
86
86
/// </summary>
87
87
/// <typeparam name="TService">The type of the service.</typeparam>
88
- /// <param name="activator">The optional activator.</param>
89
88
/// <returns>
90
89
/// The service instance.
91
90
/// </returns>
92
- public TService Create < TService > ( Func < IMoqContainer , TService > activator = null ) where TService : class
91
+ public TService Create < TService > ( ) where TService : class
93
92
{
94
- return ResolveOrCreate < TService > ( activator == null
95
- ? ( builder => builder . RegisterType < TService > ( )
96
- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) )
97
- : ( Action < ContainerBuilder > ) ( builder => builder . Register ( c => activator ( this ) )
98
- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ) ;
93
+ return Container . Resolve < TService > ( ) ;
94
+ }
95
+
96
+ /// <summary>
97
+ /// Creates an instance of the specified implementation (as the specified service),
98
+ /// injecting mocked objects for all unregistered dependencies.
99
+ /// </summary>
100
+ /// <typeparam name="TService">The type of the service.</typeparam>
101
+ /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
102
+ /// <returns></returns>
103
+ public TService Create < TService , TImplementation > ( ) where TService : class where TImplementation : TService
104
+ {
105
+ Update < TService , TImplementation > ( ) ;
106
+ return Create < TService > ( ) ;
99
107
}
100
108
101
109
/// <summary>
@@ -109,7 +117,7 @@ public TService Create<TService>(Func<IMoqContainer, TService> activator = null)
109
117
public IMoqContainer Update < TService , TImplementation > ( ) where TService : class where TImplementation : TService
110
118
{
111
119
UpdateWithBuilder ( builder => builder . RegisterType < TImplementation > ( ) . As < TService > ( )
112
- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
120
+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
113
121
114
122
return this ;
115
123
}
@@ -125,7 +133,7 @@ public IMoqContainer Update<TService, TImplementation>() where TService : class
125
133
public IMoqContainer Update < TService > ( TService instance ) where TService : class
126
134
{
127
135
UpdateWithBuilder ( builder => builder . RegisterInstance ( instance ) . As < TService > ( )
128
- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
136
+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
129
137
130
138
return this ;
131
139
}
@@ -141,17 +149,17 @@ public IMoqContainer Update<TService>(TService instance) where TService : class
141
149
public IMoqContainer Update < TService > ( Func < IMoqContainer , TService > activator ) where TService : class
142
150
{
143
151
UpdateWithBuilder ( builder => builder . Register ( c => activator ( this ) ) . As < TService > ( )
144
- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
152
+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
145
153
146
154
return this ;
147
155
}
148
156
149
157
/// <summary>
150
- /// Updates the container using the specified module.
158
+ /// Updates the container using the specified module.
151
159
/// </summary>
152
160
/// <param name="module">The module.</param>
153
161
/// <returns>
154
- /// The container.
162
+ /// The container.
155
163
/// </returns>
156
164
public IAutofacMoqContainer Update ( Module module )
157
165
{
@@ -160,11 +168,11 @@ public IAutofacMoqContainer Update(Module module)
160
168
}
161
169
162
170
/// <summary>
163
- /// Updates the container using the specified registration.
171
+ /// Updates the container using the specified registration.
164
172
/// </summary>
165
173
/// <param name="registration">The registration.</param>
166
174
/// <returns>
167
- /// The container.
175
+ /// The container.
168
176
/// </returns>
169
177
public IAutofacMoqContainer Update ( Action < ContainerBuilder > registration )
170
178
{
@@ -178,11 +186,5 @@ private void UpdateWithBuilder(Action<ContainerBuilder> registration)
178
186
registration ( builder ) ;
179
187
builder . Update ( Container ) ;
180
188
}
181
-
182
- private T ResolveOrCreate < T > ( Action < ContainerBuilder > registration )
183
- {
184
- if ( ! Container . IsRegistered < T > ( ) ) UpdateWithBuilder ( registration ) ;
185
- return Container . Resolve < T > ( ) ;
186
- }
187
189
}
188
190
}
0 commit comments