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 .