CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • CSLA unit testing MVC no DAL

    rated by 0 users
    Answered (Verified) This post has 2 verified answers | 4 Replies | 2 Followers

    Not Ranked
    8 Posts
    zbend posted on Tue, Aug 28 2012 6:59 PM

    I'm new to CSLA, and also unit testing in general, yet I'm newly assigned to a project that uses CSLA and they want to now write unit tests for it, and I'm lost.

    The project is a ASP.NET MVC 3 (c#) application, with a csla library that has umm inline? dataportal methods. The actual data fetching is done in the csla business object itself, there is no DAL, just calls to SQL stored procedures right in the object that then call LoadProperty. It seems like this is not possible to unit test I can't mock the csla objects and I have no DAL, is this true does it need to be totally refactored\rewritten? Or am I missing some approach? To get a bit more specific, I have an HTML helper method that takes one of my csla objects and returns and MVCHtmlString, I want to test what the helpers is doing how can I accomplish this? Any help, or samples you can post me to is appreciated.

     

    ------

    Not to get off topic but I'm also failing to see the point of using CSLA in a scenario like this where it will always be an asp.net web app connecting to a MSSQL database, its not using any of the power of the dataportal and its ntier-ed abbstraction-ness right? It could use some of the authorization stuff or validation rules of CSLA (but it doesn't appear to be) I feel like if I'm allowed to refactor\rewrite it I would be better off not using CSLA at least for this app and given my unfamiliarity with CSLA, thoughts? Thanks.

     

     

    Answered (Verified) Verified Answer

    Top 10 Contributor
    9,475 Posts
    Verified by zbend

    You are correct that you'll need to refactor the DataPortal_XYZ methods to invoke a DAL so you can create a mock DAL for testing. That is usually not terribly difficult, but it does require repetative effort.

    In terms of the value of CSLA, there are a few primary benefits. CSLA

    • helps shield you from changes to data access technologies over time
    • helps shield you from UI technology changes over time
    • imposes a consistent architecture and coding approach that improves maintainabilty through consistency
    • allows switching from your current 2-tier to a 3-tier deployment without code changes (sometimes important if you get audited for SOX or HIPPA or similar regulatory requirements)
    • defines a formal "home for business logic" including validation, business rules, calculations, authorization rules, etc.
    • helps you create a reusable business layer that is often useful when your future requirements end up including a service interface or mobile interface in addition to your existing web interface

    I recognize that many of those benefits are long-term. Personally I'm OK with that, because the real cost of software isn't in its original creation. It is in the cost of maintaining and updating the software over the typical 7-15 year timeframe during which that software is in use.

    Rocky

    Top 10 Contributor
    2,279 Posts
    Answered (Verified) JonnyBee replied on Wed, Aug 29 2012 12:43 AM
    Verified by zbend

    Hi,

    You may want to look at the MEF Sample projects in CslaContrib that show sample on how to plug in DataAccess in either DataPortal_XYZ methods or use the ObjectFactory pattern. This sample shows usage of Repository pattern and ObjectFactory pattern. Goto source code and download the latest source code.

    In order to achieve this - be prepared to refactor the SQL code to another layer and just return a IDataReader - that can be mock'ed to return fake data.You can create a mockable datalayer by using IDataReader as a contract to the database. (This is not shown in the MEF sample tho' - but search the forum as there has been a few threads on this.)

    You can get a child object also by either using DataPorta.CreateChild with parameters or add public static factory methods or public constructor. CSLA has typically had private constructor to force the use of factory methods but with the limits imposed from SL we now tend to make all the constructors public.

     

     

    Jonny Bekkum, Norway CslaContrib Coordinator

    All Replies

    Top 10 Contributor
    9,475 Posts
    Verified by zbend

    You are correct that you'll need to refactor the DataPortal_XYZ methods to invoke a DAL so you can create a mock DAL for testing. That is usually not terribly difficult, but it does require repetative effort.

    In terms of the value of CSLA, there are a few primary benefits. CSLA

    • helps shield you from changes to data access technologies over time
    • helps shield you from UI technology changes over time
    • imposes a consistent architecture and coding approach that improves maintainabilty through consistency
    • allows switching from your current 2-tier to a 3-tier deployment without code changes (sometimes important if you get audited for SOX or HIPPA or similar regulatory requirements)
    • defines a formal "home for business logic" including validation, business rules, calculations, authorization rules, etc.
    • helps you create a reusable business layer that is often useful when your future requirements end up including a service interface or mobile interface in addition to your existing web interface

    I recognize that many of those benefits are long-term. Personally I'm OK with that, because the real cost of software isn't in its original creation. It is in the cost of maintaining and updating the software over the typical 7-15 year timeframe during which that software is in use.

    Rocky

    Not Ranked
    8 Posts
    zbend replied on Tue, Aug 28 2012 7:45 PM

    Thanks very much for the reply!,

    Still a little unclear how I would test my HTML Helper example thouhg. My CSLA obejct would be using the mock DAL thats cool, but how would I get an instance of my CSLA object for the helper or should I rework the helper to take a DTO? I could make an instance with the 'top' object, but it has children objects that don't really  have a creation method, if that makes sense. So my top object has a topObject.NewTop(); but its children I just call topObject.Childrens.AddNew(), so I'd have to create a top object then call its .Childrens.AddNew() to get my child csla instance. Is that the right approach or should I use the DTO?

    Thanks.

     

    Top 10 Contributor
    2,279 Posts
    Answered (Verified) JonnyBee replied on Wed, Aug 29 2012 12:43 AM
    Verified by zbend

    Hi,

    You may want to look at the MEF Sample projects in CslaContrib that show sample on how to plug in DataAccess in either DataPortal_XYZ methods or use the ObjectFactory pattern. This sample shows usage of Repository pattern and ObjectFactory pattern. Goto source code and download the latest source code.

    In order to achieve this - be prepared to refactor the SQL code to another layer and just return a IDataReader - that can be mock'ed to return fake data.You can create a mockable datalayer by using IDataReader as a contract to the database. (This is not shown in the MEF sample tho' - but search the forum as there has been a few threads on this.)

    You can get a child object also by either using DataPorta.CreateChild with parameters or add public static factory methods or public constructor. CSLA has typically had private constructor to force the use of factory methods but with the limits imposed from SL we now tend to make all the constructors public.

     

     

    Jonny Bekkum, Norway CslaContrib Coordinator

    Not Ranked
    8 Posts
    zbend replied on Wed, Aug 29 2012 8:39 AM

    Great, thanks for all the help,  it sounds like I have a fair amount of work to do, simple and repetative as it may be, but it makes sense to me now, Thanks!

     

    Page 1 of 1 (5 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems