3
I am editing the T4 templates from ASP.NET MVC’s Scaffold and I need to get some extra class information. At first, for example, of the attribute DisplayName
class.
I found some examples:
var env = (DTE)((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE));
var proj = env.Solution.Projects.OfType<Project>()
.Where(p => p.Name == assemblyName)
.FirstOrDefault();
var codeType = proj.CodeModel.CodeTypeFromFullName(classFullName);
var attr = codeType != null ? codeType.Attributes
.OfType<EnvDTE.CodeAttribute>()
.FirstOrDefault(x => x.Name == "DisplayName") : null;
var modelName = attr != null ?
attr.Value.Replace("\"", "") :
ViewDataTypeShortName;
So far, so good. But I have in my Solution two projects that are from another Solution (and are in different Solutions directories), but which were added normally via Add / Existing Project.
So these other two assemblies I can’t get reference using:
var proj = env.Solution.Projects.OfType<Project>()
.Where(p => p.Name == assemblyName)
.FirstOrDefault();
In fact, I did a re-interview on env.Solution.Projects.OfType<Project>()
to see what would be printed:
<# foreach (var proj in env.Solution.Projects.OfType<Project>()) { #>
// <#= proj.Name #>
<# } #>
And I got something I didn’t understand. Among the Projects that were really created, like:
// Common
// Projeto.Domain
// Projeto.Service
// Projeto.WebMVC
Where that Common
is a Solution Folder created for the projects of the other Solution. But the projects that are within it.
So, another way I found to perform a Reflection in the assemblies was to add them directly:
<#@ assembly name="C:\Projects\SolutionA\Projeto.Domain\bin\Debug\Projeto.Domain.dll" #>
<#@ include namespace="Projeto.Domain.Entities" #>
<#@ assembly name="C:\Projects\SolutionA\Projeto.Service\bin\Debug\Projeto.Service.dll" #>
<#@ include namespace="Projeto.Service.ViewModels" #>
This way the Reflection is already made in the traditional way known.
The problem with this type of addition is that the sources will not be in the same directories, which will result in assemblies not being in the specified directories.
So I tried with, for example $(SolutionDir)
, but it seems that in Scaffold templates it does not work.
My question is how to add the references logically or how to read the other assemblies via env.Solution.Projects
.
For those who have had experience, another way to solve the problem is also welcome!
I don’t think it was a good idea for you to layer your project and try to use Scaffolding after. In the tests I do here, the only separation that is reasonable is to separate Models in a Class Library and Controllers and Views continue in the ASP.NET MVC project. Anyway, I can direct the response to the use of
EnvDTE
, if you want.– Leonel Sanches da Silva
My
Controllers
Views
are in the ASP.NET MVC project, only theViewModels
go to another layer. Yes, I would like you to give an example of solution if you have. Thank you!– JamesTK