Is this the correct way to implement the factory pattern?
I have an application using raw MVVM (without a framework). My ViewModels are a list that the MainViewModel manages, but there are some view models that need specific information (like user account, name etc...) that I get from a web service, so I've created a service that does this for me, I get the user account, username and other stuff and since my view models need this information I've created a factory that return the ViewModels object and insert it in the collection that the MainViewModel holds and in the constructor I pass the service as an interface.
Example:
SelectionViewModel.cs
public SelectionViewModel(IClient provider)
{
_provider = provider;
_configurationRepository = new ConfigurationRepository();
}
public SelectionViewModel(IClient provider, IPageFactory factory)
{
_provider = provider;
_factory = factory;
_configurationRepository = new ConfigurationRepository();
}
Factory.cs
IPageViewModel IPageFactory.Create(Type type)
{
var page = Activator.CreateInstance(type) as IPageViewModel;
if (page != null)
return page;
else
return (IPageViewModel)Activator.CreateInstance(typeof(OutOfServiceViewModel));
}
IPageViewModel IPageFactory.Create(object [] parameters, Type type)
{
IPageViewModel page;
if (type.GetConstructor(Type.EmptyTypes) != null)
page = Activator.CreateInstance(type) as IPageViewModel;
else
page = Activator.CreateInstance(type, parameters) as IPageViewModel;
if (page != null)
return page;
return (IPageViewModel)Activator.CreateInstance(typeof(OutOfServiceViewModel));
}
My question is if there's a better way to send the parameters to the constructors or a better way at all to solve my problem (pass the service with user information between ViewModels)
链接地址: http://www.djcxy.com/p/82262.html上一篇: 将多个事件合并到一个接口中
下一篇: 这是实施工厂模式的正确方法吗?