Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesSomethingController ClassSomethingController Class
Previous
 
Next
New Post
4/11/2012 8:32 AM
 

I've develop many modules for my company and I've a question about components controller class.

Inside controller class, is it possible to use static methods to avoid class instance.
So we can use like this:

var mylist = mycontroller.GetItem()
vs
var listcontroller = new mycontroller();
var mylist = listcontroller.GetItem()

I read the source code of HTMLModule from DNN6.2 and they continue to use the second solution.
Is there a particular reason?

 
New Post
4/11/2012 9:32 AM
 

You absolutely can use static methods in your controller class.  In fact we started doing that for most of the core framework as we were updating methods.  The only thing you need to watch out for is if your method requires you to maintain state, in which case static methods are not appropriate.


Joe Brinkman
DNN Corp.
 
New Post
4/11/2012 9:37 AM
 
Hi Joe,
Thanks for this information: Cool, I'm on the good way :-)

Have you a sample when you say "maintain state"
At this time I've used static on all the methods (insert, update, delete, getXXX)
 
New Post
4/11/2012 10:13 AM
 

Hi Fabien,

Of course you can use static methods for your own modules. DNN don't do it simply because it's not optimal for a CMS to have all controllers using static methods. When a static class/method is called, the memory used is never released. Now imagine if you have undred modules doing the same! From MSDN:

"As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides."

Moreover it's not always a good idea, because all members (fields, variables, etc.) have to be static. There is possible case where this could generate problems like sharing the same value from different calls.

On the other hand, a classic module use the singleton pattern for the SqlDataProvider part. This is more efficient because several module instances can share the same db connection.

Gilles


We (team members) are Humans offering their knowledge, their work and their spare time FOR FREE to benefit the community. It would be so particularly appreciated that your messages begin with "Hello" and end with "Thank you" or any other form of politeness. Ask yourself what your reaction would be, if you were approached by me (a total stranger) on the street to ask you something without saying "Hello" nor "Thank you"? After several years of services dedicated to the community, I begin to be tired to read requests without any form of politeness.
 
New Post
4/11/2012 11:06 AM
 
Hi Gilles,

I'm agree with your about memory allocation. But controller class contains only methods like this sample.

public static List GetPerformances(int moduleId, string taskRef)
{
return CBO.FillCollection Data.DataProvider.Instance().GetPerformances(moduleId, taskRef));
}

You think it's not optimal?
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesSomethingController ClassSomethingController Class


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out