ARTICLE

Visual Basic Generic Dal using WCF

Posted by Mahadesh Mahalingappa Articles | WCF with VB.NET September 21, 2011
In this article I would just give a tip on how to improve on the Generic DAL which we just finished in this Series .
 
Reader Level:

In this article I would just give a tip on how to improve on the Generic DAL which we just finished in this Series . I already posted the C# code of the Service Layer.

To see: http://www.c-sharpcorner.com/Authors/Mahadesh/mahadesh-mahalingappa.aspx

For Visual Basic Guys I am doing it now .

Though the Generic DAl which we had designed worked well. We still were not able to create a 100% Generic Solution in the previous article . Inorder to achieve a 100% Generic Method , We need to take the Help of Reflection . We would need to call the method CreateObjectSet using Reflection .

This is how the Service Layer needs to be modified .

Imports GenericDAL
Imports
System.Data.Objects
Imports
System.Reflection

' NOTE: You can use the "Rename" command on the context menu to change the class name "Service1" in code, svc and config file together.

Public Class Service1
    Implements
IService1
     Public Function [Get](ByVal entity As TEntity) As TEntity Implements IService1.Get
        Dim x1 As IEnumerable(Of TEntity) = GetObjectSet(entity)
        Return x1.First()
   End
Function

     Public Function GetObjectSet(ByVal entity As TEntity) As Object
        Dim context As New MCSEntities()
        Dim typeArgs As Type() = {ObjectContext.GetObjectType(entity.[GetType]())}
        Dim typObjectContext As Type = context.[GetType]()
        Dim NoParams As Type() = {}
        Dim meth As MethodInfo = typObjectContext.GetMethod("CreateObjectSet", NoParams)
        Dim methGeneric As MethodInfo = meth.MakeGenericMethod(typeArgs)

          Return methGeneric.Invoke(context, Nothing)
    End
Function

End
Class

The Service Contract Looks as below :

Imports GenericDAL
<ServiceContract()>

Public
Interface IService1 
    <OperationContract()> _
    Function [Get](ByVal entity As TEntity) As
TEntity

End
Interface 
The TEntity Class looks as below :

Imports
System.Runtime.Serialization
Imports
System.Data.Objects.DataClasses
<KnownType(GetType(MCS_CR_Company))> _
<KnownType(GetType(MCS_CR_Product))> _
<Serializable()> _
<DataContractAttribute(IsReference:=True)> _

Public
Class TEntity
    Inherits
EntityObject

End
Class

Client Code :

Imports Client.DataServiceReference
Module
Module1
      Sub Main()
        Dim client As New Service1Client()
 
        Dim entity As TEntity = New Article()
      Dim company As TEntity = client.Get(entity)

          Console.ReadKey()
      End Sub
End
Module

I will soon try to post the Entire project . Till then happy Coding .

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor