Using Parse in a PCL or .NET Class Library

Hi, for the last fews day I have been trying to implement Parse into either a PCL or .NET Class Library in my Cross Platform (Not Forms) Solution, however I have not been very successful in my endeavor thus far. I would like to for both my iOS and Android app to refer to the same shared functions instead of duplicating the exact code for each.

I keep finding myself on the thread below, and have followed the instructions within, however I consistently get an error stating that Parse has not been initialized.
http://forums.xamarin.com/discussion/14934/what-s-the-best-approach-to-sharing-code-using-parse-sdk-on-ios-android

I do have the code to initialize Parse in my AppDelegate file, as well as my MainActivity. Everything was working fine when calling the Parse Quick Start SDK test code within the iOS or Android project, but not in the .NET Library or PCL.

If there is any confusion, I currently have a PCL and .NET Library in my Solution because I wasn't sure if one was more ideal for what I'm trying to do over the other, so was trying with both. The .NET Library seems to cause a bunch of errors regarding assembly reference after building the project a few times each coding session (So I find myself restarting Xamarin frequently), but also seems to be OK with running on a Simulator / Device and using any methods or functions form the Library without caring about the errors (Which is a very strange but separate issue).

Another important piece of information is that I downloaded the Parse SDK 1.6.2, and have iOS referencing Parse.iOS.dll, Android referencing Parse.Android.dll, and the Shared Libraries referencing Parse.dll.

So basically my question is: Has anyone found success in using Parse strictly in a Shared Library / PCL, and can offer simple and clear instructions on how one can do this? I can't imagine there being many steps or being difficult, I think I'm just in need of clear step by step instructions.

Thanks so much for all and any help!

Best Answer

