I've played around with Visual Studio Project Templates and found them useful so far.
Now I've found some material on Wizards and the IWizard interface.
The first basic steps are fairly easy to comprehend and seem very useful, but now I'd like to explore what else the IWizard interface has to offer, other than mere Text replacement. I can imagine a few use cases for the ShouldAddProjectItem method, but what - for example - if I want the user to be able to pick database tables and have the wizard generate model, mapping and CRUD-dao code (I already have an assembly for that tedious task)?
Can anyone give me links or hints on facilities of the interface?
In order to make CRUD, DAO, etc... I'll suggest you to check T4 Templates and, if you want more power.
Combine T4 with Dev Art's Entity Developer
Put it all inside a IWizard
Let me explain it a little bit more...
You'll need to develop some T4 Template integrated with Entity Developer (to apply either Entity Framework's or NHibernate's metadata) or standalone, in order to create as many CRUD-Dao-Service-DTO like classes you need.
Then, with the T4, you can create a new project, add it as long as a Entity Developer empty model, export it as project template, and make a IWizard with a custom dialog as show in the example you referenced.
In this IWizard, you ask for the Connection String, then you make text replacement with IWizard replacement and... you're done! User makes a "Update Model from Database" and the Entities are done.
I hope this hints are enough to start!
Related
I heavily use the VS 2019 quick refactorings, particularly the "Generate Type in new file" refactoring.
However, this refactoring generates code with private members like this
private FloorId id;
private BuildingId buildingId;
private SiteMapId siteMapId;
whereas I want public properties like
public FloorId Id{get;}
public BuildingId BuildingId{get;}
Public SiteMapId SiteMapId{get;};
Also the class is generated with an internal access modifier on the class itself, and I want public.
I can't determine how to customise the code generation, so I thought I would create a custom VSIX Refactoring Project and build my own refactoring implementation.
I would like to try and find the source code for the "Generate class in new file" refactoring so that I can use that as a basis to build my own.
Does anyone know where I could find this code, or something similar?
It seems like creating custom refactorings VSIX projects is a rather niche subject, and not a great deal out there to work from.
If you're unaware the "Generate new type..." option that's last in the refactoring list, that pops up a dialog that lets you customize some behavior, including the access modifier of the class. We don't let you choose properties but that's a good suggestion.
The C# engine for Visual Studio (Roslyn) is open source, so you can find at least a tiny bit of the code here: https://github.com/dotnet/roslyn/tree/master/src/Features/Core/Portable/GenerateType. That said, there's a lot of code there and a huge amount of stuff it depends on...this is not trivial to rewrite and easily extract. You're probably best off trying to modify that and then running a custom Roslyn to experiment with. You may also want to consider opening an issue or discussion on the Roslyn repository, because honestly this might just be a feature we'd take via pull request.
How to filter Intellisense, Auto Completion, in Visual Studio C#, to show only members that the Project defined, or that are contained in the assembly from which I am currently editing code. Or that are defined in the actual class/interface that I am completing code one, and not the parent class?
Very often when I want to invoke/call/use a member that I defined. I am currently in a habbit of navigating to the class which I want to invoke a function on, copy/remember the name, then complete the code.
The thing is that I would find it very useful (in reducing redundant hand movement), while coding, to be able to view only the functionality that is written by me. Because in the environment that I am writing each object has at least 200 built in members that appear in most intellisense suggestions. And these built in suggestions are not part of my Assembly or Project. they are from the Engine. Similarly C# itself defines all these ToString() like members for each System.Object of which everything is derived from. To find the User-Defined members amongst all these System and Engine defined members is a hassle.
I searched google and forum for answers, to no avail. Also I don't have the money for solutions, so I would appreciate if it would be a free solution.
I afraid the answer is negative.
For now, IntelliSense cannot suppress the display of built-in properties, methods, and so on from the root cause to only show the custom parameter methods, and so on.
However, since VS2017, IntelliSense has only added filters, which can only be filtered by fields, methods, classes, keywords and so on.But you cannot filter by customization or built-in.
Suggestion
I suggest you put the class name, method naming specification(add some fields to prompt the method's role such as function select_AllManagers). After that, you can type a few more keywords to narrow IntelliSense down.
In addition, if you still want to get this point, you could report it to DC forum to raise the Support Team's attention. Hope this could help you.
I am using Linq-to-sql for generating code for database interaction. I need the generated code to include Interfaces for each concrete class, and believe the only easy way to achieve this is to modify the Linq-to-SQL code generating template.
I have seen a guide by Damien Guard (http://damieng.com/blog/2008/09/14/linq-to-sql-template-for-visual-studio-2008), however I have 2 constraints prohibiting the approach:
I am on VS2010; and
I am working on a Dev environment in which I cannot install non-approved tools/etc.
Are there any other ways to modify how the Linq-to-sql code is generated the template for VS2010 that do not require installing 3rd party templates/etc?
Alternatively, are there any other easy ways to reverse engineer Interfaces to match existing concrete classes?
In my mind this sounds like a superb idea. Using the EnvDTE would make this possible too, so why isn't there more examples on this available?
Maybe I'm missing an disadvantage of doing this...?
Any pointer to good T4 and EnvDTE resources would be great. :)
You probably don't see it around much because it's actually quite difficult to implement well. I've been using T4 to generate model classes from WCF DTOs for use in an WinForms MVP variant for a while now, and it took quite some time to get it working right.
Using a class as a "data" source for a template is pretty difficult in-and-of itself. You'll need to choose between using reflection (or a similar API) to read compiled IL or CodeDom to read the source code. If you choose to work with compiled assemblies, you'll need to contend with problems like file locking and loading referenced assemblies. If you choose to work with source code, you'll need to deal with potential uncompilable code.
Once you've made that decision, copying properties will be the most trivial thing you'll need to do. You'll also need to make decisions about which interfaces and attributes (if any) on the source class should be reimplemented/copied to the generated class. Depending on how you're implementing things like validation, this can raise all sorts of little, picky problems. There's are also a lot of fun decisions to make around how to handle inheritance hierarchies and references to other model classes.
All of the above is addressable, but a one-size-fits-all approach would be pretty hard to implement. Returning to the "example" part of your question, there's also the potential issue of doing quite so much work without getting paid for it. I'd love to be able to share the T4 I created for model generation, but it belongs to my employer, and I have better things to do with my spare time than re-implement the approach for posting on the web...
Using a class as a "data" source for a template is pretty difficult
This is wrong. Look at asp mvc 3 scaffolding.
http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/
T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes.
I am researching some different ways that T4 is used, and I would appreciate to hear how YOU may have used it for real life scenarios. I am primarily interested in non-standard and creative uses.
Some interesting examples:
Phil Haack uses T4 to create static CSS files from .less
To Generate WPF and Silverlight Dependency Properties using T4 Templates
Note: I realize this is a discussion-oriented question, but the answers could be helpful to others. I have tagged it as subjective and also marked as "community wiki", so please allow the question to remain open. Thanks!
I am not a big fan of the stringy-ness of app.config/web.config, so I use T4 to read those files and make an AppSettings/WebSettings class that wraps the connection strings and key/values in a real class. This means that, as long as I always use AppSettings.SomeValue to reference my app.config, I get compile time checking, which is really nice.
I've used the T4 Templates within the sharp-architecture to generate everything from models to controllers to basic views.
Definitely worth checking out, even if you just want to see some advanced examples of T4 templates in action
I use T4 to:
Generate CRUD SQL Server and Oracle scripts.
Generate Data Access Layer, based on a database schema
Layer Generate Business Logic Layer, based on a database schema
Generate ASP.Net webforms, both HTML and codebehind, based on a database schema (scafolding).
It gives me a good, quick, simple, basic starting point for my projects.
And the best is I'm in control.
Here you can download an example of my templates
SubSonic 3.0 makes heavy use of T4 templates for generating your entity code.
Essentially it calls GetSchema() on your database connection and runs each table it finds through the T4 entity template. The great thing about using T4 here is that if you don't like the way it's handling your database schema, just edit the template.
I've tweaked the T4's to handle MySQL databases better for my situation, as I make use of many tinyint columns which the default T4 maps to byte types. A quick edit to the T4 gave me the type I wanted instead for my application entities.
LINQ to SQL templates for T4
http://l2st4.codeplex.com/
Templates replicating the functionality of the SQLMetal and the LINQ to SQL classes designer code-generators for both C# and VB.Net requiring just Visual Studio 2008.
Check out this podcast on T4 by Scott Hanselman talking with Kathleen Dollard.
http://www.hanselminutes.com/default.aspx?showID=170
I've used T4 to generate:
proxies (design time, for injecting/wrapping monitoring
code/logging/... in a very specific exposed api).
interface generation for a one-on-one interface/class mapping
replace reflection
code by "directly/real" calling code (maintenance advantage of
reflection code, but performance of the actual code) for instance
when allowing access to properties through an indexer, or something
in that direction.
xml generation for a java project (couldn't find a
T4-like solution for java, that is easily shared within a company, T4
is easy because it's build in and you can run it from command line)
generate enums from a master database (we generated them both for a delphi-code base and .Net code base)
T4 Templates are used heavily in the Web Service Software Factory (Service Factory).
See here for a list of more than 30 T4 Generators from the community in several areas including ASP.NET,WCF, UML, ADO.NET, .NET
http://t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.htm