DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Reflection.CreateObject is obsolete

 5 Replies
 2 Subscribed to this topic
 26 Subscribed to this forum
Sort:
Author
Messages
New Around Here
Posts: 2
New Around Here
New Member
New Member
You are a new member!
New Poster
New Poster
Congrats on posting!

Hello,

I am re-developing a custom module with its own data provider in a DNN 9.13 dev instance. In the DataContext class, I have to create the provider by using Reflection.CreateObject. I have a message telling me that this is obsolete and that I should use an overload with IServiceProvider. I could not find samples or documentation for this. Can someone help me to identify a documentation or example that I could learn from?

Thanks a lot.

Veteran Member
Posts: 349
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
Can you give some code sample, what do you mean by its own data provider exactly? APIs that required IServiceProvider are usually meant for internal usage only. There are ways around it but it usually is a bit of a code smell to do so.
New Around Here
Posts: 2
New Around Here
New Member
New Member
You are a new member!
New Poster
New Poster
Congrats on posting!

Dear Daniel,

Thanks a lot for your reply. My custom module is used to retrieve lists of documents to populate different tabs. Those documents are stored in a document management system. I am usinig its API to retrieve the necessary data for my module, hence my need for an abstract DataContext Class to link with DataProvider Class that handles the API calls to the document management system. Here is my code:

public abstract class MfilesDataContext
{

    #region "Shared/Static Methods"

    // singleton reference to the instantiated object 
    private static MfilesDataContext objProvider;

    // static constructor
    static MfilesDataContext()
    {
        CreateProvider();
    }

    // dynamically create provider
    private static void CreateProvider()
    {
        objProvider = (MfilesDataProvider)DotNetNuke.Framework.Reflection.CreateObject("mfiles", "Brs.Modules.MFiles.Data", "");
        //I have a warning here: "reflection.CreateObject(string, string, string) is obsolete. deprecated in DotNetNuke 9.11.3. Please use overload with IServiceProvider. Scheduled for removal in v11.0.0"
    }

    // return the provider
    public static MfilesDataContext Instance()
    {
        return objProvider;
    }

    #endregion


    #region "Abstract methods"

    public abstract IQueryable GetViews(string VaultName);

etc.

     #endregion

    }
}

Veteran Member
Posts: 349
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
The devil is in the details here, but why even use reflection for this?

I am no expert on the subject as I have been using EntityFramework in my own modules for many years. But would the patterns decribed in DAL2 work for your use case. See https://www.dnnsoftware.com/wiki/dal-2
Advanced Member
Posts: 132
Advanced Member
Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
2 Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
This code looks like the common DNN 4.x and 5.x patterns, also known as "DAL" which exposed certain things in a way that you had to use reflection and abstract classes, but the reality is that you don't need to do it that way any more.

My suggestion would be to look at the DAL 2 Pattern, or if you want a simple example this is a DAL2 way of doing data acess - https://github.com/IowaCo...ContentRepository.cs

Most likely you can just do a DAL 2 implementation and delete that older class.
New Around Here
Posts: 2
New Around Here
New Member
New Member
You are a new member!
New Poster
New Poster
Congrats on posting!

Dear Daniel and Mitchel,

Thank you very much! An eye opener! 

Indeed, this module was originally developed for DNN 4 and went trough all updates since. This time, I used Chris templates to make it as a SPA module and did not realize that I simply need to call my DataProvider instance from the Repositary class that inherits ServiceLocator. All is in order now.

All the best.
 

These Forums are for the discussion of the open source CMS DNN platform and ecosystem.

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

  1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
  2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
  3. 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.
  4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  5. No Flaming or Trolling.
  6. No Profanity, Racism, or Prejudice.
  7. Site Moderators have the final word on approving / removing a thread or post or comment.
  8. English language posting only, please.

Would you like to help us?

Awesome! Simply post in the forums using the link below and we'll get you started.

Get Involved