CSLA .NET

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

CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • DataAccess, DataAccess.Mock - what am missing/am I doing wrong?

    rated by 0 users
    Answered (Verified) This post has 1 verified answer | 10 Replies | 1 Follower

    Top 50 Contributor
    168 Posts
    gajit posted on Thu, Feb 16 2012 3:17 PM

    OK, so I'm trying to re-engineer things into VB and creating my DALs from scratch using some of the examples I have seen.

    I created my first class library, DataAccess, with 3 classes;

    DalFactory, DataNotFoundException & IDalManager.

    I compiled the class library project and the DataAccess.dll exists as I would expect in Bin/Debug

    I then created a DataAccess.Mock class library and added a reference to the above DLL.

    Yet when I create the first class in the mock library;

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text

    Namespace DataAccess.Mock

        Public Class DalManager

            Implements DataAccess.IDalManager

            Private Shared _typeMask As String = GetType(DalManager).FullName.Replace("DalManager", "{0}")

            Public Function GetProvider(Of T As Class)() As T
                Dim typeName = String.Format(_typeMask, GetType(T).Name.Substring(1))
                Dim type__1 = Type.[GetType](typeName)
                If type__1 IsNot Nothing Then
                    Return TryCast(Activator.CreateInstance(type__1), T)
                Else
                    Throw New NotImplementedException(typeName)
                End If
            End Function

            Public Sub Dispose()
            End Sub

        End Class

    End Namespace

    I get an error on the Implements "DataAccess.IDalManager" - type is not defined??

    What am I missing?

    Thanks,

    Graham

    Answered (Verified) Verified Answer

    Top 50 Contributor
    168 Posts
    Answered (Verified) gajit replied on Fri, Feb 17 2012 8:49 AM
    Verified by gajit

    Thanks Scott!

    That looks almost certainly to be the issue. I have to play around now and see what the resolution is.

    I'm tormented by the fact that I may have to shift my development to C# ..( errrghh )... and become a *real* developer ;)

    I appreciate your input and will let you know how I make out.

    Thanks again,

    Graham

    All Replies

    Top 10 Contributor
    9,475 Posts

    What you describe seems correct. I usually use project references instead of file references when working within one solution, otherwise you are describing what I do as a matter of course.

    I suspect you have some basic issue with the assembly reference - that is sure what it sounds like anyway.

    Rocky

    Top 50 Contributor
    168 Posts
    gajit replied on Thu, Feb 16 2012 3:54 PM

    I tried using the project reference - no difference.

    What I did notice, and I'm not sure it's _the_  issue but when I translate the C# static class DalFactory to VB.Net, it becomes a "NonInheritable" class;

     

    Imports System.Configuration

    Namespace DataAccess

        Public NotInheritable Class DalFactory

            Private Sub New()
            End Sub

            Private Shared _dalType As Type

            Public Shared Function GetManager() As IDalManager

                If _dalType Is Nothing Then

                    Dim dalTypeName = ConfigurationManager.AppSettings("DalManagerType")

                    If Not String.IsNullOrEmpty(dalTypeName) Then
                        _dalType = Type.[GetType](dalTypeName)
                    Else
                        Throw New NullReferenceException("DalManagerType")
                    End If

                    If _dalType Is Nothing Then
                        Throw New ArgumentException(String.Format("Type {0} could not be found", dalTypeName))
                    End If

                End If
                Return DirectCast(Activator.CreateInstance(_dalType), IDalManager)

            End Function

        End Class

    End Namespace

    Could that have anything to do with it?

    This is very frustrating, it seems that no matter how I try to move down this CSLA4 path, I hit something that prevents me from moving forward....

     

     

     

     

     

     

     

    Top 25 Contributor
    422 Posts

    The "NotInheritable" shouldn't matter.

    Let's start with the basics: how is your IDalManager interface defined?  Is it Public?

    - Scott

    Top 50 Contributor
    168 Posts
    gajit replied on Thu, Feb 16 2012 4:20 PM

    Here's what I have in the DataAccess Library:

    The DalFactory.cb class:

    Imports System.Configuration

    Namespace DataAccess

        Public NotInheritable Class DalFactory

            Private Sub New()
            End Sub

            Private Shared _dalType As Type

            Public Shared Function GetManager() As IDalManager

                If _dalType Is Nothing Then

                    Dim dalTypeName = ConfigurationManager.AppSettings("DalManagerType")

                    If Not String.IsNullOrEmpty(dalTypeName) Then
                        _dalType = Type.[GetType](dalTypeName)
                    Else
                        Throw New NullReferenceException("DalManagerType")
                    End If

                    If _dalType Is Nothing Then
                        Throw New ArgumentException(String.Format("Type {0} could not be found", dalTypeName))
                    End If

                End If
                Return DirectCast(Activator.CreateInstance(_dalType), IDalManager)

            End Function

        End Class

    End Namespace

    The IDalManager.vb class:

    Namespace DataAccess

        Public Interface IDalManager

            Inherits IDisposable

            Function GetProvider(Of T As Class)() As T

        End Interface

    End Namespace

    And finally the DataNotFoundException.vb class:

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports Csla.Serialization
    Imports System.Security.Permissions

    Namespace DataAccess

        <Serializable()> _
        Public Class DataNotFoundException

            Inherits Exception

            Public Sub New(message As String)
                MyBase.New(message)
            End Sub

            Public Sub New(message As String, innerException As Exception)
                MyBase.New(message, innerException)
            End Sub

            Protected Sub New(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext)
                MyBase.New(info, context)
            End Sub

            <SecurityPermission(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.SerializationFormatter)> _
            <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.SerializationFormatter)> _
            Public Overrides Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext)
                MyBase.GetObjectData(info, context)
            End Sub

        End Class

    End Namespace

    The DataAccess project references CSLA. It's assembly name and root namespace are both set to DataAccess.

    It builds with no problems on its own and the DataAccess.dll builds to the Bin/Debug folder as I would expect.

    My DataAccess.Mock Class library project has a project dependency to DataAccess. I set a reference to the DataAccess project also ( I had previously added the .Net reference to the DataAccess.dll). The project also references CSLA.

    It's assembly name and root namespace are both set to DataAccess.Mock

    I really don't see where the problem is.

    Your help is much appreciated.

    Thanks,

    Graham

     

     

     

     

    Top 50 Contributor
    168 Posts
    gajit replied on Thu, Feb 16 2012 5:43 PM

    Hmmm, just got home and reloaded my Mock project and something else interesting happened. The reference to the DataAccess project said file not found. So I removed it, and added it back via the file.

    When I did that the IDE's error correction suggested I change "Implements DataAccess.IDalManager:" to "Implements Global.DataAccess.IDalManager:"

    What's THAT all about? When I did do that, it then further errored out by stating:

    Class 'DalManager' must implement 'Function GetProvider(Of T As Class)() As T' for interface 'DataAccess.IDalManager'

    Class 'DalManager' must implement 'Sub Dispose()' for interface 'System.IDisposable'

    So, I removed the reference AGAIN. Re-added it as a Project reference, and NOW the Intellisense wants Global.DataAccess.DataAccess.IDalManager ...

    This is unbelievable...

    I'm not THAT dense. Things haven't changed that much from VS2005 to VS2010 ... it's a simple bloody reference....

     

    Any ideas anyone?

     

    Top 50 Contributor
    168 Posts
    gajit replied on Thu, Feb 16 2012 6:58 PM

    And yest more info.

    It looks like the DataAccess Assembly is sound.

    I removed my "Mock" project from the solution, added in the DataAccess.Mock (C#) project that exists in the Encapsulated Invoke - changed the reference to my new VB.Net assembly and it's fine.

    So, it looks like the little amount of code in my VB DataAccess.Mock is somehow not working. Did it translate incorrectly? Or is there some underlying issue with the way my Mock project has been created?

    I'm losing hope here, that this is going to port to VB....

    If someone can prove me wrong I'd appreciate it....

    Thanks.

     

     

     

     

     

    Top 50 Contributor
    168 Posts
    gajit replied on Thu, Feb 16 2012 8:40 PM

    No takers eh?

    Well, it appears CSLA4 using VB.Net is a no go, unless someone can prove otherwise.

    There is either an assembly referencing issue with VB.Net projects that doesn't exist in the C# world, or the inheritance of the interface in the VB.Net DalManager is incorrect.

    To verify, I created 2 completely new C# projects for DataAccess and DataAccess.Mock, and they reference correctly and compile.

    The issue clearly lies in the VB arena.

    Off to bed, frustrated - as yet another day passes with so many questions and few answers.

    Graham

     

    Top 25 Contributor
    422 Posts

    I think this post may help:

    http://spiderinnet1.typepad.com/blog/2011/12/vbnet-root-namespace-vs-c-default-namespace.html

    VB.NET deals with "Root Namespace" differently than C# deals with "Default Namespace".  Essentially, VB.NET takes your "Root Namespace" value and prepends it to all your namespace declarations in your code files.  Given that, and how you have your namespaces defined, I'm not surprised VS is getting confused.

    It's been a REALLY long time since I did VB.NET development - I forgot all about this...

    HTH

    - Scott

    Top 50 Contributor
    168 Posts
    Answered (Verified) gajit replied on Fri, Feb 17 2012 8:49 AM
    Verified by gajit

    Thanks Scott!

    That looks almost certainly to be the issue. I have to play around now and see what the resolution is.

    I'm tormented by the fact that I may have to shift my development to C# ..( errrghh )... and become a *real* developer ;)

    I appreciate your input and will let you know how I make out.

    Thanks again,

    Graham

    Top 50 Contributor
    168 Posts
    gajit replied on Fri, Feb 17 2012 9:10 AM

    Well, it looks like we're all good.

    I can't thank you enough Scott.

    I removed the intrinsic Namespace = .... from my classes and that certainly fixed the referencing problem.

    I was left with errors that I was not implementing the GetProvider and Dispose methods of the interface. I let the IDE create a couple for me to see what they looked like, and lo and behold - it looks like the translated code doesn't define the methods correctly:

    This is what the vb.Net DalManager class SHOULD look like:

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text

    Public Class DalManager

        Implements DataAccess.IDalManager

        Private Shared _typeMask As String = GetType(DalManager).FullName.Replace("DalManager", "{0}")

        Public Function GetProvider(Of T As Class)() As T Implements IDalManager.GetProvider

            Dim typeName = String.Format(_typeMask, GetType(T).Name.Substring(1))
            Dim type__1 = Type.[GetType](typeName)
            If type__1 IsNot Nothing Then
                Return TryCast(Activator.CreateInstance(type__1), T)
            Else
                Throw New NotImplementedException(typeName)
            End If

        End Function

        Public Sub Dispose() Implements IDisposable.Dispose
        End Sub

    End Class

    So, no need to become a *real* developer just yet.... I'll now push on.

    Thanks again,

    Graham

    Page 1 of 1 (11 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