2

I'm new around here (I know this site for long but it's my first time actually asking somehting).

Components that I'm using: - EF6, Devexpress XtraGrid

Ok... so, what I want is to kind of do this, I have 1 form with multiple tables, and which I will have to be able to add and delete from each's NavigationBar.

I know how to do it, I just need a way to skip the select case.

Here's some code,

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        If m IsNot Nothing Then
            Select Case _curentPageIndex
                Case 0 : db.GESTARM.Add(m)
                Case 1 : 'Other table add
                Case 2 : 'Other table add
            End Select
        End If
    End If
End Sub

What I want to do with that is kind of this:

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        'somehow get the table (type) of the entry through the m object
        If m IsNot Nothing Then
            db.<Table>.Add(m)
        End If
    End If
End Sub

So instead of writing every add for each case, I just had to do something like that. Is it possible or am I going to stick with the select case?

Thanks in advance, and sorry if my english is bad (I'm not native).

EDIT 1: as Mark mentioned in a comment we could use this in C# but in VB it doesn't work...

Public Class GenericRepository(Of T)
Implements IDisposable
Friend context As GestProGest1Entities
Friend dbSet As Entity.DbSet(Of T) ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"

Public Sub Dispose() Implements IDisposable.Dispose
    If context IsNot Nothing Then
        context.Dispose()
        context = Nothing
    End If
End Sub

Public Sub New(context As GestProGest1Entities)
    Me.context = context
    Me.dbSet = context.Set(Of T)() ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"
End Sub

Public Overridable Sub Insert(entity As T)
    dbSet.Add(entity)
    context.SaveChanges()
End Sub
End Class

Any ideas how to do this in VB?

EDIT 2: Ok, so I got it working like this

Public Class GenericRepository(Of T As Class)

now my problem is how to get the type from the object

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        Dim myType As Type = m.GetType()
        Dim table As New GenericRepository(Of myType)(db) 'Doesn't accept myType here...
        table.Insert(m)
    End If
End Sub
Community
  • 1
  • 1
Paulo Lima
  • 142
  • 10
  • Welcome to SO! You english is good and your question is well explained, so job well done. This question has come up a couple times before,http://stackoverflow.com/q/28364376/1158842, http://stackoverflow.com/q/5295554/1158842, http://stackoverflow.com/q/8138070/1158842, http://stackoverflow.com/q/17879206/1158842. Do these address your question? – hubson bropa Nov 02 '15 at 16:49
  • Well, I guess I have to stick with select case from what I read from those posts, EF doesn't like dyncamic stuff... Thanks for the help anyway, and if someone has any idea how to make this in a better way, I'm all ears. – Paulo Lima Nov 02 '15 at 16:53
  • you could add the entities on DataSet level, however you'll have to do a lot of handling yourself then. – DevilSuichiro Nov 02 '15 at 16:56
  • @DevilSuichiro I can't use DataSet on this project, it would be easy to just bind the grid to that (DevExpress makes wonders with DataSets) but I just can't use it here sorry, thanks for the sugestion tho. – Paulo Lima Nov 02 '15 at 17:04
  • What is the type of the variable `db`? – Yacoub Massad Nov 02 '15 at 17:54
  • Is something like [this](http://stackoverflow.com/questions/16944527/is-it-possible-to-create-a-generic-method-for-adding-items-to-a-entity-framework) or [this](http://stackoverflow.com/questions/30675564/in-entity-framework-how-do-i-add-a-generic-entity-to-its-corresponding-dbset-wi) what you are looking for? – Mark Nov 02 '15 at 17:55
  • @Mark yes, it should be exactly that, except VB doesn't like that `context.Set` – Paulo Lima Nov 03 '15 at 15:17
  • Edited the post to give some more info with @Mark 's help – Paulo Lima Nov 03 '15 at 15:24
  • I think perhaps I sent you down the wrong path because I didn't fully understand your question. Since you don't know `T`, I think you would have to use reflection to use the generic repository from that example. However, it seems that `DbContext` has a [`Set`](https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.set%28v=vs.113%29.aspx#M:System.Data.Entity.DbContext.Set%28System.Type%29) method that takes the type as a parameter, so perhaps all you need in your initial attempt (2nd code block) is something like `db.Set(m.GetType()).Add(m)`? – Mark Nov 03 '15 at 16:52
  • @Mark, waw, didn't think it was really that simple, thank you. – Paulo Lima Nov 04 '15 at 09:32

1 Answers1

1

With Mark's help I finally got this working.

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
  If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
      Dim m As Object = sender(sender.count - 1)
      db.Set(m.GetType()).Add(m)
  End If 
End Sub

Thanks for everyone's help!

Paulo Lima
  • 142
  • 10