Home > Development > Template Pattern in C#

Template Pattern in C#


Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

public interface IPersistable
    {
        void Save();
        void Update();
        void Delete();
    }

    public abstract class PersistableBase<T> : IPersistable
        where T : class
    {

        protected abstract void PreSave(T data);
        protected abstract void PostSave(T data);

        public void Save()
        {
            PreSave(this as T);
            SaveData(this as T);
            PostSave(this as T);
        }

        protected void SaveData(T data)
        {
            
        }

        protected abstract void PreUpdate(T data);
        protected abstract void PostUpdate(T data);

        public void Update()
        {
            PreUpdate(this as T);
            UpdateData(this as T);
            PostUpdate(this as T);
        }

        protected void UpdateData(T data)
        {
            
        }

        protected void DeleteData(T data)
        {
            
        }

        protected abstract void PreDelete(T data);
        protected abstract void PostDelete(T data);

        public void Delete()
        {
            PreDelete(this as T);
            DeleteData(this as T);
            PostDelete(this as T);
        }
    }

    public class Customer : PersistableBase<Customer>
    {
        private readonly int _id;
        private readonly string _name;

        public Customer(int id, string name)
        {
            _id = id;
            _name = name;
        }

        protected override void PreSave(Customer data)
        {
            string methodname = "PreSave";
            Console.WriteLine("Logging: {0} - {1}",methodname,data);
        }

        protected override void PostSave(Customer data)
        {
            string methodname = "PostSave";
            Console.WriteLine("Logging: {0} - {1}", methodname, data);
        }

        protected override void PreUpdate(Customer data)
        {
            Type t = GetType();
            string methodname = t.DeclaringMethod.Name;
            Console.WriteLine("Logging: {0} - {1}", methodname, data);
        }

        protected override void PostUpdate(Customer data)
        {
            Type t = GetType();
            string methodname = t.DeclaringMethod.Name;
            Console.WriteLine("Logging: {0} - {1}", methodname, data);
        }

        protected override void PreDelete(Customer data)
        {
            Type t = GetType();
            string methodname = t.DeclaringMethod.Name;
            Console.WriteLine("Logging: {0} - {1}", methodname, data);
        }

        protected override void PostDelete(Customer data)
        {
            Type t = GetType();
            string methodname = t.DeclaringMethod.Name;
            Console.WriteLine("Logging: {0} - {1}", methodname, data);
        }

        public override string ToString()
        {
            return String.Format("[{0}: {1}]", _id, _name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer(1,"Blair");
            c.Save();
            Console.ReadKey();
        }
    }
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: