Click here to Skip to main content
15,357,293 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 4:53
mvaPete O'Hanlon14-Apr-15 4:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Simon_Whale14-Apr-15 5:16
professionalSimon_Whale14-Apr-15 5:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 5:53
mvaPete O'Hanlon14-Apr-15 5:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 7:13
mveBillWoodruff14-Apr-15 7:13 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 7:22
mvaPete O'Hanlon14-Apr-15 7:22 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Gyana_Ranjan Dash14-Apr-15 19:30
MemberGyana_Ranjan Dash14-Apr-15 19:30 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 20:03
mvaPete O'Hanlon14-Apr-15 20:03 
AnswerRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 8:05
mveBillWoodruff14-Apr-15 8: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 9:18
mveRichard Deeming14-Apr-15 9:18 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:21
mveBillWoodruff14-Apr-15 12:21 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Richard Deeming15-Apr-15 1:03
mveRichard Deeming15-Apr-15 1:03 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 2:58
mveBillWoodruff15-Apr-15 2:58 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Sascha Lefèvre15-Apr-15 3:39
professionalSascha Lefèvre15-Apr-15 3:39 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 9:32
mvaPete O'Hanlon14-Apr-15 9:32 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:35
mveBillWoodruff14-Apr-15 12:35 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 20:37
mvaPete O'Hanlon14-Apr-15 20:37 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen14-Apr-15 10:16
professionalEddy Vluggen14-Apr-15 10:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:41
mveBillWoodruff14-Apr-15 12:41 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 0:24
professionalEddy Vluggen15-Apr-15 0:24 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 3:08
mveBillWoodruff15-Apr-15 3:08 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 3:16
professionalEddy Vluggen15-Apr-15 3:16 
QuestionHow do I get images without using picturebox? Pin
Member 1138819913-Apr-15 22:29
MemberMember 1138819913-Apr-15 22:29 
AnswerRe: How do I get images without using picturebox? Pin
Richard MacCutchan13-Apr-15 23:25
mveRichard MacCutchan13-Apr-15 23:25 
GeneralRe: How do I get images without using picturebox? Pin
Member 1138819913-Apr-15 23:59
MemberMember 1138819913-Apr-15 23:59 
GeneralRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 0:02
mveRichard MacCutchan14-Apr-15 0: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.