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?
Related
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!
I have used T4 to generate partial classes from some input file (XML, etc) and then hand code additional partial bits onto those generated classes.
Is it possible to go the other way? To hand craft partial classes, and use T4 to template boiler plate bits to them?
Obviously I can't use reflection to look for the classes since it's not compiled yet, but I see Visual Studio inspect uncompiled code for different utilities. Perhaps Visual Studio offers some feature to support this I don't know about. Long shot, I guess.
Thanks
Also, you can use T4 with VS's CodeModel to read the code in your project without compiling and then generate from that metadata.
There's some pointers to examples here: http://blogs.msdn.com/b/garethj/archive/2009/09/25/dte-and-t4-better-together.aspx
Actually, T4 is used this way frequently. Yes, it requires reflection, but partial classes compile even if bits of them aren't generated yet. I would look at examples for generating strongly typed views as described here for examples of using reflection to generate new files.
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/
My question isn't so much about use of interfaces but more of a project organization nature.
Note: I am using VisualStudio in a multi-layered application.
Should my Interface files live in a separate project from their implementations? My initial thought is that it would be useful to separate out all my service interfaces into their own project (and a project for my initial implementations) so that down the road the implementation/concrete project may be removed and replaced with a new one if necessary.
To clarify with an example: Suppose I have a business layer Interface called IBusinessService which lives in the MyApp.Business.Services namespace. My implementation FooBusinessService would exist in the same namespace, but a different project in VisualStudio. If later on the implementation needed to be reworked, a developer could remove the reference to the FooService.proj and replace it with a reference to BarService.proj.
This seems like it would declutter the app solution by allowing you to reference a project with only interfaces without also acquiring concrete implementations (which may be obsolete or of no use to you), but am I missing something?
I'm with you. I prefer to put my interfaces in a separate project AND in a different namespace. The classic example is with data access classes. You want to be able to code an MSSQL version and a MySQL version, both implementing the same interface. As such, I prefer that the interface definition be in a separate assembly/project. Here's an example of how I lay out assemblies and namespaces:
Elder.DataAccess.Core - contains the interfaces and common utilities
Elder.DataAccess.MSSQL - specific MSSQL implementations of the interfaces
Elder.DataAccess.MySQL - specific MySQL implementations of the interfaces
This allows me to modify the implementations without touching the project that contains the interface definitions. This helps me with version control and change tracking, too. There might be other ways to skin this cat, so I'll be eager to see other folks' answers.
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