Answers

  • MarcSabinMarcSabin USMember

    Still haven't had any luck with this.. anyone able to shed some light?

  • MarcSabinMarcSabin USMember

    Hi @rmacias, thanks so much for your response!

    Certainly sounds like the correct way of doing it, and am attempting to get this working in my project at the moment. So the code you shared is to be used in a Cross Platform Shared Project? Wasn't too sure if it was possible to reference Parse in that kind of project, and only by using a PCL or .NET / generic project so it can be referenced.

    However, personally I need to study up on the use of IOC Containers, as I haven't worked with them yet. So that may be my misunderstanding. I'm going to see if I can get tinyioc working with this and can post my results.

    If you or anyone has any recommendations of another ioc container and maybe some beginner friendly examples of IOC Container use, would sincerely appreciate it :smile:

    Thanks again!

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    I use MVVMCross a lot, so I use the built in IOC container that comes with it. But I hear good things about TinyIOC. I haven't used it though.

    Also, a Shared Project does not create a DLL. When you reference a Shared Project from your iOS or Android project, it is compiled against that platform. It's just like if you had the classes defined inside of your iOS or Android project. A PCL on the other hand does generate a DLL and it is compiled against the .NET Profile selected.

    In this case, Xamarin supports Profile 78, which supports Xamarin.iOS, Xamarin.Android, Windows 8, Windows Phone 8, and Windows Store. Think of a PCL is an intersection of the .NET Framework in where all those platforms I mentioned support.

    This explains a Shared Project vs PCL in more detail:
    https://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/

  • MarcSabinMarcSabin USMember

    Wow, so much easier than I thought it would be!! Your last explanation really helped me understand the Shared Projects more, as well as referring to and reading the info in the link you provided. Many thanks @rmacias!! :smiley:

  • SpencerSmithSpencerSmith USMember

    I don't quite understand the IOC Container part, what is that and how would I set it up?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    For a quick rundown on IOC, you can read this:

    http://arteksoftware.com/ioc-containers-with-xamarin/

    And view sample code here:

    https://github.com/RobGibbens/Xamarin.IoC

    Once you get the idea how that works, you would do something like this (based on my example above):

    //Somewhere in your Android & iOS Project
    //I Use MvvmCross, but any IOC Containter Will Work
    //This creates an instance of your Parse Service class and registers it with the IOC Container
    var myMusicService = new ParseMusicCatalogService();
    Mvx.RegisterSingleton<IMusicCatalogService>(myMusicService);
    
    //Then in your PCL, when you want to use the Parse Service, just resolve it from the IOC Container
    //Now you can access the same instance your created in your Android and/or iOS project.
    var parseService = Mvx.Resolve<IMusicCatalogService>();
    
  • SpencerSmithSpencerSmith USMember

    So just trying to understand better, you have your parse service class in a Shared Project (your ParseMusicCatalogService)? Or can this class be in the PCL?

    Then, in your individual projects you have the MvvmCross initialization lines.

    And finally in your PCL you have the code where you actually make the calls to your ParseMusicCatalogService class and handle data?

    Am I understanding how the project should be set up correctly?

    Thank you so much for your help by the way!

  • I was to use my dll and asmx web service. but it is very tough job to convert dll to pcl. i dropped this idea. and trying to use just asmx service but unfortunately, i am getting error below :

    _**_Severity Code Description Project File Line Suppression State
    Error CS0234 The type or namespace name 'XmlElement' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)'_
    **_

    Can someone figure out the exact problem?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    So just trying to understand better, you have your parse service class in a Shared Project (your ParseMusicCatalogService)? Or can this class be in the PCL?

    It's defined in the Shared Project and not the PCL. The Parse SDK needs to be compiled this way until they support it being Initialized from a PCL.

    Then, in your individual projects you have the MvvmCross initialization lines.

    Yes, using the iOC container of your choice.

    And finally in your PCL you have the code where you actually make the calls to your ParseMusicCatalogService class and handle data?

    Yes

    Am I understanding how the project should be set up correctly?

    Sounds like you got it!

    Thank you so much for your help by the way!

    Your welcome!

  • SpencerSmithSpencerSmith USMember

    @rmacias Thank you so much for your clarification!

    So just to finish clarifying. Which parse files to I include in each project?
    In the .iOS and .Android projects I'm assuming that I add the Parse.Android.ddl/Parse.iOS.ddl

    Which parse files do I add to the PCL and the Shared Project?

    Also just to clarify, I'm assuming that I add the NuGet package for the IOS library to both the PCL and the .iOS and .Android projects?

    Thanks again! Sorry for so many questions.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    So just to finish clarifying. Which parse files to I include in each project?
    In the .iOS and .Android projects I'm assuming that I add the Parse.Android.ddl/Parse.iOS.ddl

    Yes. In your iOS project, you add the Parse.iOS.dll. In your Android project you add your Parse.Android.dll

    Which parse files do I add to the PCL and the Shared Project?

    None of them. You're dealing with Abstractions at this point, and underneath the hood, all your Parse specific calls is being handled by either Parse.Android.dll or Parse.iOS.dll. So no need to add a reference in your PCL.

    Shared Projects don't contain references, and don't generate an actual binary. Read up on the difference from the link above.

    Also just to clarify, I'm assuming that I add the NuGet package for the IOS library to both the PCL and the .iOS and .Android projects?

    I manually downloaded the SDK. At the time I create my app, I don't think it was available from Nuget. All you need are references in your Android and iOS app. A reference from the PCL is not necessary.

  • SpencerSmithSpencerSmith USMember
    edited January 2016

    None of them. You're dealing with Abstractions at this point, and underneath the hood, all your Parse specific calls is being handled by either Parse.Android.dll or Parse.iOS.dll. So no need to add a reference in your PCL.

    @rmacias But wouldn't I need to add a reference to Parse.dll in my Shared Project ParseService class? Or else how would the class find the specific functions that need to be called? Maybe I'm not understanding completely how the projects and dependencies should be set up...

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited January 2016

    No, SharedProjects do not produce a DLL. They are compiled against your platform. It will use the reference from your Android or iOS project (i.e Parse.Android.dll or Parse.iOS.dll).

    https://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Shared_Projects

    Look at the picture. All files in a Shared Project are compiled in your App, unlike a PCL in where they are compiled against a .NET Profile.

  • SpencerSmithSpencerSmith USMember

    @rmacias Ohh okay, I think I understand now. So the Shared Project depends on the .iOS and .Android project, and in those projects is the Abstract class that the Shared Project then defines? I had the platform projects referencing the Shared Projects for some reason.

    So the PCL and the Shared Project will both reference the .iOS and .Android projects? Am I understanding it correct now?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    It'll become more obvious once you try it out in the IDE. Just setup your references as outlined above. If you continue to have issues, feel free to host a sample project on GitHub demonstrating the issue. Give it a shot and see how it works out.

  • SpencerSmithSpencerSmith USMember

    Alright I figured it out. I decided to just do all of the Parse functions calls from the Shared project instead of trying to make it visible to the PCL also. All I have is the ParseInitialize statements in the iOS and Android projects, then all of the parse code in the Shared Project, and that seems to work fine.

    Actually, I may just use a Shared Project instead of using a PCL (because I won't be sharing the dll with any other project) for all of my Core code. Is that a bad idea? Are you able to use NuGet packages in a Shared Project? I just don't see any other advantages over using a PCL after having read all of the documentation.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Alright I figured it out. I decided to just do all of the Parse functions calls from the Shared project instead of trying to make it visible to the PCL also.

    You don't have to. That's where dependency injection comes in using an IOC container. You just resolve an instance that you created inside of your Android and/or iOS app. Since the class implements an Interface (IMusicCatalogService), it does not need a concrete definition. (Notice that the ParseMusicService implements an interface, and when it gets registered with the IOC container, it registers using that Interface).

    Actually, I may just use a Shared Project instead of using a PCL (because I won't be sharing the dll with any other project) for all of my Core code. Is that a bad idea?

    No not necessarily. You can certainly define all your common code in a Shared Project.

    Are you able to use NuGet packages in a Shared Project?

    Again, Shared Projects are not actual projects, nor do they compile into a binary file. Shared Projects are no more than glorified file linking. All the files listed in your Shared Project get compiled into your Android or iOS app. It does not create a separate DLL that your app references. It works just like if your defined those classes in your Android or iOS app. Read the link above and it will explain everything in detail. You'll also notice that you can't add references inside of a shared project. That is because the Shared Project uses the references defined inside of your Android or iOS app.

    I just don't see any other advantages over using a PCL after having read all of the documentation.

    This is beyond the scope of your project, but the advantage of a PCL is having the ability to share my business logic (rules), models, and view models with not only my Android and iOS code, but with my Web Service code on the back end. But that topic is beyond the scope of this discussion. It sounds like you should just create a Shared Project. That should fulfil your requirements.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Well shucks, I guess I wouldn't spend the time to invest in Parse now. It's going away. Time to find an alternative.

    http://blog.parse.com/announcements/moving-on/

  • MarcSabinMarcSabin USMember

    Yes this is a very huge bummer.... Hmm.... Is there really anything out there that is similar and has decent support for Xamarin? Before looking into Parse, I was wanting to use Firebase but it's support for Xamarin is still very limited.

  • KLEVERTONANTUNESKLEVERTONANTUNES USMember ✭✭

    @rmacias said:
    I couldn't get the Parse SDK to work in a PCL directly, but got it to work in a Shared Library. I kept my PCL for all my other cross platform stuff. I then created a SharedLibrary that only contained my Parse related code. So my solutions looked like this:

    --Solution
    ----MyApp.iOS
    ----MyApp.Droid
    ----MyApp.Pcl
    ----MyApp.Shared

    MyApp.Shared only has one class, and it's just a Repository class for my Parse Data. So it looks something like this:

    namespace MyApp.Core.Services
    {
        public class ParseMusicCatalogService : IMusicCatalogService
        {
            private readonly string ParseApplicationId = "MyParseAppId";
            private readonly string ParseDotNetKey = "MyParseDotNetKey";
    
            public ParseMusicCatalogService()
            {
                ParseClient.Initialize(ParseApplicationId, ParseDotNetKey);
            }
    
            public async Task<List<SongTrack>> GetAllMusicByArtistOrSong(string artistOrSongName)
            {
                //Code to Query Parse
            }
    
            public async Task<List<string>> GetAllArtistNamesStartingWith(string startingCharacter)
            {
                //Code to Query Parse
            }
        }
    }
    

    From here, you can use an IOC container to register an instance of the concrete class from the iOS and Android projects. Then in your PCL, you can Resolve that instance from the IOC container and do all your calls from the PCL.

    I think the important point is that your concrete class needs to be compiled from either the iOS or Android project. It can't be compiled in the PCL. Since a SharedProject compiles with the platform (iOS or Android), that's why it works. And that is one use case in where you would use a Shared Project instead of a PCL.

    Hi @rmacias! Hope is not too late to get some help about this subject. This is a great solution and helped me a lot to define the architecture of an app that I'm working on, using a Parse server (back4app). I created the exact same structure that you suggested on this post.

    The only problem that I've found is when it gets to Unit Tests. I have a Unit Test Project and I'm using NUnit to write my tests. Apparently I can not unit test code written on the Shared Project, which is the code responsible for making the operations against the Parse Server. This is bad, because of course I'd like to have unit tests for this code, since it is the more important for the project.

    Have you ever faced this limitation? If so, is there any workaround?

    Thank you!

  • sarveshsinghsarveshsingh INMember ✭✭

    I am having trouble to either to use PCL or Shared Project! which situation we use PCL and which situation we use Shared for the Developr Point of View ??

    Thanks

Sign In or Register to comment.