Primera Versión copiada del tfs
This commit is contained in:
72
ObjetExtensions.vb
Normal file
72
ObjetExtensions.vb
Normal file
@@ -0,0 +1,72 @@
|
||||
Imports System.Runtime.CompilerServices
|
||||
Imports System.Linq.Expressions
|
||||
Imports Microsoft.EntityFrameworkCore
|
||||
Imports System.Data.Entity.Core.Objects
|
||||
Imports System.Data.Entity.Core
|
||||
Imports System.Reflection
|
||||
Imports Microsoft.EntityFrameworkCore.Infrastructure
|
||||
Imports Newtonsoft.Json
|
||||
Imports System.Formats
|
||||
'Imports System.Data.Entity.Core.Objects
|
||||
'Imports System.Data.Entity.Core.Objects
|
||||
|
||||
Namespace Extensiones
|
||||
Public Module ObjetExtensions
|
||||
<Extension>
|
||||
Public Function ObjetoNothingAVacio(ByVal Cadena As Object) As String
|
||||
If Cadena Is Nothing Then
|
||||
Return ""
|
||||
Else
|
||||
Return Cadena.ToString
|
||||
End If
|
||||
End Function
|
||||
'<Extension>
|
||||
'Public Function GetDbContextFromEntity(ByVal entity As Object) As Data.Entity.DbContext
|
||||
' Dim object_context = GetObjectContextFromEntity(entity)
|
||||
' If object_context Is Nothing OrElse object_context.TransactionHandler Is Nothing Then Return Nothing
|
||||
' Return object_context.TransactionHandler.DbContext
|
||||
'End Function
|
||||
'Private Function GetObjectContextFromEntity(ByVal entity As Object) As ObjectContext
|
||||
' Dim field = entity.[GetType]().GetField("_entityWrapper")
|
||||
' If field Is Nothing Then Return Nothing
|
||||
' Dim wrapper = field.GetValue(entity)
|
||||
' Dim [property] = wrapper.[GetType]().GetProperty("Context")
|
||||
' Dim context = CType([property].GetValue(wrapper, Nothing), ObjectContext)
|
||||
' Return context
|
||||
'End Function
|
||||
'<Extension>
|
||||
'Public Function ObtieneContexto(entity As Objects.DataClasses.EntityObject) As Objects.ObjectContext
|
||||
' Dim relationshipManager = DirectCast(entity, Objects.DataClasses.IEntityWithRelationships).RelationshipManager
|
||||
' Dim wrappedOwnerProperty = relationshipManager.GetType.GetProperty("WrappedOwner", Reflection.BindingFlags.Instance Or BindingFlags.NonPublic)
|
||||
' Return wrappedOwnerProperty.GetValue(relationshipManager).Context
|
||||
'End Function
|
||||
|
||||
<Extension()>
|
||||
Function GetDbContext(Of T As Class)(ByVal dbSet As DbSet(Of T)) As DbContext
|
||||
Dim infrastructure = TryCast(dbSet, IInfrastructure(Of IServiceProvider))
|
||||
Dim serviceProvider = infrastructure.Instance
|
||||
Dim currentDbContext = TryCast(serviceProvider.GetService(GetType(ICurrentDbContext)), ICurrentDbContext)
|
||||
Return currentDbContext.Context
|
||||
End Function
|
||||
<Extension()>
|
||||
Function Clonar(Of T)(ByVal self As T) As T
|
||||
Dim serialized = JsonConvert.SerializeObject(self)
|
||||
Return JsonConvert.DeserializeObject(Of T)(serialized)
|
||||
End Function
|
||||
<Extension()>
|
||||
Sub CopiarPropiedadesDe(Of T)(ByVal Destino As T, ByVal Origen As T)
|
||||
Dim props = Destino.GetType.GetProperties.Where(Function(x) x.CanWrite).ToList
|
||||
For Each p In props
|
||||
p.SetValue(Destino, Origen.GetType.GetProperty(p.Name).GetValue(Origen))
|
||||
Next
|
||||
End Sub
|
||||
|
||||
'<Extension()>
|
||||
'Function ObtieneContexto(Of T As Class)(ByVal Objeto As T) As DbContext
|
||||
' Dim infrastructure = TryCast(Objeto, IInfrastructure(Of IServiceProvider))
|
||||
' Dim serviceProvider = infrastructure.Instance
|
||||
' Dim currentDbContext = TryCast(serviceProvider.GetService(GetType(ICurrentDbContext)), ICurrentDbContext)
|
||||
' Return currentDbContext.Context
|
||||
'End Function
|
||||
End Module
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user