Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for simple mockup viewmodels #662

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Caliburn.Micro.Core/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ namespace Caliburn.Micro
/// <summary>
/// Used by the framework to pull instances from an IoC container and to inject dependencies into certain existing classes.
/// </summary>
public static class IoC
{
public static class IoC {
static IoC() {
if (Execute.InDesignMode) {
GetInstance = (service, key) => Activator.CreateInstance(service);
GetAllInstances = service => new[] { Activator.CreateInstance(service) };
BuildUp = o => { };
}
}
/// <summary>
/// Gets an instance by type and key.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Caliburn.Micro.Platform.Core/TypeMappingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class TypeMappingConfiguration
/// </summary>
public string DefaultSubNamespaceForViewModels { get; set; } = "ViewModels";

/// <summary>
/// The default subnamespace for DesignViewModels. Used for creating default subnamespace mappings. Defaults to "DesignViewModels".
/// </summary>
public string DefaultSubNamespaceForDesignViewModels = "DesignViewModels";

/// <summary>
/// Flag to indicate whether or not the name of the Type should be transformed when adding a type mapping. Defaults to true.
/// </summary>
Expand All @@ -38,6 +43,12 @@ public class TypeMappingConfiguration
/// </summary>
public List<string> ViewSuffixList { get; set; } = new List<string>(new[] { "View", "Page" });

/// <summary>
/// List of design ViewModels suffixes for which default type mappings should be created. Applies only when UseNameSuffixesInMappings = true.
/// Default values are "ViewModelDesign", "DesignViewModel"
/// </summary>
public List<string> DesignViewModelSuffixList = new List<string>(new[] { "ViewModelDesign", "DesignViewModel" });

/// <summary>
/// The name suffix for ViewModels. Applies only when UseNameSuffixesInMappings = true. The default is "ViewModel".
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Caliburn.Micro.Platform/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class ViewLocator {
//These fields are used for configuring the default type mappings. They can be changed using ConfigureTypeMappings().
static string defaultSubNsViews;
static string defaultSubNsViewModels;
static string defaultSubNsDesignViewModels;
static bool useNameSuffixesInMappings;
static string nameFormat;
static string viewModelSuffix;
Expand Down Expand Up @@ -70,6 +71,11 @@ public static void ConfigureTypeMappings(TypeMappingConfiguration config) {
throw new ArgumentException("DefaultSubNamespaceForViewModels field cannot be blank.");
}

if (Execute.InDesignMode && String.IsNullOrEmpty(config.DefaultSubNamespaceForDesignViewModels))
{
throw new ArgumentException("DefaultSubNamespaceForDesignViewModels field cannot be blank.");
}

if (String.IsNullOrEmpty(config.NameFormat)) {
throw new ArgumentException("NameFormat field cannot be blank.");
}
Expand All @@ -79,13 +85,20 @@ public static void ConfigureTypeMappings(TypeMappingConfiguration config) {

defaultSubNsViews = config.DefaultSubNamespaceForViews;
defaultSubNsViewModels = config.DefaultSubNamespaceForViewModels;
if (Execute.InDesignMode) defaultSubNsDesignViewModels = config.DefaultSubNamespaceForDesignViewModels;
nameFormat = config.NameFormat;
useNameSuffixesInMappings = config.UseNameSuffixesInMappings;
viewModelSuffix = config.ViewModelSuffix;
ViewSuffixList.AddRange(config.ViewSuffixList);
includeViewSuffixInVmNames = config.IncludeViewSuffixInViewModelNames;

SetAllDefaults();

if (Execute.InDesignMode) {
foreach (var designSuffix in config.DesignViewModelSuffixList) {
NameTransformer.AddRule($"{designSuffix}$", config.ViewSuffixList);
}
}
}


Expand All @@ -96,6 +109,7 @@ private static void SetAllDefaults() {
}
else {
AddSubNamespaceMapping(defaultSubNsViewModels, defaultSubNsViews);
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsDesignViewModels, defaultSubNsViews);
}
}

Expand All @@ -113,6 +127,9 @@ public static void AddDefaultTypeMapping(string viewSuffix = "View") {

//Check for <Namespace>.ViewModels.<NameSpace>.<BaseName><ViewSuffix> construct
AddSubNamespaceMapping(defaultSubNsViewModels, defaultSubNsViews, viewSuffix);

//Check for <Namespace>.DesignViewModels.<NameSpace>.<BaseName><ViewSuffix> construct
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsDesignViewModels, defaultSubNsViews, viewSuffix);
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Caliburn.Micro.Platform/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class ViewModelLocator
//These fields are used for configuring the default type mappings. They can be changed using ConfigureTypeMappings().
static string defaultSubNsViews;
static string defaultSubNsViewModels;
static string defaultSubNsDesignViewModels;
static bool useNameSuffixesInMappings;
static string nameFormat;
static string viewModelSuffix;
Expand Down Expand Up @@ -85,6 +86,11 @@ public static void ConfigureTypeMappings(TypeMappingConfiguration config)
throw new ArgumentException("DefaultSubNamespaceForViewModels field cannot be blank.");
}

if (Execute.InDesignMode && String.IsNullOrEmpty(config.DefaultSubNamespaceForDesignViewModels))
{
throw new ArgumentException("DefaultSubNamespaceForDesignViewModels field cannot be blank.");
}

if (String.IsNullOrEmpty(config.NameFormat))
{
throw new ArgumentException("NameFormat field cannot be blank.");
Expand All @@ -95,6 +101,7 @@ public static void ConfigureTypeMappings(TypeMappingConfiguration config)

defaultSubNsViews = config.DefaultSubNamespaceForViews;
defaultSubNsViewModels = config.DefaultSubNamespaceForViewModels;
defaultSubNsDesignViewModels = config.DefaultSubNamespaceForDesignViewModels;
nameFormat = config.NameFormat;
useNameSuffixesInMappings = config.UseNameSuffixesInMappings;
viewModelSuffix = config.ViewModelSuffix;
Expand All @@ -114,6 +121,7 @@ private static void SetAllDefaults()
else
{
AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsViewModels);
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsDesignViewModels, defaultSubNsViews);
}
}

Expand All @@ -133,6 +141,10 @@ public static void AddDefaultTypeMapping(string viewSuffix = DefaultViewSuffix)

//Check for <Namespace>.Views.<NameSpace>.<BaseName><ViewSuffix> construct
AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsViewModels, viewSuffix);

//Check for <Namespace>.Views.<NameSpace>.<BaseName><ViewSuffix> construct
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsDesignViewModels, viewSuffix);

}

/// <summary>
Expand Down