Click here to Skip to main content
15,909,591 members
Home / Discussions / C#
   

C#

 
GeneralRe: What is the difference between Object Oriented,Object based and purely Object Oriented in terms of any language ? Pin
OriginalGriff14-Apr-15 22:45
mveOriginalGriff14-Apr-15 22:45 
AnswerRe: What is the difference between Object Oriented,Object based and purely Object Oriented in terms of any language ? Pin
Florian Braun14-Apr-15 21:40
professionalFlorian Braun14-Apr-15 21:40 
Questionbest coding practises in c# Pin
dhivya.sakthi14-Apr-15 19:14
dhivya.sakthi14-Apr-15 19:14 
AnswerRe: best coding practises in c# Pin
Gyana_Ranjan Dash14-Apr-15 19:38
Gyana_Ranjan Dash14-Apr-15 19:38 
AnswerRe: best coding practises in c# Pin
V.14-Apr-15 20:15
professionalV.14-Apr-15 20:15 
AnswerRe: best coding practises in c# Pin
aadhira14-Apr-15 20:22
aadhira14-Apr-15 20:22 
GeneralRe: best coding practises in c### Pin
dhivya.sakthi14-Apr-15 20:40
dhivya.sakthi14-Apr-15 20:40 
SuggestionRe: best coding practises in c# Pin
aadhira14-Apr-15 21:09
aadhira14-Apr-15 21:09 
GeneralMessage Removed Pin
14-Apr-15 19:11
dhivya.sakthi14-Apr-15 19:11 
QuestionGeneric interface and generic delegate in simple terms. Pin
Gyana_Ranjan Dash14-Apr-15 2:14
Gyana_Ranjan Dash14-Apr-15 2:14 
AnswerRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 2:44
mvePete O'Hanlon14-Apr-15 2:44 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Gyana_Ranjan Dash14-Apr-15 2:49
Gyana_Ranjan Dash14-Apr-15 2:49 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 4:53
mvePete O'Hanlon14-Apr-15 4:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Simon_Whale14-Apr-15 5:16
Simon_Whale14-Apr-15 5:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 5:53
mvePete O'Hanlon14-Apr-15 5:53 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 7:13
professionalBillWoodruff14-Apr-15 7:13 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 7:22
mvePete O'Hanlon14-Apr-15 7:22 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Gyana_Ranjan Dash14-Apr-15 19:30
Gyana_Ranjan Dash14-Apr-15 19:30 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 20:03
mvePete O'Hanlon14-Apr-15 20:03 
AnswerRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 8:05
professionalBillWoodruff14-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
professionalBillWoodruff14-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
professionalBillWoodruff15-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 

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.