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.
Related
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.
In a C# project, I need to add many classes that each have 10 common properties, each one adding several specific properties.
It is expected that the number of common properties will grow up to 20.
I would like to:
Have a base class / file that would hold all the common properties
Create those many classes without inheriting the base class and the new classes have the common properties from the base class/file.
In the "derived" classes I can add new properties.
In the future, when the list of common properties will grow, I would just edit the base class/file and have them inserted automatically in all "descendant" classes.
Basically, I need a placeholder inserted somewhow in my files that links to an external file and add the content into the classes at compile time, just as #include directive works in C++.
So I need inheritance without deriving the classes, it that makes sense.
Probably the closet you can get is interface properties. You will still need to implement the properties in the "descendant" classes. It would be fairly easy to write a script yourself that implements them as auto-properties when the master file changes though.
You can easily generate your classes with CodeDOM. See that example.
You can use a compilation-time T4 text template. There you can add all your properties and from there the code is multiplied and you can even change it as you like. If you need individual code for these classes, you can just make the classes partial and add the individual code in manual files.
BUT: You will hardly get any IDE features for T4. There are some extensions that at kleast give you some syntax highlighting, but basically thats it.
I have an existing ASP.NET MVC3 application and want to incorporate ExtJs primarily for charting now, but for more functionality later. I already have a set of POCO Entities in my architecture, and would like to have them all available for the javascript-based model. Also, I don't want to have to maintain two sets of entities. I figure it's probably not hard to write something that will creat JSON representations of the entities, but someone must have done this already, right? I couldn't find it in my own searching. Any assistance is appreciated!
I don't really understand what you're asking, but perhaps a C# to JavaScript compiler can help you. Alternatives I know of:
Saltarelle (mine)
Script#
SharpKit
That's an interesting idea! I can certainly understand the motivation. A nice solution may be to generate a javascript file from your C# models using the T4 templating engine.
This way you could create a template that uses reflection to grab all the types defined under a certain project or namespace and iterate over them, writing out a javascript definition for each. A little more reflection to list the public property names, and whatever javascript syntax you'd like surrounding them and you're all sorted!
You add a reference to newtonsoft.json and you do like that
var model = #Html.Raw(JsonConvert.SerializeObject(Model, new IsoDateTimeConverter()));
Is it what you're looking for ?
I've been looking to do something simlar for a personal project and have found DataContractJsonSerializer on MSDN which would allow you to serialize your POCOs to JSON. There is also an article on CodeProject entitled JSON Serialization and Deserialization in ASP.NET by SummiG that gives a good -- and at first glance complete -- example of creating a static helper class.
That being said, I haven't tried it myself yet and would appreciate any feedback if you do go that route.
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.
Is it possible to have any control over the class names that get generated with the .Net XSD.exe tool?
As far as I'm aware I don't think this is possible, the class names match almost exactly to whats in the schema.
Personally I would change the class names after XSD has generated the code, but to be honest I usually just stick with what XSD generates. Its then easier for someone else reading the code to understand what classes map to what parts of the XML.
Alternatively, if you have control over the schema you could update that?
Basically, no. If you were writing the classes manually, you could have:
[XmlType("bar")]
class Foo {}
however, you can't do this with the xsd-generated classes. Unfortunately, one of the things you can't do with a partial class is rename it. Of course, you could use xsd to generate it, change the .cs file and don't generate it again, but that is not ideal for maintenance.
Any schema with somewhat deep nesting then ends up with utterly useless names.
I don't know of a way to work around the problem, but my tip to at least reduce the negative impact is this: Define a list of aliases for the awfully-named types. This way you can write code that isn't completely unreadable without losing the ability to regenerate.
using AgentAddress = Example.Namespace.DataContract.RootElementNestedElementAgentAddress;
...
It's a pity this list itself has to be copy-pasted to all code files needing it, but I think this at least constitutes an improvement.