c# - Unity: Change default lifetime manager for implicit registrations and/or disable them -


the unity container automatically resolve type can figure out on own without need manual registration. that's in ways, problem have uses transientlifetimemanager type of resolution, while want containercontrolledlifetimemanager. can still register types singletons manually, of course, if forget, instead of getting unhandled exception @ startup, app launch , appear work. there bugs, possibly subtle, hard-to-diagnose ones, due fact there multiple instances of type that's meant singleton.

so question is: there way can either specify different default lifetime manager or disable default auto-resolution behavior , limit container types register myself (directly or own conventions)?

is there way can either specify different default lifetime manager

yes, can use container extension use different lifetime manager. see request configurable default lifetimemanager example.

or disable default auto-resolution behavior , limit container types register myself

yes, container extension can well.

first during explicit registration record buildkey of registration. before creating object check if buildkey explicitly registered.

public class registrationtrackingextension : unitycontainerextension {     private concurrentdictionary<namedtypebuildkey, bool> registrations =         new concurrentdictionary<namedtypebuildkey, bool>();      protected override void initialize()     {         base.context.registering += context_registering;         base.context.strategies.add(             new validateregistrationstrategy(this.registrations), unitybuildstage.precreation);     }      private void context_registering(object sender, registereventargs e)     {         var buildkey = new namedtypebuildkey(e.typeto, e.name);         this.registrations.addorupdate(buildkey, true, (key, oldvalue) => true);     }      public class validateregistrationstrategy : builderstrategy     {         private concurrentdictionary<namedtypebuildkey, bool> registrations;           public validateregistrationstrategy(concurrentdictionary<namedtypebuildkey, bool> registrations)         {             this.registrations = registrations;         }          public override void prebuildup(ibuildercontext context)         {             if (!this.registrations.containskey(context.buildkey))             {                 exception e = new exception("type not explicitly registered in container.");                 throw new resolutionfailedexception(context.buildkey.type, context.buildkey.name, e, context);             }         }     } } 

then add extension, register classes , resolve. if class not explicitly registered exception thrown.

iunitycontainer container = new unitycontainer(); // add container extension container.addnewextension<registrationtrackingextension>();  // register types container.registertype<myclass>(); container.registertype<imyclass, myclass>(); container.registertype<imyclass, myclass>("a");  // these succeed because explicitly registered container.resolve<imyclass>(); container.resolve<imyclass>("a"); container.resolve<myclass>();  // myclass2 not registered throw exception container.resolve<myclass2>(); 

Comments