Reputation: 3907
in a WPF I've got a class called UserMessageService that I use to dispatch usermessages. I've created implemented it as a singleton but I want to expose it via IoC as well
public class UserMessageService : IUserMessageService
{
private static UserMessageService userMessageService;
private static readonly object objLock = new object();
private readonly IMessageMediator messageMediator;
private UserMessageService()
{
messageMediator = ServiceLocator.Default.ResolveType<IMessageMediator>();
}
public static UserMessageService Default
{
get
{
lock (objLock)
{
return userMessageService ?? (userMessageService = new UserMessageService());
}
}
}
public void SendMessage(string message,LogMessageTypeEnum type =LogMessageTypeEnum.Info)
{
var userMessage = new UserMessage
{
Message = message,
LogMessageType = type
};
messageMediator.SendMessageAsync(userMessage);
}
}
What's the best practice to do so?
I've added this during my registration's code (for other Catel's users)
serviceLocator.RegisterType<IUserMessageService>((x) => UserMessageService.Default);
Upvotes: 0
Views: 92
Reputation: 12846
Create a factory method and configure the IoC container to use this factory method for the type UserMessageService
Example
IoCContainer.For<UserMessageService>().Use(() => return UserMessageService.Default);
Upvotes: 1