1﻿usingSystem; 2usingSystem.Collections; 3usingSystem.Collections.Generic; 4usingSystem.Linq; 5 6usingLinFu.IoC; 7usingLinFu.IoC.Configuration; 8usingLinFu.IoC.Interfaces; 9usingMicrosoft.Practices.ServiceLocation;1011namespaceCommonServiceLocator.LinFuAdapter12{13publicsealedclassLinFuServiceLocator:ServiceLocatorImplBase14{15readonlyIServiceContainer_container;1617publicLinFuServiceLocator(IServiceContainercontainer)18{19if(container==null)20thrownewArgumentNullException("container");2122_container=container;23}2425protectedoverrideobjectDoGetInstance(TypeserviceType,stringkey)26{27if(key!=null)28return_container.GetService(key,serviceType);2930// Return the first service if nothing else can be found
31Func<IServiceInfo,bool>criteria=info=>32{33varmatchesServiceType=info.ServiceType==serviceType;3435if(!matchesServiceType)36returnfalse;3738returninfo.ArgumentTypes==null||info.ArgumentTypes.Count()==0;39};4041vardefaultService=_container.AvailableServices.Where(criteria).FirstOrDefault();42return_container.GetService(defaultService);43}4445protectedoverrideIEnumerable<object>DoGetAllInstances(TypeserviceType)46{47Func<IServiceInfo,bool>matchesServiceType=info=>serviceType==info.ServiceType&&48(info.ArgumentTypes==null||49info.ArgumentTypes.Count()==0);5051varservices=_container.AvailableServices.Where(matchesServiceType);52foreach(varserviceinservices)53{54yieldreturn_container.GetService(service);55}56}57}58}