PC Review


Reply
Thread Tools Rate Thread

C# constructor best practice: how much logic to place in a C#constructor

 
 
Jon
Guest
Posts: n/a
 
      11th Nov 2009
Is there a best practice for how much logic, and what type of logic,
to place in a C# constructor?

For example, if I have setup logic that might take 20 seconds to
several minutes to complete, should that be placed in a C#
constructor?

This involves replicating data from a remote service into a local
database, and so it sometimes is quick, but if there is a large amount
of data, it could take several minutes to complete.

Are there any good books or web pages that explain C# constructor best
practices?
 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      11th Nov 2009
Jon wrote:
> Is there a best practice for how much logic, and what type of logic,
> to place in a C# constructor?


IMHO: as little as possible.

> For example, if I have setup logic that might take 20 seconds to
> several minutes to complete, should that be placed in a C#
> constructor?


20 seconds is a stretch, but in just the right circumstance you might be
able to get away with it. Several minutes? No way.

Use a factory or an initialization method for lengthy
initialization-related operations.

If nothing else, a lengthy operation in a constructor strongly implies
that in the constructor, the object "this" reference is "leaking" like
crazy, which is a big no-no. The "this" reference should not be exposed
to external code until the object is fully constructed.

If the implication is wrong, then that means all that initialization
code had nothing to do with the "this" reference, and so could precede
its existence (e.g. by being executed in a factory method). On the
other hand, if the implication is right, then in theory there's a bunch
of initialization code that really needs a fully-constructed object, in
which case an initialization method could be more appropriate.

> This involves replicating data from a remote service into a local
> database, and so it sometimes is quick, but if there is a large amount
> of data, it could take several minutes to complete.
>
> Are there any good books or web pages that explain C# constructor best
> practices?


I don't know of any. Surely a "good book" would only address this as a
small subset of its content. I suppose a blog entry or other web page
might exist. Google can help you find that.

Of course, wait an hour or so, and this post will be on Google, viewable
as a single web page. Then there will be at least one web page
addressing the question.

Pete
 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      11th Nov 2009
* Jon wrote, On 11-11-2009 21:59:
> Is there a best practice for how much logic, and what type of logic,
> to place in a C# constructor?
>
> For example, if I have setup logic that might take 20 seconds to
> several minutes to complete, should that be placed in a C#
> constructor?
>
> This involves replicating data from a remote service into a local
> database, and so it sometimes is quick, but if there is a large amount
> of data, it could take several minutes to complete.
>
> Are there any good books or web pages that explain C# constructor best
> practices?


The constructor shouldn't really need exceptionally much error handling
and should take a reasonable amount of time.

Plus, it sounds like the bulk of this work could be offloaded to a
separate thread, making the constructor fast and giving you the
opportunity to implement an event signaling that the object is ready for
use.

I'd refactor the time consuming and possibly error prone stuff out of
the constructor.

--
Jesse Houwing
jesse.houwing at sogeti.nl
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
some logic for constructor André Freitas Microsoft ASP .NET 2 13th Jul 2009 02:26 AM
What's 'best practice'--instantiate in the constructor or outside it for collection class? raylopez99 Microsoft C# .NET 16 13th Aug 2007 11:01 PM
overloaded constructor, access zero-argument constructor mblatch Microsoft C# .NET 3 8th Apr 2005 09:53 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:29 AM.