locked
Design-Best practices for Business object creation in .Net RRS feed

  • Question

  • User1238834067 posted

    We are using below class(TypeInterceptor) in one of our project to create the instance of business objects.

    To Create any business object, we are using the below code

    ++++++++++

    BLayer.TestBO objTestBO;

    this.objTestBO = TypeInterceptor.CreateObject<BLayer.TestBO>();

    +++++++++++

    I think using this kind of creational pattern makes the application not scalable as the same instance will be shared among all the concurrent users.Is it correct?

    Is there any better way to abstract the creation and maintaining instance of business objects

    Class code:

    namespace Microsoft.IT.RelationshipManagement.Internal.CISF.Infrastructure
    {
        using System;
        using System.Linq;
        using System.Text;
        using System.Reflection;
        using System.Collections;
        using System.CodeDom.Compiler;
        using System.Collections.Generic;
        using Microsoft.CSharp;
        using System.IO;

        public class TypeInterceptor
        {
            private static Hashtable _htTypes = null;
            private static Hashtable _htTypesSynchronized = null;

               static TypeInterceptor()
            {
                _htTypes = new Hashtable();
                _htTypes.Clear();
                _htTypesSynchronized = Hashtable.Synchronized(_htTypes);
            }

            public static T CreateObject<T>() where T : new()
            {
                T targetObject = default(T);
                Type typeGenerated = null;

                string strCallingAssembly = Assembly.GetAssembly(typeof(T)).CodeBase.ToString().Replace("file:///", string.Empty);

                if (!_htTypes.Contains(typeof(T).ToString()))
                {
                    string str = BuildClass<T>();
                    typeGenerated = GenerateType<T>(str, strCallingAssembly);
                    _htTypes.Add(typeof(T).ToString(), typeGenerated);
                }
                else
                {
                    typeGenerated = (Type)_htTypes[typeof(T).ToString()];
                }

                targetObject = (T)Activator.CreateInstance(typeGenerated);
                return targetObject;
            }

    Thursday, February 11, 2010 4:06 AM

All replies

  • User681263371 posted

    In your code you have used static constructors and the purpose for using static constructors is that you don't want to instantiate the object often. The static constructors will be called only once. To know more about static constructors you can refer one of my blogs.

    http://sandblogaspnet.blogspot.com/2008/02/static-constructors-in-net.html

    If you want to avoid concurrency problem then you can make use of "lock" to enclose user request and avoid multiple access of the static object at the same time. Sample code with lock keyword is pasted below.

    public static T CreateObject<T>() where T : new()
    {
         Object obj = new Object();
         lock(obj)
         {
             T targetObject = default(T);
             Type typeGenerated = null;
             string strCallingAssembly = Assembly.GetAssembly(typeof(T)).CodeBase.ToString().Replace("file:///", string.Empty);
             if (!_htTypes.Contains(typeof(T).ToString()))
             {
                 string str = BuildClass<T>();
                 typeGenerated = GenerateType<T>(str, strCallingAssembly);
                 _htTypes.Add(typeof(T).ToString(), typeGenerated);
              }
             else
             {
                 typeGenerated = (Type)_htTypes[typeof(T).ToString()];
              }
              targetObject = (T)Activator.CreateInstance(typeGenerated);
              return targetObject;
         }
     }


    Also the use of static doesn't put restriction on scalability aspect of the class. To design scalable classes you take a look at some of the design patterns. 

    Thursday, February 11, 2010 8:41 PM
  • User-952121411 posted

    We went throught your exact same question and post from last week here:

    Maintaining Business Objects -Thoughts: 

    http://forums.asp.net/p/1520903/3666116.aspx#3666116

    Were you looking for something different that was not added in that post?

    Friday, February 12, 2010 11:48 AM
  • User1238834067 posted

    Yes.Al though issue is same I would like to know more about  best practices in this question rather than to be speciifc about the issue,so that I can propose.

    Monday, February 15, 2010 6:52 AM