Get current class diagram target of t4 template - c#

Currently, I have a T4 template (still empty), used for class diagram designers. So, let's say that I design a class (of any type), and I wanna get the the target class diagram which is using the T4 template. How do I do to get this? plus I wanna keep the generated code by the Diagram Class Designer. Is this possible??
Thanks by the way, any comment or answer.
UPDATE:
More important will be to process the usual diagram processor and then overwrite the generated code from my custom template. I've read about CodeModel but I still don't get it.
UPDATE:
Well, seems like nobody has an answer for me, so, all I need is to get de Class Diagram Designer Host or Service (I don't know), but there must be a way to do this.

Related

VSIX. Add code to existing class

I created vsix extension which have my custom templates. These templates create custom classes. After that I created a template that should write the pieces of code into another existing class (to supplement it), but in the end it overwrites it. I was looking for an answer on the Internet, but I did not find anything (maybe I was looking for information badly). So, the question is: Is there any opportunity to create vsix with such template and if it's possible how can I do this?
I will be glad for any help!

Provide hint to IntelliSense that a partial class should not be modified

As of lately I'm using quite some code generation, usually in combination with partial classes. Basically the setup is as follows:
Partial class containing generated code. Some of this code will call partial methods. The code is re-generated a lot of time. The code generator is in some cases a custom tool.
Partial methods are manually implemented in a separate file.
The problem is that when I'm using Intellisense features like "generate method", they are for some reason generated in the file containing the generated code. Obviously I don't want that.
My question is: Is it possible to generate some hint that tells Intellisense it shouldn't touch certain 'cs' files (but instead the other partial class)?
Update
In retrospect I should have noted that I'm using a custom tool to generate the code. It's not a EF or a simple transformation; there's quite a bit of logic involved in the code generation. Also, it generates a complete namespace and class structure with partial classes. The 'root namespace' is found by extracting it from the csproj file and then using the folder structure to figure out the absolute namespace (it's similar to how Linq2sql does this).
The answer suggested by xanatos (thanks!) works: intellisense sorts its operation on the name, then alphabetically sorts on the name and then picks the first item in the list. This means that you can generate a zzzz.foo.cs which (albeit a bit ugly) will work just fine. I've just ran some experiments and found out that the feature find all references returns the order that VS appears to use. As it turns out, it works like this:
Say you have a custom tool that works on the filename foo.bar and transforms it into foo.cs. The custom tool will generate the content as string and pass it back to Visual studio (that's just how custom tools work...). The result will be in a file called foo.cs.
Now, I was quite surprised to found that Intellisense does not sort it as foo.cs but rather as foo.bar\foo.cs. In other words: regardless of how you name the 'cs' output in your custom tool, you have to rename the base file foo.bar to something like zoo.bar.
While that might be a workaround, I'm hesistant to accept it as the answer, because I would have to give files in my project strange names (names have meaning...). Also, some of my custom tools have dependencies on their filenames, so that will also get broken...
Therefore, I'm still open for suggestions on how to fix this properly.
From a simple test I've done in VS2013, it seems that Visual Studio 2013 adds the method to the "first" file he finds in the Solution Explorer. So you could simply add a .something.cs to your file-name, like MyClass.generated.cs vs MyClass.cs. Be aware that the VS2013 seems to be using the "full path", with path ordering based on name. So:
Z\MyClass.cs
comes after
MyClass.generated.cs
(and Intellisense will put code in MyClass.generated.cs) even while in the Solution Explorer all the folders are ordered first.
Full example:
A\MyClass.gen3.cs
MyClass.gen2.cs
Z\MyClass.gen1.cs
This should be the order as "seen" by the Intellisense, so it will put the new classes in A\MyClass.gen3.cs.
Assuming you're talking about the EF, I always change the template file (.tt) so the filename of the auto-generated file is [classname].model.cs. This means my partial file, which by convention is called [classname].cs is alphabetically first and always seems to get picked for auto-generation.
All you have to do is find/replace all the:
fileManager.StartNewFile(entity.Name + ".cs");
With:
fileManager.StartNewFile(entity.Name + ".model.cs");
There should be 3.
This has other benefits like auto-generated files are clearly marked in the filename.
I still have no idea why they didn't do this in the first place.
If you're not talking about the EF, the same trick of using the filename to order them should work.

How can I modify the default code generation strategy for edmx?

I want to modify the default code generation strategy, how can I do that?
I simply want to modify the class name from <#=code.Escape(container)#> to Entities and change the default connection string to name=Default.
(I don't want to create a template file for the project, I want to edit it so it will work globally)
I've searched for .tt files, I could only find the ItemTemplates. I don't know what generates the code by default, this is the one I want to edit.
Update: I still don't know how to do this.
You can see what generates the code if you click your EMDX file and check file properties in Visual Studio. Look for Custom Tool property that will tell you the class name of the generator that converts EDMX XML into compilable code.
But regarding model customization, I would still suggest you use T4 that takes your EDMX and generates the same code as original generator. The good thing is that you can then manipulate it 'till you drop dead if you will.
And if you intend to use the T4 on several EMDXs in your project then I suggest you rather create a .ttinclude file and reference it in every .tt file. This way you will reuse existing code and when you'd change it it will be reflected on all generated files.
One more question: What do you mean by globally? Globally for all EDMX files in your project or for all EDMX files on your machine or all EDMX files on your project team or what? Define globally.
Additional edit
Since you've defined global as all projects on a particular machine this is what I'd do.
First of all: using T4 allows you to adjust EDMX -> code conversion changes per project or better said per solution (all projects in a particular solution). On other projects/solutions on the same machine, you should include the same T4 template reference. So it's not actually global in your sense...
The best thing you could do is to create a custom Visual Studio item template with this T4 template so it'd be much much easier adding this default T4 template to your solutions/projects. That's as global as you can make it by T4.
Maybe you should read this MSDN entry that talks about your kind of customization:
How to: Customize Object-Layer Code Generation (Entity Data Model Designer)
I don't know whether it is even possible to alter the default code generation.
Instead of trying to modify the default code generation, I suppose you could create a .tt that generates a derived class from the ObjectContext. This way you can name it and implement the default constructor as you wish.
Something like:
<#=Accessibility.ForType(container)#> partial class Entities : <#=code.Escape(container)#>
{
public Entities()
: base("name=Default")
{ }
}
The downside to this approach is you will need to deploy this .tt file with every EDMX you create.
However, with Visual Studio's addin architecture you could look into creating a template that creates an EDMX and this .tt file by default. As a replacement for adding a plain "ADO.NET Entity Data Model"
Looking into the EntityModelCodeGenerator (the Custom Tool that is run by the default codegen strategy), it seems that it is registered with the SingleFileGenerator extensibility mechanism, which is a COM component. Some more info here.

C# Possible to partial class "program" class for a console application?

I was wondering if its possible to change the default "program" class that gets created for any console application to a partial class.
I want to do this because I want better organisation rather than have all methods in 1 file categorized by region. It would make more sense for me to have certain method categories sitting in separate files.
My understanding of a partial class is that it is a class definition in multiple files that during a compile merges the class files into 1 class unit.
I could be wrong, or there could be a better way for me to achieve better organisational structure. Any suggestions would help, and thanks
You certainly can do that - but it sounds to me like you'd be better off splitting your code into multiple classes. If you've got multiple method "categories" those categories may well be natural class boundaries.
Generally speaking, the entry point class should be fairly small. That's not always the case, but it's a good rule of thumb. Usually its only purpose is to get the rest of the program running.
"Guidance Automation Toolkit" and "Guidance Automation Extension" provide option to extend the File -> New -> Project options and you can generate the code the way you like. You can use this only if you want to create initial code automatically while creating projects.
One example for Guidance Package is SmartClient Library (CAB+EL). Smartclient source code is available.
It is good to have separate files for single class. One example based on CAB/Composite Appliacation Block or Windows form application.
there would be view.cs and view.designer.cs and both are defining the same file. view.designer.cs is used specifically for GUI designer where developer normally won't edit. Developer edit view.cs.

Generate EF4 POCO classes first time only

The problem I'm having is, using the POCO templates, generating my POCO classes the first time only and not overwriting them when the templates are re-ran. I know this sounds hokey and the reason is that I'm actually changing these templates and trying to generate metadata classes rather than the actual POCO classes, but these metadata classes will be hand-edited and I want to keep those edits in the future but still regenerate a certain amount of it. I have it all working exactly as I want except for the regeneration of the files.
I have looked into T4 and it seems that there is a flag to do just this (see the Output.PreserveExistingFile property) but I don't understand where/how to set this flag. If you can tell me where/how to set this in the default POCO templates, then I think that's all I really need.
Thanks!! :-)
PreserveExistingFile property is only available in T4 Toolbox. POCO templates are plain T4, someone would have to convert them. Any volunteers?
You're doing this wrong.
All the classes created are partial classes. If you wish to alter the templates create new partial classes and put your code in them.

Categories

Resources