Click here to Skip to main content
15,897,315 members
Articles / Web Development / ASP.NET

A Simple Object Collaboration Framework

Rate me:
Please Sign up or sign in to vote.
4.71/5 (7 votes)
17 Feb 2009CPOL22 min read 46.4K   189   39  
A library that simplifies complex interactions between objects by providing a new mechanism of instance discovery and lifetime management. It is an extension of the .NET CallContext or HTTPContext mechanism that provides a way of sharing objects within an execution code path.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOCF
{
    /// <summary>
    /// Base class for a custom collaboration entity.
    /// A custom collaboration entity provides instances of objects
    /// that can be used in a collaboration.
    /// You can derive from this class, implement properties that
    /// return object instances.
    /// 
    /// The collaboration context will just be accessible during the
    /// along the code execution path until using() block is exited.
    /// All objects that are in the calling method as well as all nested methods
    /// will be able to access this shared collaboration context.
    /// Objects can thus interact as if they were in the same method scope.
    /// This allows organized code to always remain organized independently of the
    /// current task that they are performing.
    /// A better approach is to create a separate custom collaboration entity
    /// that derives from CustomCollaboratoin.
    /// 
    /// </summary>
    /// <example>
    /// <![CDATA[
    /// 
    ///     // To start a new collaboration use:
    ///     using (new OrderConfirmation())
    ///     {
    ///         ..
    ///     }
    ///     
    ///     // Here's an example code snippet to access this collaboration entity and the provided object instances in a nested method call:
    ///     OrderConfirmation confirmation = OrderConfirmation.Current;
    ///     if (confirmation != null)
    ///     {
    ///         confirmation.Subject = string.Format("Order {0} has been received.", confirmation.Order.ID);
    ///         confirmation.Email.Body = ...;
    ///         confirmation.EmailService.Send();
    ///     }
    /// ]]>
    /// </example>
    public class CustomCollaboration : NamedCollaborationContext
    {
        public CustomCollaboration()
            : base(null)    // Uses full name of this class.
        {
               
        }

        public static T Get<T>() where T : CustomCollaboration
        {
            return (T)NamedCollaborationContext.Get(typeof(T).FullName);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Netsoft-USA
United States United States
Technical Architect at Netsoft-USA. Developed Netsoft USA Framework (A software factory approach for developing object oriented + service based, data driven enterprise scale applications). Check out my new app VocaTalk Personal Podcast (A cool app that generates podcasts using Text-to-speech, background music and effects)

Comments and Discussions