Click here to Skip to main content
15,849,262 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic interface and generic delegate in simple terms. PinPopular
Pete O'Hanlon14-Apr-15 5:53
subeditorPete O'Hanlon14-Apr-15 5:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Simon_Whale14-Apr-15 6:16
Simon_Whale14-Apr-15 6:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 6:53
subeditorPete O'Hanlon14-Apr-15 6:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 8:13
professionalBillWoodruff14-Apr-15 8:13 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 8:22
subeditorPete O'Hanlon14-Apr-15 8:22 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Gyana_Ranjan Dash14-Apr-15 20:30
Gyana_Ranjan Dash14-Apr-15 20:30 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 21:03
subeditorPete O'Hanlon14-Apr-15 21:03 
AnswerRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 9:05
professionalBillWoodruff14-Apr-15 9:05 
This is an "alternate take" on the code example using a generic interface provided on this thread by Pete O'Hanlon. It uses a pattern I've used before which is slightly different than the in Pete's example; of course I do not claim this is any "better" than Pete's example !

I've made sure this example actually works by testing it. 'DataContract/DataMember is used for serialization/de-serialization. I'd really appreciate any feedback on this code vis-a-vis its differences with Pete's implementation.

C#
using System;
using System.IO;
using System.Runtime.Serialization;

namespace YourNameSpace
{
    public interface IPersist<T> where T : class
    {
        void LoadLayout();
        void SaveLayout();
    }

    [DataContract]
    public class MyModel : IPersist<MyModel>
    {
        // changed from VS 2015 legal auto-property with 'get only
        // to readonly string because VS 2013 does not that syntax
        private readonly string FilePath;

        public MyModel(string name, string fname, string filepath)
        {
            Name = name;
            FriendlyName = fname;
            FilePath = filepath;
            LastLogin = DateTime.Now;
        }

        [DataMember]
        public string Name { private set; get; }

        [DataMember]
        public string FriendlyName { private set; get; }

        [DataMember]
        public DateTime LastLogin { private set; get; }

        public void LoadLayout()
        {
            if (!File.Exists(FilePath)) throw new FileNotFoundException("Invalid file path");

            MyModel temp;

            try
            {
                temp = Persistence<MyModel>.LoadLayout(FilePath);
            }
            catch (Exception ex)
            {
                throw new InvalidDataContractException(string.Format("Failed to read data file: {0}", ex.Message),
                    ex.InnerException);
            }

            if (temp == null) throw new InvalidDataContractException("no data error");

            Name = temp.Name;
            FriendlyName = temp.FriendlyName;
            LastLogin = temp.LastLogin;
        }

        public void SaveLayout()
        {
            if (!File.Exists(FilePath)) throw new FileNotFoundException("Invalid file path");

            try
            {
                Persistence<MyModel>.SaveLayout(FilePath, this);
            }
            catch (Exception ex)
            {
                throw new InvalidDataContractException(string.Format("Failed to save to data file: {0}", ex.Message),
                    ex.InnerException);
            }
        }
    }

    internal static class Persistence<T> where T : class
    {
        // changed to private variable per suggestion Richard Deeming
        private static DataContractSerializer serializer;

        public static T LoadLayout(string filepath)
        {
            serializer = new DataContractSerializer(typeof (T));

            Console.WriteLine("Loading");

            T LoadedData = null;

            using (var reader = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                LoadedData = serializer.ReadObject(reader) as T;
            }

            return LoadedData;
        }

        public static void SaveLayout(string filepath, T instance)
        {
            serializer = new DataContractSerializer(typeof (T));

            Console.WriteLine("Saving");

            using (var writer = new FileStream(filepath, FileMode.Create, FileAccess.Write))
            {
                serializer.WriteObject(writer, instance);
            }
        }
    }
}

«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"


modified 15-Apr-15 13:03pm.

GeneralRe: Generic interface and generic delegate in simple terms. Pin
Richard Deeming14-Apr-15 10:18
mveRichard Deeming14-Apr-15 10:18 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 13:21
professionalBillWoodruff14-Apr-15 13:21 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Richard Deeming15-Apr-15 2:03
mveRichard Deeming15-Apr-15 2:03 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 3:58
professionalBillWoodruff15-Apr-15 3:58 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Sascha Lefèvre15-Apr-15 4:39
professionalSascha Lefèvre15-Apr-15 4:39 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 10:32
subeditorPete O'Hanlon14-Apr-15 10:32 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 13:35
professionalBillWoodruff14-Apr-15 13:35 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 21:37
subeditorPete O'Hanlon14-Apr-15 21:37 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen14-Apr-15 11:16
professionalEddy Vluggen14-Apr-15 11:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 13:41
professionalBillWoodruff14-Apr-15 13:41 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 1:24
professionalEddy Vluggen15-Apr-15 1:24 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 4:08
professionalBillWoodruff15-Apr-15 4:08 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 4:16
professionalEddy Vluggen15-Apr-15 4:16 
QuestionHow do I get images without using picturebox? Pin
Member 1138819913-Apr-15 23:29
Member 1138819913-Apr-15 23:29 
AnswerRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 0:25
mveRichard MacCutchan14-Apr-15 0:25 
GeneralRe: How do I get images without using picturebox? Pin
Member 1138819914-Apr-15 0:59
Member 1138819914-Apr-15 0:59 
GeneralRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 1:02
mveRichard MacCutchan14-Apr-15 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.