May 13 2009

VS.NET Add-ins worth looking at

Category: .NET | Development Tools | DOTNET | Visual StudioJonathan @ 04:53

Run VS.NET as Administrator on Vista.

Export your settings using Tools | Import and Export Settings, don't let another windows reinstall take it all away from you.

Get Regionerate
- but make sure you use the "Remove All regions" command, before trying to apply others. It wont replace your existing regions.

Get Studio Tools, for File manager and the tear off editor.

Download Dev /efor - not sure about the name, but it works nicely.

Snippet Editor, useful, for changing the way the existing snippets work, as well as new ones.

Carl J has a large list of add-ins.

Tags:

Jan 29 2009

CodeGen and Visual Studio Snippets

Category: .NET | CodeGenWeek | Design | DOTNET | FrameworksJonathan @ 03:36
Code Snippets in Visual Studio is a form of CodeGen.

You have a template and its injected into your editor. I modify these quite a bit, mainly because I like my properties to have fields with an underscore and named the same as the property:

Here is my Prop.snippet (found in Tools|Code Snippet Manager|C#)
It is a little different as this one matches the variable name with the property, puts the brackets how I want them and also puts my _CamelCasedField name.

Also now with Visual Studio.NET 2008, we have a Code Snippet Editor to make life easier.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
            <Header>
                  <Title>prop</Title>
                  <Shortcut>prop</Shortcut>
                  <Description>Code snippet for an automatically implemented property</Description>
                  <Author>Microsoft Corporation</Author>
                  <SnippetTypes>
                        <SnippetType>Expansion</SnippetType>
                  </SnippetTypes>
            </Header>
            <Snippet>
                  <Declarations>
                        <Literal>
                              <ID>type</ID>
                              <ToolTip>Property type</ToolTip>
                              <Default>int</Default>
                        </Literal>
                        <Literal>
                              <ID>property</ID>
                              <ToolTip>Property name</ToolTip>
                              <Default>MyProperty</Default>
                        </Literal>
                  </Declarations>
                  <Code Language="csharp">
             <![CDATA[
private $type$ _$property$;

/// <summary>
/// 
/// </summary>
public $type$ $property$ 
{ 
  get
  {
    return _$property$;
  }
  set
  {
    _$property$ = value;
  }
}$end$]]>
                  </Code>
            </Snippet>
      </CodeSnippet>
</CodeSnippets>



Yeah, I know its longer, and some like the bracket on the top line {
}
like that, but thats the way I like it. Who can argue?
I know someone who still prefixes everything with str_, int_ and so on. We are all different.

Round Trip Engineering (RTE) for Code Snippets

I believe, the next generation of code snippets, and CodeGen as a whole, should have rte capabilities. using Code snippets as an example, when we embed a code snippet, in our code, it should always understand that it was/is a code snippet.
That way we could change the variable and have it change the expression in the code snippet. In fact this is simply refactoring, but keeping the Expression and code snippet metadata is an important step.

Tags: , ,

Jan 27 2009

The Top CodeGen App on CodePlex?

Category: .NET | Design | DOTNETJonathan @ 05:53

With downloads of 1294
It's

SubSonic

Ruby On Rails is propelling a new wave of programming - one that drives "Convention Over Configuration". This simple, elegant approach is attracting many developers who find the intricacies and complexity of Java and .NET to be daunting. SubSonic is our attempt to bring this ...

View the project


The nearest one to that amount of downloads is T4 Toolbox
T4 Toolbox is a library of code generation templates for Visual Studio.
T4 Toolbox has 321 downloads, which is sure to increase.

The creator Oleg Sych, will be joining me on a podcast. Subscribe to TalkWare to hear what he has to say about his creation.

PS. I am not affiliated with SubSonic
Although I do like it.

Tags: ,

Jan 26 2009

An Even better Idea: Abstract the Code file from the Language

and the editor provides you with visualizations of the code file, which contains a base language, metadata and more contextual information.

The Web 2.0 of Code Editing?

Currently, and even our new tools, like OSLO, when its released, is and will be file based.
Now that in itself is fine, to keep in line with source control and other file based tools. But our Integrated Development Environments require a better, a whole new kind of Code Editor.

I believe that we need to create what I tentatively call ActiveEditors. An ActiveEditor is one which as described above surround code snippets, understands a lot more about Context and the code we write. In order to do that, it should build a Code Tree, Abstract Syntax tree -LIKE, Object Model, separating the FILE from the Editor.

This means that we open a FILE, but view an ActiveEditor, showing a generated “view” of the file.

The actual file and the contents could be XML, or MSIL, but what we see (via built in CodeGen), is c# or vb. So the actual code file needs not be in a particular language.

What does this do? It provides an abstraction that creates a dynamic representation of the code file, in your language. Have a look at the picture. The file could store c#, but it never directly puts the textual content of that file in the editor. Instead, it generates the code, from the model, built from the file, and pushes that into your editor. Based on a setting, the file could be MSIL, and your view could be C#, or any combination.



Now you could write an entire project in c#, but at anytime, switch to VB.NET, and it would never affect anything, because the compiler is compiling the actual file, not your Visual Display within your ActiveEditor that was created.

How would it help?

We could potentially store MetaData within the Code Tree, that would be excluded from viewing, although kept in the file. Your language choice and the code could still be generated as you are used to it. (think code-behind, where the sub document is the base language, which you can show/hide).
This new MetaData, would then actively describe the code so that Code Generation tools and the Editor can manipulate it for context, refactoring and other.


The next big thing? Coming to Visual Studio 10? is it too late ?

Imagine your Visual View of the code had toggle switches, to show/hide comments, show/hide regions, show/hide methods, show/hide classes - it would still be in the actual file, but not in your Visual Display. In the same way, I could switch my Visual Display to VB.NET to edit something, and then switch it to c# to edit something else.
Each developer on the team can view and edit in the language of their choice, it would not matter.
If a team of two was using vb.net, and then 3 more developer were brought on the team, they could continue in the same project in their own language, and it would not change anything.

Tags: , ,

Jan 26 2009

Code Snippets should have Context

"A revolution for editing code in .NET"

One of the most common Code Generation tools involve databases and are called O/R Mappers. This is going to change with the likes of OSLO and DSL's. But historically, Object-Relational Mappers have been the predominant force in CodeGen and they are usually CRUD based. (create, read, update delete) They come in various flavors and I have not used one before now that I still use today. That is, I could never find one that gives me enough freedom to design and implement my project the way I need to, and very quickly I leave them behind. Most have limitations, and don't actually do what it says on the tin.

For example, more than most try to sell you the idea, that it removes the database from you, you can talk to it via the objects, however, I have never found one, that I have not had to go “under-the-hood” to modify a Stored procedure or some aspect of the generated code. So if you do choose to use an O/R mapper, you must be sure, that you can make changes deeper down in the generated code. CodeGen is not good enough yet, to be a black-box tool

But thats one flavor of CodeGen. Other flavors involve Wrappers, Code Snippets, Designer Tools, and general utilities that can generate entire projects and/or layers and everything in between. This post, I would like to concentrate on Visual Studio .NET Code snippets.
Visual Studio.NET Code Snippets currently have a few problems. No editing tool, no built-in way of designing snippets easily. It is one way. It injects the snippet into the Editor and when done, forgets everything about the Expressions. The Refactoring Engine and the Snippet are not friends. They do not communicate.

Code Snippets

Lisa Feigenbaum - Code Snippets in Visual Studio
Ken Levy takes his camcorder over to interview Lisa Feigenbaum and get a look at the new Code Snippets feature in Visual Studio.

Code Snippets Solution applied to all code files, LIVE

A Code Snippet should not “lose” knowledge of the parameters/expressions. Anytime, I click on an Expression (example: PropertyName) that was inserted with a Code Snippet or NOT, it should still understand it. Refactoring would be automatic. Why should it be two separate tasks? Have you ever changed a Property Name, without wanting to change where its referenced?

Consider adding the “prop” Code Snippet. It asks you for Field and PropertyName. Now I believe that this is “kind-of” there for Visual Studio 2010.

But the key missing ingredient, is having our code “always aware”, especially of “expressions”. If you click on a Field it should highlight and as you make the rename change, it should alter your references automatically.

But a Code Snippet is only a small utility, but I wanted to use it as a small example of how code snippets need to keep context and knowledge of its injection, and in the same way Code generation in general has to be able to do this too.

If you generate a class for my person table in a database, and I change the class name, it should, by means of Code Snippet Expressions, Refactoring Engine, understand and align the change across to the database and the the code that references it.

Tags: ,

Jan 25 2009

Code Generation Tools

Category: c# | CodeGenWeek | Design | Development Tools | DOTNETJonathan @ 18:11

All your code are belong to us

Send me a list of your favorites and I will compile a list of them on the website. For now, here are some ad-hoc random ones to perhaps try out. if you know anything about any of these, let me know what you think of them.

http://www.codesmithtools.com/
http://www.ibm.com/developerworks/rational/library/2949.html http://www.tangiblearchitect.net/visual-studio/
http://www.raboof.com/Projects/VsCodeGeneratorShim/
http://www.microsoft.com/downloads/details.aspx?familyid=89e6b1e5-f66c-4a4d-933b-46222bb01eb0&displaylang=en
http://www.jetbrains.com/resharper/
http://www.oracle.com/technology/tech/dotnet/tools/index.html
http://www.mygenerationsoftware.com/portal/default.aspx
http://www.codeplex.com/jeremydotnet
http://www.altova.com/solutions/code-generation-tools.html
http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/
http://www.eurowisesoft.com/
http://www.dotnetsavant.com/
http://www.adonetexpress.com/
http://www.bluage.com/
http://www.razorsource.com/SourceCutter/Overview.aspx
http://www.developerinabox.com/
http://www.innoq.com/iqgen/index.html
http://www.sd.nl/software/
http://www.radsoftware.com.au/codegenerator/

Steve Hansen sent me a link to m-Power

Tags:

Jan 24 2009

One important reason why your Code Generation tool does not get used

Category: .NET | c# | CodeGenWeek | Design | Development Tools | DOTNET | TalkWareJonathan @ 04:20

You say “m_” , I say “_”, lets call the whole thing off!

Code Generation has one tremendous problem.
We dont like the way the code looks.
Majority of the code spat out, is, if not badly formatted, then not formatted the way I like.


Hi, after you read this post, don't miss my Interview with Kent Beck coming soon.


and it does not change the formatting when you don't like the way it was generated or you have to fiddle with codegen settings (which for every tool is different). There are other reasons of course why codegen is not 100% accepted, like you cannot edit the generated code. If you do, it mostly quickly gets overwritten by the next run of the codegen.
But this is already being addressed (a little) by partial classes in .NET, BUT there is still no answer for the formatting problem.

AND let's face it, if any helpful and usable code was generated and it looked like our own code, we would be more inclined to use it.

Introducing JIT Code Formatting

it is not altering the actual layout of the file, it's just the "view" of it.
Just in time code formatting is the process of changing the formatting of any code, no matter how it looks on disk, "to the way you want it". Imagine I send you a small project, you open it and find I have prefixed all fields with an underscore. Your development tool, understanding the semantics and changes the variable prefixes for you when you open it. It formats out the code the way you like it and likewise, when I open it, its changes it the way I like it. It is not altering the actual layout of the file, it's just the "view" of it.

Checking the source into Source Control is not a problem, as it would convert it to a consistent format for before checking in.

For example: In Visual Studio, you would have settings for how you want your formatting. The formatting can be set and pushed to all developers and each developer can have the formatting the way they need and like it.

The JIT-CodeGen engine translates the code to your settings.

Now who wants to write this?

SideBar

Built-in CodeGen in Visual Studio

The T4 templates are an example of a powerful feature in Visual Studio, incorporating code generation, however it will generate the code based on the developers ideas, conventions and standards. This is one of the problems of codegen adoption.
However T4 from Visual Studio is still very cool. Read a little intro by Rob and from Scott

There are many new features coming in Visual Studio 2010, but where is this feature? There is still time, to add it in.

Listen Now:




Download

CodeGenWeekPodCast1.mp3 (7.55 mb)


iTunes

Copy this address: http://www.jonathancrossland.com/syndication.axd and paste it into iTunes.



Tags: , , , , , ,

Jan 5 2009

Accessors for Object Assignment

Category: c# | DOTNETJonathan @ 08:50
How much code do you write, how many bugs occur because one of your objects is null, not set by some piece of code? How many lines of code includes, if (this!=null) etc ? I have seen my fair share of null reference exceptions and you have probably too.

So with Null Reference Exceptions being a very big part of development still, we really do need a way to combat it better. One way I believe would help is by giving us the opportunity to have Accessors on an Object. Just like we have a Constructor, the Accessor, should have the same name, but contain a get and set declararion.

More...

Tags:

Jan 2 2009

Interface Contracts and Access Modifiers

Category: .NET | c# | Design | Design Patterns | DOTNETJonathan @ 05:05
Easier than before, we create Interfaces and contracts quickly and easily, with little or no attention to detail.

Increasingly I have the opinion that we are in a need to control and define our contracts to levels yet unseen. As a base for this, comes the "Public is NOT Published" thought from Martin Fowler

And before you say combinations of ObsoleteAttribute, InternalsVisibleTo and public, private, internal modifiers etc - They do not all add up to the same thing!

Martin Fowler said here and also he said it eloquently enough here , for me to not rehash the argument.


But, "Publishing an Interface" must be thought of as being as complex as in any print media business, where the term is strong. If we are creating contracts, its a shame we have much less control on it than we thought.

And there are a few ways of thinking about what we need.

Do we need access modifiers of type published/unpublished or Attribute based?
Can it be role based, versioned, per client? (I wish to publish these two additional methods to the Administrators group)
Should it be Attributed/Annotated with tools or within the language itself.?

From an Artima interview Erich Gamma says "And in Eclipse we have the conventions for that difference. Actually we even have tool support. In Eclipse 3.1 we have added support for defining rules for which packages are published API. These access rules are defined on a project's class path. Once you have these access restrictions defined the Eclipse Java development tools report access to internal classes in the same way as any other compiler warnings. For example you get feedback as you type when you add a dependency to a type that isn't published."

Is it a good feature in Eclipese? Should we not have something as powerful in Visual Studio and languages?

So where will it go?

Well, I think its extremely important for our future contracts and interfaces to be malleable, and towards that end, Duck Typing , dynamic "versioned" proxies, and other technologies need to come into the frame.

I also believe that a split between Internal and External lines is needed within our code.

Example: Partial Methods is an internal tool, where as Partial Classes is an External tool. By internal, the Partial Method, can never be public, since its an internal feature of the class. The Partial Class can only be declared and partial'ed within the same .NET Assembly Module. This kind of feature (and many more to come) will aid Code Generation directly, making it a far more reliable and superior form of tool. We require these 'internal' helpers for code maintenance and as a "code file splitting technique", but we also need the External (traditionally Interfaces) to be revved up.

btw: Partial methods and classes is one of my favourite features. I like the way it now embodies a design pattern into the inner workings. (Template Method Design Pattern)

CodeGen meets Refactoring, meets new Interface Contract

Consider a tool in your IDE, where you select a class, and select 'Build Facade', which presents you with options and then ultimately creates an Adaptor/Facade for the selected model.

Can Rational do this?

However, importantly it will also set up the access modifiers for the selected classes which will allow us to present the Adaptor to a Client, without them seeing, being confused by, wanting access to additional non essential interfaces. In effect, we now have a new Assembly in Visual Studio, named, code generated talking to the bottom layers. The bottom layers would be marked as InternalsVisibleTo the new generated assembly. I can now supply the Adaptor/Facade to a client. I can then version that off, and create a new generated version of it. We can Select which members to Publish as a set and which to remain public

But thank MS for InternalsVisibleTo

By using the internal and public modifiers. together with InternalsVisibleTo Attribute on the Assembly, the client code, will be able to
  • Call the public member
  • Call internal member (if inheriting from the class)
  • Call the internal member (if InternalsVisibleTo) is added to the assembly attributes




namespace ClassLibrary1
{
    public interface InterfaceA
    {
        void methodA();
    }

    public class TypeX : InterfaceA
    {
        public TypeX() {}

        public void methodA() {}

        public void methodB(){}

        internal void methodC(){}

        private void methodD() {}
    }

    public class TypeY : TypeX
    {
        public TypeY()
        {
            //I can see
            base.methodA();
            base.methodB();
            base.methodC();
        }

        public void methodD() {}

        internal void methodE() {}

        private void methodF() {}
    }
}


So with the code above in one Assembly and the code below in another referenced assembly, the methods you see below are visible.


    //Client Code within a different Assembly.
    public class Class1 : ClassLibrary1.InterfaceA
    {
        public Class1()
        {
            //base contains nothing

            ClassLibrary1.TypeX x = new ClassLibrary1.TypeX();
            x.methodA();
            x.methodB();
            x.methodC(); //if InternalsVisibleTo
            
            ClassLibrary1.TypeY y = new ClassLibrary1.TypeY();
            y.methodA();
            y.methodB();
            y.methodC();//if InternalsVisibleTo
            y.methodD();
            y.methodE(); //if InternalsVisibleTo

        }

        #region InterfaceA Members
        void ClassLibrary1.InterfaceA.methodA() { }
        #endregion
    }

    public class Class2 : ClassLibrary1.TypeX
    {
        public Class2()
        {
            base.methodA();
            base.methodB();
            base.methodC(); //if InternalsVisibleTo

            ClassLibrary1.TypeX x = new ClassLibrary1.TypeX();
            x.methodA();
            x.methodB();
            x.methodC();//if InternalsVisibleTo

            ClassLibrary1.TypeY y = new ClassLibrary1.TypeY();
            y.methodA();
            y.methodB();
            y.methodC(); //if InternalsVisibleTo
            y.methodD();
            y.methodE(); //if InternalsVisibleTo
        }
    }

    public class Class3 : ClassLibrary1.TypeY
    {
        public Class3()
        {
            base.methodA();
            base.methodB();
            base.methodD();
            base.methodC(); //if InternalsVisibleTo

            ClassLibrary1.TypeX x = new ClassLibrary1.TypeX();
            x.methodA();
            x.methodB();
            x.methodC(); //if InternalsVisibleTo
            
            ClassLibrary1.TypeY y = new ClassLibrary1.TypeY();
            y.methodA();
            y.methodB();
            y.methodC(); //if InternalsVisibleTo
            y.methodD();
            y.methodE(); //if InternalsVisibleTo
        }
    }


InternalsVisibleToAttribute needs to also be available to Classes and Members, as well as excluding a member from visibility to the outside. Also it should not be for internals only, but also for public, so therefore VisibleTo Attribute, and ExcludeTo Attribute



Tags: , , , ,