public void Z__EmbedIOCSharedPerThread() { EmbedIOC.Register <IDbAccess>(() => _threadStatic ?? (_threadStatic = new SqlDb("SqlDb:" + Thread.CurrentThread.Name))); var threadList = new List <Thread>(); for (int threadCounter = 1; threadCounter <= 10; threadCounter++) { var newThread = new Thread(new ThreadStart(EmbedIOCSharedPerThread_Worker)) { Name = threadCounter.ToString() }; threadList.Add(newThread); } // start threads threadList.ForEach(t => t.Start()); // wait for all thread completion threadList.ForEach(t => t.Join()); _threadReferenceList.ForEach(x => { // assert that 10 different instances were created, one for each thread Assert.AreEqual(1, _threadReferenceList.Count(v => v == x)); }); }
public void Z__EmbedIOCExample() { EmbedIOC.Register <IDbAccess>(() => new SqlDb("")); var result = EmbedIOC.Resolve <IDbAccess>(); Assert.IsInstanceOfType(result, typeof(SqlDb)); }
public void InitializeEmbedIOCResolver() { EmbedIOC.Register <ICustomDbContext>(() => new CustomDbContext()); EmbedIOC.Register <IRepository>(() => new Repository(EmbedIOC.Resolve <ICustomDbContext>())); // run once so it will be cached var tmp = EmbedIOC.Resolve(typeof(ICustomDbContext)); var tmp2 = EmbedIOC.Resolve(typeof(IRepository)); }
static void EmbedIOCSharedPerThread_Worker() { Thread.Sleep(100); var result1 = EmbedIOC.Resolve <IDbAccess>(); var result2 = EmbedIOC.Resolve(typeof(IDbAccess)); Assert.ReferenceEquals(result1, result2); _threadReferenceList.Add(result1); Debug.Print("Thread: " + Thread.CurrentThread.Name + " uses connection " + ((SqlDb)result1).ConnectionName); }
public void Z__EmbedIOCSingleton() { var singleton = new SqlDb("Singleton"); EmbedIOC.Register <IDbAccess>(() => singleton); var result1 = EmbedIOC.Resolve <IDbAccess>(); var result2 = EmbedIOC.Resolve(typeof(IDbAccess)); Assert.ReferenceEquals(result1, result2); Assert.ReferenceEquals(singleton, result1); }
public void Presentation_099_ResolverRelativeSpeedDemo() { var unity = InitializeUnityResolver(); InitializeGenericResolver(); InitializeEmbedIOCResolver(); var watch = new Stopwatch(); var oneMillion = 1000000; double computeRelativeBaseTicks = 0; watch.Start(); for (int i = 0; i < oneMillion; i++) { var myVar = new CustomDbContext(); } watch.Stop(); //Log("new from Constructor {0} ", FormatTicks(watch.ElapsedTicks)); computeRelativeBaseTicks = watch.ElapsedTicks; Log("new from Constructor {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = new Repository(new CustomDbContext()); } watch.Stop(); //Log("complex new from Constructor {0} ", FormatTicks(watch.ElapsedTicks)); Log("complex new from Constructor {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); //watch.Restart(); //for (int i = 0; i < oneMillion; i++) //{ // var myVar = GenericResolver<CustomDbContext>.Resolve(); //} //Log("new from GenericResolver {0} ", FormatTicks(watch.ElapsedTicks)); //watch.Restart(); //for (int i = 0; i < oneMillion; i++) //{ // var myVar = GenericResolver<IRepository>.Resolve(); //} //Log("complex new from GenericResolver {0} ", FormatTicks(watch.ElapsedTicks)); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = EmbedIOC.Resolve <ICustomDbContext>(); } //Log("new from EmbedIOC {0} ", FormatTicks(watch.ElapsedTicks)); Log("new from EmbedIOC {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = EmbedIOC.Resolve <IRepository>(); } //Log("complex new from EmbedIOC (generic) {0} ", FormatTicks(watch.ElapsedTicks)); Log("complex new from EmbedIOC (generic) {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); var typeofICustomDbContext = typeof(ICustomDbContext); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = EmbedIOC.Resolve(typeofICustomDbContext); } //Log("new from EmbedIOC (type object) {0} ", FormatTicks(watch.ElapsedTicks)); Log("new from EmbedIOC (type object) {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = unity.Resolve <ICustomDbContext>(); } //Log("new from Unity {0} ", FormatTicks(watch.ElapsedTicks)); Log("new from Unity {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); watch.Restart(); for (int i = 0; i < oneMillion; i++) { var myVar = unity.Resolve <IRepository>(); } //Log("complex new from Unity {0} ", FormatTicks(watch.ElapsedTicks)); Log("complex new from Unity {0} ", FormatRelativeUnits(watch.ElapsedTicks / computeRelativeBaseTicks)); Log(""); }