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
Join the conversation! Your thoughts help the community grow.