Cherian
Cherian

Reputation: 19418

Are anonymous types a good thing to use outside of LINQ?

While analyzing some ASP.NET MVC projects I got to see anonymous types scattered all over.

HTML helpers have them:

<%=Html.TextBox("view.Address", "address", new { Class = "text_field" })%>

A lot of the return types for actions have them:

JsonNetResult jsonNetResult = new JsonNetResult
{
  Formatting = Formatting.Indented,
  Data = new {Something= “”}
}

I know this came from LINQ:

from p in context.Data
select new { p.Name, p.Age };

Are these really the correct way to accomplish things now outside of LINQ? Do they hurt code reusability and readability?

Upvotes: 3

Views: 393

Answers (5)

user83642
user83642

Reputation: 85

I agree

ditto passing around of lambdas and delegates instead of finding coherent domain level abstrations

Upvotes: 0

JaredPar
JaredPar

Reputation: 755457

IMHO, the biggest problems with anonymous types stem from the inability to name their type. That is, it's not possible to expilictly specify the type of an anonymous type as an expression in code. This really makes in awkward to do things like create a generic List.

var list = New List<No way to specify an Ananymous type>();

Usually you have to resort to a helper method.

public List<T> CreateList<T>(T notUsed) {
  return new List<T>();
}
var list = CreateList(new { .Class = "foo" });

This also has a larger impact in that you can't use an anonymous type as a return type, makes casting extremely awkward (need a helper method), etc ...

But these are not the operations that Anonymous Types were designed for. In many ways they are designed to used within a particular defined function and it's subsequently created lambda expressions. Not as a data communication type between to full fledged functions. This is certainly a limitation in the design and at times drives me batty. But overall I find them to be a very useful construct in the language.

Many parts of LINQ would not be possible without them in some form.

Upvotes: 5

Lennaert
Lennaert

Reputation: 2465

I use them regularly to fill GridViews and DataGridViews since it saves a lot of time configuring the GridView to only show the columns I'm interested in.

Apart from settings DataSources, however, I think it would be bad for readability -- a good abstraction is probably better.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532695

When the object being created is a transitory object, i.e., it's immediately consumed or converted into something else, I like the idea of anonymous types. It prevents you from littering your code with single-purpose classes whose only use is as a short-lived container. Your examples are typical of the types of uses where it comes in handy, i.e., in the helper extensions it's almost always immediately converted into a parameter dictionary and with the json result it gets serialized. If the class has domain significance or needs to be used as a first-class object, then by all means create a domain class for it.

Upvotes: 3

user83286
user83286

Reputation:

I think of them as a better way of constructing dictionaries. Especially in your samples, what's the alternative? Passing an IDictionary<String,Object>? I think the anonymous type is more readable.

Upvotes: 1

Related Questions