Vous feriez mieux de créer manuellement UnityContainer et l'injecter dans votre localisateur de services:
public class ServiceLocator
{
public static void SetServiceLocatorProvider(IServiceLocator serviceLocator)
{
Instance = serviceLocator;
}
public static void SetServiceLocatorProvider(Func<IServiceLocator> serviceLocator)
{
Instance = serviceLocator();
}
public static IServiceLocator Instance { get; private set; }
}
public class ServiceLocatorContainer : IServiceLocator
{
private readonly IUnityContainer _unityContainer;
public ServiceLocatorContainer(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
}
//skipped for simplicity sake
}
public abstract class ApplicationControllerBase
{
/// <summary>
/// Создать экземпляр Unity контейнера
/// </summary>
private IUnityContainer CreateContainer()
{
return new UnityContainer();
}
protected virtual void ConfigureContainer()
{
RegisterTypeIfMissing(typeof(IServiceLocator), typeof(ServiceLocatorContainer), true);
RegisterTypeIfMissing(typeof(ISharedAssemblyInitializer), typeof(SharedAssemblyInitializer), true);
ServiceLocator.SetServiceLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
}
protected void RegisterTypeIfMissing(Type fromType, Type toType, bool registerAsSingleton)
{
if (Container.IsTypeRegistered(fromType))
{
}
else
{
if (registerAsSingleton)
{
Container.RegisterType(fromType, toType, new ContainerControlledLifetimeManager());
}
else
{
Container.RegisterType(fromType, toType);
}
}
}
public virtual void Run()
{
this.Container = CreateContainer();
this.ConfigureContainer();
}
public IUnityContainer Container { get; private set; }
}
ceci est une version simplifiée de l'extension prisme à l'unité. voir des sources plus détaillées de la classe modulesmanager dans le prisme. application commence par la méthode Run. Vous créez un conteneur et enregistrez votre servicelocator dans celui-ci. Wile enregistrement, UnityContainer crée par exemple de celui-ci et se passer dans cteur:
public ServiceLocatorContainer(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
}
puis, en utilisant la méthode SetServiceLocatorProvider vous attribuez à l'instance de propriété instance de classe ServiceLocator, qui a référence au contenant de l'unité au sein. Une telle solution donne également l'abstraction du conteneur DI concret. Et vous pouvez faire référence à votre localisateur de service à partir de n'importe où dans votre code. 1) Injectiong en constructeur
public class RLoginABS
{
IServiceLocator serviceLocator;
public RLoginABS(IServiceLocator serviceLocator)
{
this.serviceLocator = serviceLocator;
}
public void Login(string user, string password)
{
REnvironmentRWS environment = REnvironmentRWS.Current;
serviceLocator.RegisterInstance<IEnvironmentRWS>(environment as IEnvironmentRWS);
}
}
ou en utilisant la classe statique:
shellViewModel = ServiceLocator.Instance.Resolve<IShellViewModel>();
ps: Interface IServiceLocator, qui est une abstraction de la mise en œuvre concrète du conteneur DI:
public interface IServiceLocator
{
void Register<TInterface, TImplementor>() where TImplementor : TInterface;
void Register(Type TInterface, Type TImplementor);
void RegisterAsSingleton<TInterface, TImplementor>() where TImplementor : TInterface;
T Resolve<T>();
}
ses méthodes enveloppements méthodes de conteneurs en béton, par exemple:
public void Register(Type TInterface, Type TImplementor)
{
_unityContainer.RegisterType(TInterface, TImplementor);
}
Espoir - ça aide!