@@ -71,11 +71,11 @@ public AutofacMoqContainer() : this(new ContainerBuilder().Build()) {}
7171 /// </returns>
7272 public Mock < TService > Mock < TService > ( ) where TService : class
7373 {
74- var service = Try . Get ( ( ) => Create < TService > ( ) ) ;
74+ TService service = Try . Get ( Create < TService > ) ;
7575 var existingMock = service as IMocked < TService > ;
7676 if ( existingMock != null ) return existingMock . Mock ;
7777
78- var mock = MoqRegistrationSource . Repository . Create < TService > ( ) ;
78+ Mock < TService > mock = MoqRegistrationSource . Repository . Create < TService > ( ) ;
7979 Update ( mock . Object ) ;
8080 return mock ;
8181 }
@@ -85,17 +85,25 @@ public Mock<TService> Mock<TService>() where TService : class
8585 /// for all unregistered dependencies.
8686 /// </summary>
8787 /// <typeparam name="TService">The type of the service.</typeparam>
88- /// <param name="activator">The optional activator.</param>
8988 /// <returns>
9089 /// The service instance.
9190 /// </returns>
92- public TService Create < TService > ( Func < IMoqContainer , TService > activator = null ) where TService : class
91+ public TService Create < TService > ( ) where TService : class
9392 {
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 > ( ) ;
99107 }
100108
101109 /// <summary>
@@ -109,7 +117,7 @@ public TService Create<TService>(Func<IMoqContainer, TService> activator = null)
109117 public IMoqContainer Update < TService , TImplementation > ( ) where TService : class where TImplementation : TService
110118 {
111119 UpdateWithBuilder ( builder => builder . RegisterType < TImplementation > ( ) . As < TService > ( )
112- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
120+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
113121
114122 return this ;
115123 }
@@ -125,7 +133,7 @@ public IMoqContainer Update<TService, TImplementation>() where TService : class
125133 public IMoqContainer Update < TService > ( TService instance ) where TService : class
126134 {
127135 UpdateWithBuilder ( builder => builder . RegisterInstance ( instance ) . As < TService > ( )
128- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
136+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
129137
130138 return this ;
131139 }
@@ -141,17 +149,17 @@ public IMoqContainer Update<TService>(TService instance) where TService : class
141149 public IMoqContainer Update < TService > ( Func < IMoqContainer , TService > activator ) where TService : class
142150 {
143151 UpdateWithBuilder ( builder => builder . Register ( c => activator ( this ) ) . As < TService > ( )
144- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
152+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
145153
146154 return this ;
147155 }
148156
149157 /// <summary>
150- /// Updates the container using the specified module.
158+ /// Updates the container using the specified module.
151159 /// </summary>
152160 /// <param name="module">The module.</param>
153161 /// <returns>
154- /// The container.
162+ /// The container.
155163 /// </returns>
156164 public IAutofacMoqContainer Update ( Module module )
157165 {
@@ -160,11 +168,11 @@ public IAutofacMoqContainer Update(Module module)
160168 }
161169
162170 /// <summary>
163- /// Updates the container using the specified registration.
171+ /// Updates the container using the specified registration.
164172 /// </summary>
165173 /// <param name="registration">The registration.</param>
166174 /// <returns>
167- /// The container.
175+ /// The container.
168176 /// </returns>
169177 public IAutofacMoqContainer Update ( Action < ContainerBuilder > registration )
170178 {
@@ -178,11 +186,5 @@ private void UpdateWithBuilder(Action<ContainerBuilder> registration)
178186 registration ( builder ) ;
179187 builder . Update ( Container ) ;
180188 }
181-
182- private T ResolveOrCreate < T > ( Action < ContainerBuilder > registration )
183- {
184- if ( ! Container . IsRegistered < T > ( ) ) UpdateWithBuilder ( registration ) ;
185- return Container . Resolve < T > ( ) ;
186- }
187189 }
188190}
0 commit comments