This project is read-only.

Namespace Conflicts Due To Fully Qualified Types

Nov 26, 2012 at 7:49 AM
Edited Nov 26, 2012 at 7:52 AM
//External type Thing
namespace Baz.Qux
	public class Thing
		public Thing() {}

//MVC Controller
using Baz.Qux;

namespace Foo.Bar.Baz.Controllers
	public partial class SomeController : Controller
		public virtual ActionResult SomeAction(Thing thing) 

//Generated T4MVC ActionResult
public override System.Web.Mvc.ActionResult SomeAction(Baz.Qux.Thing thing)

With the situation above I get a compile time error:

The type or namespace name 'Qux' does not exist in the namespace 'Foo.Bar.Baz' (are you missing an assembly reference?)

I thought adding the namespace to ReferencedNamespaces thus removing the requirement to fully qualify the type might work, but it did not.

Manually adding a using directive and deleting the qualification works, but will be overwritten next time I change the controller.


Simon Hartcher

Nov 27, 2012 at 8:19 AM

The most correct thing is probably to use the global syntax, e.g. global::Baz.Qux.Thing. But one concern is that we'd end up using it all over the place, which would make the generated code a fair bit uglier. I suppose it could be a flag that's off by default.

Nov 27, 2012 at 11:28 AM
Edited Nov 27, 2012 at 11:30 AM

I agree, it would be nice if the generated code was still as readable as possible.

I just played around with a different approach.

    public void WriteFormalParameters(bool first) {
        foreach (var p in Parameters) {
            if (first)
                first = false;
                TT.Write(", ");

            string referencedType = settings.ReferencedNamespaces.Where(ns => p.Type.StartsWith(ns + ".")).FirstOrDefault();
            string type = referencedType == null ? p.Type : p.Type.Replace(referencedType + ".", "");

            TT.Write(type + " " + p.Name);

If the namespace is in ReferencedNamespaces it will remove that part of the string from the type.

Its late so I may have overlooked the bleeding obvious but this works in my use case. I'm sure this could be tweaked further...

Nov 27, 2012 at 6:07 PM

That looks potentially interesting. If you get it in a shape that you feel confident works well and doesn't break thing, please send a PR and I'll get it in!

Feb 28, 2013 at 11:09 PM