We have a number of web sites (e.g. one public facing, one for internal administration) which share common web controls. We have made use of the T4MVC action result interface (IT4MVCActionResult) in our web controls to allow strongly-typed routes to be used.
To share the interface, we have had to copy the interface out of the template as a separate interface file. This interface is then referenced by the two (modified) T4MVC templates.
Proposed Enhancements
We have implemented our changes in a backwards-compatible manner, using some new configuration properties in "T4MVC.settings.t4":
// Namespaces to be referenced by the generated code
readonly string[] ReferencedNamespaces = new string[] {
};
// The name of the interface that all T4MVC action results will implement
const string ActionResultInterfaceName = "IT4MVCActionResult";
// If true, the T4MVC action result interface will be generated
// If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting
const bool GenerateActionResultInterface = true;
The default settings ensure that existing behaviour is observed.
However, we can then change these values for our scenario:
// Namespaces to be referenced by the generated code
readonly string[] ReferencedNamespaces = new string[] {
"Example.Shared.Web.Mvc"
};
// The name of the interface that all T4MVC action results will implement
const string ActionResultInterfaceName = "ISharedT4MVCActionResult";
// If true, the T4MVC action result interface will be generated
// If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting
const bool GenerateActionResultInterface = false;
The changes to the "T4MVC.tt" template to support this are:
Generate "using" directives for each of the referenced namespaces.
Replace all references to "IT4MVCActionResult" in the template, with <#= ActionResultInterfaceName #>
Add a conditional test around the generated of the T4MVC action result interface, and generate the interface only when the configuration value is set appropriately
<# foreach (var referencedNamespace in ReferencedNamespaces) { #>
using <#= referencedNamespace #>;
<# } #>
Let me know what you think. I expect this isn't a common scenario, but it would help us immensely in keeping the merge changes to a minimum each time we want to upgrade to the latest T4MVC!
Cheers,
Steven
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste">// Namespaces to be referenced by the generated code
readonly string[] ReferencedNamespaces = new string[] {
};
// The name of the interface that all T4MVC action results will implement
const string ActionResultInterfaceName = "IT4MVCActionResult";
// If true, the T4MVC action result interface will be generated
// If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting
const bool GenerateActionResultInterface = true;
Why did you have to rename the interface rather than just use its original name? Since it gets omitted when you set the flag, I assume it wouldn't conflict.
Why did you have to rename the interface rather than just use its original name? Since it gets omitted when you set the flag, I assume it wouldn't conflict.
thanks,
David
Hi David, thanks for the fast response as usual :)
We could have kept the original name but since we are using it to represent a strongly-typed route in our generic web controls (that in themselves have no other knowledge of T4MVC), it was felt that it was clearer and less coupled to have a name without
"T4MVC" in it.
Perhaps my example doesn't help. Our actual interface name used is "IRouteResult".
That makes sense. Maybe we can look into making this change as it seems fairly harmless, and it's plausible that other may want to do something similar.
smsloggett
Member
3 Points
14 Posts
Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 05, 2010 01:01 PM|LINK
Background
We have a number of web sites (e.g. one public facing, one for internal administration) which share common web controls. We have made use of the T4MVC action result interface (IT4MVCActionResult) in our web controls to allow strongly-typed routes to be used.
To share the interface, we have had to copy the interface out of the template as a separate interface file. This interface is then referenced by the two (modified) T4MVC templates.
Proposed Enhancements
We have implemented our changes in a backwards-compatible manner, using some new configuration properties in "T4MVC.settings.t4":
// Namespaces to be referenced by the generated code readonly string[] ReferencedNamespaces = new string[] { }; // The name of the interface that all T4MVC action results will implement const string ActionResultInterfaceName = "IT4MVCActionResult"; // If true, the T4MVC action result interface will be generated // If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting const bool GenerateActionResultInterface = true;The default settings ensure that existing behaviour is observed.
However, we can then change these values for our scenario:
// Namespaces to be referenced by the generated code readonly string[] ReferencedNamespaces = new string[] { "Example.Shared.Web.Mvc" }; // The name of the interface that all T4MVC action results will implement const string ActionResultInterfaceName = "ISharedT4MVCActionResult"; // If true, the T4MVC action result interface will be generated // If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting const bool GenerateActionResultInterface = false;The changes to the "T4MVC.tt" template to support this are:
<# foreach (var referencedNamespace in ReferencedNamespaces) { #> using <#= referencedNamespace #>; <# } #><# if (GenerateActionResultInterface) { #> [CompilerGenerated] public interface <#= ActionResultInterfaceName #> { string Action { get; set; } string Controller { get; set; } RouteValueDictionary RouteValueDictionary { get; set; } } <# } #>Let me know what you think. I expect this isn't a common scenario, but it would help us immensely in keeping the merge changes to a minimum each time we want to upgrade to the latest T4MVC!
Cheers,
Steven
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste">// Namespaces to be referenced by the generated codereadonly string[] ReferencedNamespaces = new string[] {
};
// The name of the interface that all T4MVC action results will implement
const string ActionResultInterfaceName = "IT4MVCActionResult";
// If true, the T4MVC action result interface will be generated
// If false, the namespace of the interface must be referenced in the 'ReferencedNamespaces' setting
const bool GenerateActionResultInterface = true;
</div>
T4MVC .NET 3.5
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 05, 2010 07:14 PM|LINK
Trying to fully understand this... :)
Why did you have to rename the interface rather than just use its original name? Since it gets omitted when you set the flag, I assume it wouldn't conflict.
thanks,
David
smsloggett
Member
3 Points
14 Posts
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 06, 2010 08:17 AM|LINK
Hi David, thanks for the fast response as usual :)
We could have kept the original name but since we are using it to represent a strongly-typed route in our generic web controls (that in themselves have no other knowledge of T4MVC), it was felt that it was clearer and less coupled to have a name without "T4MVC" in it.
Perhaps my example doesn't help. Our actual interface name used is "IRouteResult".
Thanks,
Steven
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 07, 2010 07:36 AM|LINK
Hi Steven,
That makes sense. Maybe we can look into making this change as it seems fairly harmless, and it's plausible that other may want to do something similar.
David
smsloggett
Member
3 Points
14 Posts
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 07, 2010 07:43 AM|LINK
Thanks David, that would be very helpful. We've been careful to ensure that it is backward compatible for that reason.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 11, 2010 12:55 AM|LINK
FYI, this is now in 2.6.11. Please make sure I didn't mess it up :)
David
smsloggett
Member
3 Points
14 Posts
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jan 11, 2010 08:55 AM|LINK
Excellent! Works great. Cheers.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Jul 07, 2012 05:04 PM|LINK
Please see this related thread to discuss possible changes: http://t4mvc.codeplex.com/discussions/362362
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Shared IT4MVCActionResult between multiple templates (enhancement)
Oct 31, 2012 06:06 AM|LINK
This change is now in beta! Please verify that it works for you. See http://t4mvc.codeplex.com/discussions/362362 for details.