c# - Unity: Conditional resolving -


Based on the specific data I received, I am looking to use Unity to solve the types of runtime. My code (as shown below) currently registers a type in a bootstrap class on start-up and then it is decided on the basis of any kind of need within the main flow.

What am I going to do that changes the code lines which use the keyword 'new' with the resolver, although this code is outside of my bootstrapper, I'm not sure how to do it May be .. I am new in unity so please be easy.

  // in bootstrapper square resolver Registration Type & lt; IDataType1, datatype 1 & gt; (); Resolver Registration Type & lt; IDataType2, datatype2 & gt; (); Resolver Registration Type & lt; IDataType3, datatype3 & gt; (); // Main Stream ... Output Bootstrap (Detector Type) {Case Detector Type Datatype 1: Datatype 1 DT1 = New Datatype 1 (); Dt1.ProcessData (); break; Case Detector Type Data Type 2: Datatype 2 DT2 = New Datatype 2 (); Dt2.ProcessData (); break; Case Detector Type Data Type 3: Datatype 3 DT3 = New Datatype 3 (); Dt3.ProcessData (); break; Default: Break; }  

You are missing some separation here. You are missing an abstract for the normal abstraction on your data types and for the implementation of those data types:

  // public interface IDtype {void ProcessData () in your main level; } Public Interface IDataTypeFactory {Create IDataType (DataRecordType dataRecordType); } // in bootstrapper square resolver Registry interface & lt; IDataTypeFactory & gt; (New DataTypeFactory); Resolver Registration Type & lt; IDataType1, datatype 1 & gt; (); Resolver Registration Type & lt; IDataType2, datatype2 & gt; (); Resolver Registration Type & lt; IDataType3, datatype3 & gt; (); Private sealed class datatypefactor: idatiffactor {private readonly iUnityContainer container; Public DataTypeFactory (IUnityContainer Container) {this.container = container; } Create Public IDataType (DataRecordType dataRecordType) {switch (dataRecordType) {case DataRecordType.dataType1: this return.Container.Resolve & lt; IDataType1 & gt; (); Case DataRecordType.dataType2: This return.Container.Resolve & lt; IDataType2 & gt; (); Case DataRecordType.dataType3: This is returned.Container.Resolve & lt; IDataType3 & gt; (); Default: Remove the new Invalid Emergency exception (); }}}  

What you can see is that the code has been moved to the factory to create an implementation, now the remaining application code can be something like this:

  // main stream ... with bootstrapper IDataType dt = this.dataTypeFactory.Create (dataRecordType); Dt.ProcessData ();  

IDataType1 , IDataType2 and IDataType3 are now used only in Bootstrap and have become unnecessary (Or at least, unnecessary with the code you have presented), so that you can delete them all at once and change the bootstrap logic in the following:

  // Bootstrap class resolver Registration entry & lt; IDataTypeFactory & gt; (New datatifference); Resolver.RegisterType & lt; DataType1 & gt; (); Resolver.RegisterType & lt; DataType2 & gt; (); Resolver.RegisterType & lt; DataType3 & gt; (); Private sealed class datatypefactor: idatiffactor {private readonly iUnityContainer container; Public DataTypeFactory (IUnityContainer Container) {this.container = container; } Create Public IDataType (DataRecordType dataRecordType) {switch (dataRecordType) {case DataRecordType.dataType1: this return.Container.Resolve & lt; DataType1 & gt; (); Case DataRecordType.dataType2: This return.Container.Resolve & lt; DataType2 & gt; (); Case DataRecordType.dataType3: This return.Container.Resolve & lt; DataType3 & gt; (); Default: Remove the new Invalid Emergency exception (); }}}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -