简体   繁体   中英

Interface help for a Task<T> method

I'm trying to decouple some concrete classes and linking using SimpleIOC from MVVMLight. This is my class and I wanted to make an interface I can then register with simpleioc

public class SerialisationService : ISerialisationService
{

    private static async Task<T> LoadLocalXMLAsync<T>(string filename)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            T o = default(T);
            using (IInputStream inStream = await file.OpenSequentialReadAsync())
            {
                var serializer = new DataContractSerializer(typeof(T));
                o = (T)serializer.ReadObject(inStream.AsStreamForRead());
            }
            return o;
        }
        catch (Exception ex)
        {
            return default(T);
            // ERROR HANDLING AND LOGGING
        }
    }

    private async static Task SaveLocalXMLAsync(string filename, object o)
    {
        try
        {
            var serializer = new DataContractSerializer(o.GetType());
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                serializer.WriteObject(fileStream, o);
                await fileStream.FlushAsync();
            }
        }
        catch (Exception ex)
        {
            // ERROR HANDLING AND LOGGING
        }
    }

}

I've tried a few things and can't work out how I define a generic interface for returning Task in this case.

I need to register using

SimpleIoc.Default.Register<ISerialisationService, SerialisationService>();

The Interface:

public interface ISerialisationService
{
    Task<T> LoadLocalXMLAsync<T>(string filename);
    Task SaveLocalXMLAsync(string filename, object o);
}

The Implementation:

public class SerialisationService : ISerialisationService
{

    public async Task<T> LoadLocalXMLAsync<T>(string filename)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            T o = default(T);
            using (IInputStream inStream = await file.OpenSequentialReadAsync())
            {
                var serializer = new DataContractSerializer(typeof(T));
                o = (T)serializer.ReadObject(inStream.AsStreamForRead());
            }
            return o;
        }
        catch (Exception ex)
        {
            return default(T);
            // ERROR HANDLING AND LOGGING
        }
    }

    public async Task SaveLocalXMLAsync(string filename, object o)
    {
        try
        {
            var serializer = new DataContractSerializer(o.GetType());
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                serializer.WriteObject(fileStream, o);
                await fileStream.FlushAsync();
            }
        }
        catch (Exception ex)
        {
            // ERROR HANDLING AND LOGGING
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM