Why northwind linq code has unused methods

A

Andrus

I looked into northwind database sample code in MS Linq samples

Every entity has 3 partial methods:

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate();
partial void OnCreated();

OnLoaded and OnValidate are not called in any place.
Why they are in generated code ?

Why compiler does not throw error if partial method is not used in any place
? This is is coding error and must be addressed.

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

No, it's not a coding error. It's a partial method. If the partial
method is implemented in another area of the class, then that call will be
emitted to the IL on compilation. If not, then the compiler will not emit
any IL for the call.

The point is to allow for extensibility without having to define a base
class and methods on the base class which need to be overidden. The
OnLoaded, OnValidate, and OnCreated methods are only going to be called if
you implement them somewhere else in the class.

Otherwise, they do nothing.

In this code, those methods are there to expose hooks in the LINQ-to-SQL
process (after the object is loaded, validated, created, etc, etc) without
adding excessive overhead.
 
N

Nicholas Paldino [.NET/C# MVP]

Sorry to respond to my own post, but you can also download the C# 3.0
language spec at:

http://download.microsoft.com/downl...5351c669b09/CSharp Language Specification.doc

Check out section 10.2.7, "Partial Methods" (page 276) for more
information on why what you see is valid.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Nicholas Paldino said:
Andrus,

No, it's not a coding error. It's a partial method. If the partial
method is implemented in another area of the class, then that call will be
emitted to the IL on compilation. If not, then the compiler will not emit
any IL for the call.

The point is to allow for extensibility without having to define a base
class and methods on the base class which need to be overidden. The
OnLoaded, OnValidate, and OnCreated methods are only going to be called if
you implement them somewhere else in the class.

Otherwise, they do nothing.

In this code, those methods are there to expose hooks in the
LINQ-to-SQL process (after the object is loaded, validated, created, etc,
etc) without adding excessive overhead.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Andrus said:
I looked into northwind database sample code in MS Linq samples

Every entity has 3 partial methods:

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate();
partial void OnCreated();

OnLoaded and OnValidate are not called in any place.
Why they are in generated code ?

Why compiler does not throw error if partial method is not used in any
place ? This is is coding error and must be addressed.

Andrus.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
Sorry to respond to my own post, but you can also download the C# 3.0
language spec at:

http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-
75351c669b09/CSharp%20Language%20Specification.doc

Check out section 10.2.7, "Partial Methods" (page 276) for more
information on why what you see is valid.

In this case, however, it's useless. Nothing in the designer code
*calls* OnLoad or OnValidate - so there's no benefit in the methods
being there.

My guess is that they *are* called if particular options are selected
in the designer, but without any calls there's no reason to have them.

They're not doing any harm other than complicating the designer-
generated source for no benefit, and I wouldn't say this "must be
addressed" but it's not ideal.
 
N

Nicholas Paldino [.NET/C# MVP]

I agree, not ideal, but at the same time, it's not an error with the
compiler, as the OP suggests.
 
A

Andrus

Jon,
They're not doing any harm other than complicating the designer-
generated source for no benefit, and I wouldn't say this "must be
addressed" but it's not ideal.

I think C# 3.5 compiler *must* generate warning or error in this case just
like for unused variables.

It is not reasonable to have unused partial method declarations in code.
Looks like sqlmetal is not finished.

Compiler must detect as much potential issues as possible.

Andrus.
 
J

Jon Skeet [C# MVP]

Andrus said:
I think C# 3.5 compiler *must* generate warning or error in this case just
like for unused variables.

No, there's no *must* involved. If you can show me the spec requirement
that says it must do it, then I'll agree. Until then, it's just your
opinion that it would be a good thing.

Bear in mind that partial methods can be called from *any* file making
up the partial type. Here's an option:

Generator A creates FileA.cs. This contains the partial method
declaration - it knows that other generators may wish to call it, and
other manual code may wish to implement it.

Generator B creates FileB.cs, but doesn't need to call the partial
method, so doesn't.

Human creates FileC.cs, and doesn't choose to implement the partial
method.

Why should there be a warning? Because generator B chooses not to call
the partial method?

Partial types generally involve two or more entities (human or code
generators) which don't necessarily know the exact needs of the others.
With the warning you're suggesting, you're binding the different
entities too tightly.
It is not reasonable to have unused partial method declarations in code.
Looks like sqlmetal is not finished.

Show me any code which is perfect. Is yours? Yes, it looks like a
mistake - but a reasonable one.
Compiler must detect as much potential issues as possible.

While it is nice if it does so in some cases, there's no "must"
involved here to my mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top