Last active
May 26, 2017 15:39
-
-
Save thiagomajesk/bba6f93547d12285f8dae465fadd01bb to your computer and use it in GitHub Desktop.
ViewModelFactory (Mediator Pattern)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CharacterController : Controller | |
{ | |
/*[...]*/ | |
public IActionResult Profile(int id) | |
{ | |
/*[...]*/ | |
var viewModel = viewFactory.Build<int, CharacterProfileViewModelBuilder>(id); | |
return View(viewModel); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CharacterProfileViewModelBuilder : IViewModelBuilder<Character, CharacterProfileViewModel> | |
{ | |
private readonly DatabaseContext context; | |
private readonly IMapper mapper; | |
//Dependencies "automatic" discovered by ASP.NET Core container | |
public CharacterProfileViewModelBuilder(DatabaseContext context, IMapper mapper) | |
{ | |
this.context = context; | |
this.mapper = mapper; | |
} | |
public CharacterProfileViewModel Build(Character input) | |
{ | |
return new CharacterProfileViewModel{Id = input.Id, Name = input.Name}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultViewFactory : IViewModelFactory | |
{ | |
private readonly IServiceProvider provider; | |
public DefaultViewFactory(IServiceProvider provider) | |
{ | |
this.provider = provider; | |
} | |
protected IViewModelBuilder<TViewModel> GetViewBuilder<TViewModel>() | |
{ | |
return provider.GetService(typeof (IViewModelBuilder<TViewModel>)) as IViewModelBuilder<TViewModel>; | |
} | |
protected IViewModelBuilder<TInput, TViewModel> GetViewBuilder<TInput, TViewModel>() | |
{ | |
return provider.GetService(typeof (IViewModelBuilder<TInput, TViewModel>)) as IViewModelBuilder<TInput, TViewModel>; | |
} | |
public TViewModel Build<TViewModel>() | |
{ | |
var builder = GetViewBuilder<TViewModel>(); | |
return (builder != null ? builder.Build() : Activator.CreateInstance<TViewModel>()); | |
} | |
public TViewModel Build<TInput, TViewModel>(TInput input) | |
{ | |
var builder = GetViewBuilder<TInput, TViewModel>(); | |
return (builder != null ? builder.Build(input) : Activator.CreateInstance<TViewModel>()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IViewModelBuilder<TViewModel> | |
{ | |
TViewModel Build(); | |
} | |
public interface IViewModelBuilder<TInput, TViewModel> | |
{ | |
TViewModel Build(TInput input); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IViewModelFactory | |
{ | |
TViewModel Build<TViewModel>(); | |
TViewModel Build<TInput, TViewModel>(TInput input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment