Proper Seperation of Concerns?

2 posts, 0 answers
  1. Cole
    Cole avatar
    20 posts
    Member since:
    Jun 2008

    Posted 13 Jan 2009 Link to this post

    Hey there,

    I am wondering how you recommend using the tool concerning structuring.  I have 4 facets to my project...

    1. Web - the website (ASP.NET MVC)
    2. Test - test harness
    3. Data - reverse-mapped Telerik ORM classes.
    4. Services - interface between the Data and Web.

    In theory I generate my Telerik classes in Data, then create access methods in my Service project that utilizes the Data classes, so the Web does not know how the objects are specifically persisted.   I am finding that the service layer needs a level of config that is present in the Data project.  For example, consider the method "AddCustomer(Customer c)"

    public void AddCustomer(Customer c)  {//pseudo code... do NOT assume to be 100% valid.
     IObjectScope scope = Database.Get("DatabaseConnection1").GetNewObjectScope(); 
                //you could see in some expamples that this can also be done with    
                //Database.Get("DatabaseConnection1").GetObjectScope();   
                //there is absolutely no diference   
                using (scope) 
                { 
                    scope.Transaction.Begin(); 
                    Customer c = new SharedEvent.Data.Model.Customer(); 
                    c.FName = "Joe"
                    c.LName = "Test"
                    c.PassEncrypted = "testpass"
                     
                    scope.Add(c); 
                    scope.Transaction.Commit();   
                }  

    The database name "DatabaseConnection1" is specified in a config file in the Data project.  Is it fine to reference the object and fall-back to the Data project?  Or should I keep the config in Services and inject it into the model objects in Data?  The tool seems to want to keep both together, but I'd really like to seperate Data from Services if at all possible.

    Thx,
    Rob
  2. Dimitar Kapitanov
    Admin
    Dimitar Kapitanov avatar
    632 posts

    Posted 14 Jan 2009 Link to this post

    Hi Robin,
    There are different approaches to that, all of them have different level of separation, and you should look into it which one is best for you. Let me put few options here what comes to my mind:

    1. Develop a helper class inside the Data project that acts like a proxy to the data model. Then use the decorator pattern (or something similar) in your service like:
    class MyService 
     
       MyModel model; 
       public MyService(MyModel model) 
       { 
            this.model = model; 
       } 
     
       public void AddCustomer(Customer c) 
       { 
          this.modelAddCustomer(c) 
       } 

    as additional thing you can load dynamically the connection information inside the Get method like:
            static public void AdjustForDynamicLoad(string myPath) 
            { 
                if (theObjectScopeProvider1 == null
                    theObjectScopeProvider1 = new ObjectScopeProvider1(); 
     
                if (theObjectScopeProvider1.myDatabase == null
                { 
                    string assumedInitialConfiguration = 
                               @"<openaccess>" + 
                                   "<references>" + 
                                       "<reference assemblyname='PLACEHOLDER' configrequired='True'/>" + 
                                   "</references>" + 
                                   "<connections>" + 
                                        @"<connection id=""DatabaseConnection1"">" + 
                                            "<databasename>-</databasename>" + 
                                           @"<servername>.\SQLEXPRESS</servername>" + 
                                            "<integratedSecurity>True</integratedSecurity>" + 
                                           @"<connectionParams>AttachDbFilename=" + myPath + "</connectionParams>" + 
                                            "<backendconfigurationname>mssqlConfiguration</backendconfigurationname>" + 
                                        "</connection>" + 
                                    "</connections>" + 
                               "</openaccess>"
                    System.Reflection.Assembly dll = theObjectScopeProvider1.GetType().Assembly; 
                    assumedInitialConfiguration = assumedInitialConfiguration.Replace( 
                                                        "PLACEHOLDER", dll.GetName().Name); 
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
                    xmlDoc.LoadXml(assumedInitialConfiguration); 
     
                    Database db = Telerik.OpenAccess.Database.Get("DatabaseConnection1"
                                                xmlDoc.DocumentElement, new System.Reflection.Assembly[] { dll }); 
     
                    theObjectScopeProvider1.myDatabase = db; 
                } 
            } 

    This gives you the ability to prepare the connection string in your DAL layer, and then through the model helper initialize the model.

    Do not hesitate to ask additional questions you may have regarding this functionality.
    Kind regards,
    Dimitar Kapitanov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
Back to Top