mayank.karki
mayank.karki

Reputation: 748

How to use PCLStorage in Portable class library?

I have a Portable class library which I am going to use for Windows 8 and phone apps. So I created a common viewmodeland, and now I want to handle storage functionality in viewmodels. I found a PCLStorage package which is used to deal with this scenario.

I have a code like this on Windows 8 and now I want to write this in PCL using PCLStorage.

 public static object LoadSettings(string key)
    {            
        if (Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
        {
            if (Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] != null)
            {
                return Windows.Storage.ApplicationData.Current.LocalSettings.Values[key];
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

I found a way, I have created a interface for Storage used in viewmodel. And have created a class to handle storage for phone and metro in respective projects. When I am creating a object of MainViewModel I am passing storage object also.

So I want to know, is this right?

Upvotes: 3

Views: 2573

Answers (2)

Daniel Plaisted
Daniel Plaisted

Reputation: 16744

I found a way, I have created a interface for Storage used in viewmodel. And have created a class to handle storage for phone and metro in respective projects. When I am creating a object of MainViewModel I am passing storage object also. So i want to know that Is this right?

Yes, this is a good solution. Dan's answer is basically just the same thing but using an IoC container.

You can also try the Xam.Plugins.Settings library, which basically does for settings what PCL Storage does for File IO.

Upvotes: 0

Dan
Dan

Reputation: 2858

I believe the simplest pattern would be to Register the current-platform-defendant-implementation within the container bootstrapper on the 'specific' 'shell' for platform and get the dependenciy in the constructor Or simply skip the IoC thing and instantiate stuff manually

using MyInfrastructureAssembly.Interfaces;
public MyApp: App // MyNewPlatFormApp
{
public override Initiaze()
{
    var bootsrapper =  new Boostrapper(MyIoC.Current);
}
}
public class Bootsrapper: TheBootStrapperOfMyIoc
{ 
   public Bootsrapper(IocContainer container)
   {
    Container = container;
   }
   public override Register()
   {
     Container.Register<IMyAbstractedService,MyPlatformDependantService>();   
   }
} 
MyPlatformDependantService : IMyAbstractedService 
{
public object Get(); // IMyAbstractedService.Get()
}
public class MyViewModel:ViewModelBase
{
IMyService  MyService {get;set;}

MyViewModel(IMyAbstractedService myServcice)
{       
    MyService = myService;
}

public object Thing // LazyThing provided by IMyAbstractedService 
{
    get
    { 
        if(_thing!=null)
        return _thing;
        return _thing = GetIt();
    }
    set 
    {
        if(Equals(value,_thing)) return;
        _thing = value;
        base.NotifyMagicalChanges()
    }
}
    public void GetIt()
{       
    MyServcie.Get();
}
}

Static Version : Runs on LinqPad

enter code here
private async void CheckIfExist()
    {
        try
        {
            var isoStore = FileSystem.Current.LocalStorage;

            var folder = await isoStore.CreateFolderAsync("xxx", 
CreationCollisionOption.ReplaceExisting);                

            ExistenceCheckResult result = await  isoStore.CheckExistsAsync("xxx");

            switch (result)
            {
                case (ExistenceCheckResult.FolderExists):
                    Console.WriteLine(":)"); break;
                case (ExistenceCheckResult.NotFound):
                    Console.WriteLine(":("); break;
            }
        }
        catch (Exception)
        {
            Console.WriteLine(":<");
        }
    }'

Upvotes: 1

Related Questions