Tuesday 12 April 2022

Custome Service Class to connect wcf service

 using System;

using System.ServiceModel;

namespace Custom.Service.Client
{
    // Borrowed from: http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx
    public static class Service<TServiceClient>
    {
        private static readonly ChannelFactory<TServiceClient> ChannelFactory =
            new ChannelFactory<TServiceClient>(string.Empty);

        public static TResult Use<TResult>(Func<TServiceClient, TResult> action)
        {
            using (var channel = (IClientChannel)ChannelFactory.CreateChannel())
            {
                try
                {
                    return action((TServiceClient)channel);
                }
                finally
                {
                    try
                    {
                        channel.Close();
                    }
                    catch (Exception)
                    {
                        channel.Abort();
                    }
                }
            }
        }

        public static void Use(Action<TServiceClient> action)
        {
            using (var channel = (IClientChannel)ChannelFactory.CreateChannel())
            {
                try
                {
                    action((TServiceClient)channel);
                }
                finally
                {
                    try
                    {
                        channel.Close();
                    }
                    catch (Exception)
                    {
                        channel.Abort();
                    }
                }
            }
        }

        public static IAsyncResult Begin(Func<TServiceClient, IAsyncResult> action)
        {
            TServiceClient channel = ChannelFactory.CreateChannel();
            return action(channel);
        }

        public static TResult End<TResult>(IAsyncResult asyncResult, Func<TServiceClient, TResult> action)
        {
            var channel = (IClientChannel)asyncResult.AsyncState;

            try
            {
                return action((TServiceClient)channel);
            }
            finally
            {
                try
                {
                    channel.Close();
                }
                catch (Exception)
                {
                    channel.Abort();
                }
            }
        }
    }
}

Thursday 26 August 2021

Repository Pattern

Repository pattern is one of the Data Access Pattern, which is used to access the data from database or other data source. A repository pattern encapsulates the data access so the consumer no longer has to know about the underlying data structure.

Problem that repository pattern solves

  • The controller / UI is tightly coupled with data access layer , if we are using data access from controller / UI.
  • It is difficult to write a test for the controller / UI without side effects
  • Hard to extends entities with domain specific behaviour.

Benefits of repository pattern

  • The consumer is now separated (decoupled) from the data access
  • Easy to write a test without side-effects.
  • Modify and extend entities before they are passed on to the consumer.
  • A sharable abstraction resulting in less duplication of code.
  • Improved maintainability.

Sunday 8 August 2021

Chain of Responsibility Pattern

Chain of Responsibility pattern comes under behavioral design patterns category, this pattern lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

Iterator Pattern

Iterator pattern comes under behavioral design patterns category, this pattern let you traverse elements of collection without exposing its underlying implementation. This pattern also known as Cursor.

Command Pattern

Command pattern comes under behavioral design patterns category, this pattern turns a request into a stand-alone object that contains all information about the requestA request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command.

Mediator Pattern

Mediator pattern comes under behavioral design patterns category, this pattern lets you reduce chaotic dependencies between objects. The pattern restricts direct communication between the objects and forces them to collaborate only via mediator object. Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling. 

Memento Pattern

Memento pattern comes under behavioral design patterns category, this pattern lets you save and restore the previous state of an object without revealing the details of its implementation.

Custome Service Class to connect wcf service

  using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...