locked
Data Tier / Business Tier RRS feed

  • Question

  • User-2071863390 posted

    I have a business tier that calls a method on a data tier. My application can be configured to use one of two types of data source: a database or xml file.

    I want my top level presentation tier to call a function (DoSomthing()) within my business tier regardless of what the selected data source is.  This is what I've come up with so far. I'm not sure how to make this work in practice. Any ideas/pointers

    public static void DoSomething()
    {
    	var processor = GetDataObj();
    	p.DoMyThing();
    }
    
    
    static DataProcessor GetDataObj()
    {
    	bool  isDbsSource = true;
    	if(isDbsSource)
    	{
    		Data.Dbs.Processor p = new Data.Dbs.Processor();
    		return p;
    	}
    	else	
    	{
    		Data.XML.Processor p = new Data.XML.Processor();
    		return p;
    	}	
    }
    


     

    Friday, December 17, 2010 11:35 AM

Answers

  • User-952121411 posted

    I want my top level presentation tier to call a function (DoSomthing()) within my business tier regardless of what the selected data source is.
     

    Ahh... welcome to the world of design patterns! This is where a Factory method can allow you to defer instantiation of the proper Data class you define to subclasses. Lets look at a rough sample using (3) providers: SQL, Oracle, and XML. With this setup you can use configuration to switch out the data provider without changing any compiled code:

    public class MyData
    {
    
      public enum DataProviders
      {
         SQLServer = 1,
         Oracle = 2,
         XML = 3
       }
    
      public static ProviderConnection CreateConnectionFactory(DataProviders DataProvider)
      {
    
         switch (DataProvider) {
    
    	case DataProviders.SQLServer:
    	   return new SQLServerProvider();
    	case DataProviders.Oracle:
    	   return new OracleProvider();
    	case DataProviders.XML:
    	   return new XMLProvider();
    		}
    
        }
    
    }

    Note: an improvement on the code above is to have all class tpyes returned to Implement the same Interface that is actually what is returned from the Factory. And some sample calling code for the above example:

    public void Test()
    {
    	//Get an instance of the Provider based on the type dictated by configuration. Type can be switched without ever changing any compiled code.
    	ProviderConnection myConn = MyData.CreateConnectionFactory(ConfigurationManager.AppSettings("ConnectionProvider"));
    
    }


    For some more information I highly reccomend the following book Professional ASP.NET Design Patterns By Scott Millet: Review

    ...or the following links:

    http://blogs.vbcity.com/jspano/articles/197.aspx

    http://www.dofactory.com/Patterns/PatternFactory.aspx

    http://msdn.microsoft.com/en-us/library/Ee817667(pandp.10).aspx


    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 3:32 PM

All replies

  • User-810586307 posted

    Hope this will help you..

            //Declare datasource type
            public enum DataSourceType
            { 
                XMLDataSource,
                SQLDatabase
            }
    
    
    
    
            private static DataSourceType GetDataSource(string passyourvalidationstring)
            {
                //do your validation with parameter 'passyourvalidationstring'
                //finally you will get one datasourcetype
                return DataSourceType.SQLDatabase;
            }
    
    
    
    
            private static string GetValueFromDatasource(string datasourcevalidationstring )
            {
                string rt = string.Empty;
                if (GetDataSource(datasourcevalidationstring) == DataSourceType.SQLDatabase)
                {
                    //DO SQL related work
                    rt = "Some Value from SQLDatabase datasource";
                }
                else if (GetDataSource(datasourcevalidationstring) == DataSourceType.XMLDataSource)
                {
                    //DO xml related work
                    rt = "Some Value from XMLDataSource datasource";
                }
                return rt;
            }


     

    Friday, December 17, 2010 1:22 PM
  • User-952121411 posted

    I want my top level presentation tier to call a function (DoSomthing()) within my business tier regardless of what the selected data source is.
     

    Ahh... welcome to the world of design patterns! This is where a Factory method can allow you to defer instantiation of the proper Data class you define to subclasses. Lets look at a rough sample using (3) providers: SQL, Oracle, and XML. With this setup you can use configuration to switch out the data provider without changing any compiled code:

    public class MyData
    {
    
      public enum DataProviders
      {
         SQLServer = 1,
         Oracle = 2,
         XML = 3
       }
    
      public static ProviderConnection CreateConnectionFactory(DataProviders DataProvider)
      {
    
         switch (DataProvider) {
    
    	case DataProviders.SQLServer:
    	   return new SQLServerProvider();
    	case DataProviders.Oracle:
    	   return new OracleProvider();
    	case DataProviders.XML:
    	   return new XMLProvider();
    		}
    
        }
    
    }

    Note: an improvement on the code above is to have all class tpyes returned to Implement the same Interface that is actually what is returned from the Factory. And some sample calling code for the above example:

    public void Test()
    {
    	//Get an instance of the Provider based on the type dictated by configuration. Type can be switched without ever changing any compiled code.
    	ProviderConnection myConn = MyData.CreateConnectionFactory(ConfigurationManager.AppSettings("ConnectionProvider"));
    
    }


    For some more information I highly reccomend the following book Professional ASP.NET Design Patterns By Scott Millet: Review

    ...or the following links:

    http://blogs.vbcity.com/jspano/articles/197.aspx

    http://www.dofactory.com/Patterns/PatternFactory.aspx

    http://msdn.microsoft.com/en-us/library/Ee817667(pandp.10).aspx


    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 3:32 PM
  • User-2071863390 posted

    Perfect thanks!... this article was really useful http://blogs.vbcity.com/jspano/articles/197.aspx

     

    Wednesday, December 22, 2010 9:32 AM