Friday 15 April 2011

c# - Getting a singleton instance using SimpleInjector for a class which implements a generic type, not actually returning a singleton -


I have to face some strange code-issues, and in the end it has seen that as a singleton It is considered to be working in the form, actually there is not a singleton, it is a cash-class, so I'm ending multiple versions of the same cash. I have written some test codes below, and this should work in my eyes. Am I doing something wrong, or have I stumbled into a bug?

  Public Class GenericClassesNotRegisteredAsSingletonTest {Public Interface ICacheManager & lt; T & gt; {} Public Square SettingsData {} Public Category Settings Cache: CacheManager & lt; SettingsData & gt; {} Public Class Cash Manager & lt; T & gt; : ICacheManager & lt; T & gt; {} [Test] Public Zero Test () {var Container = New Container (); Var registration = Lifestyle. Signalton .CreateRegistration (typefacetch), typef (settingscatch), container); Container.Update registration (typef (ICK manager, & lt; settings data & gt;), registration); Container.Verify (); Var cache1 = container.GetInstance & lt; SettingsCache & gt; (); Var cache2 = container.GetInstance & lt; SettingsCache & gt; (); Bool sameRef = cache1 == cache2; Notice. That (sameRef == true); }}    

You made the following registration:

  _container.AddRegistration (service type: type (ICacheManager , registration: registration);   

And you are following the following solutions:

  _container.getInstance & lt; Settings cache & gt; ();   

You have not explicitly registered SettingsCache , but only ICacheManager & lt; SettingsData & gt; Since SettingsCache is a solid square, the simple injector will solve it for you as a transient example.

To resolve the solution either SettingsCache or ICacheManager & lt; SettingsData & gt; Instead, you can register another one using the same registration example, for example:

  _container.AddRegistration (service type: type (settingscatch), registration: registration);   

will warn you about this.

No comments:

Post a Comment