Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27409

VS 2010 Help Understanding Basic MEF development

$
0
0
Hi

i just started looking into this today and i think i understand it but the example from microsoft that i was learning from has the usual extra stuff that doesnt really need to be there. and im having a hard time getting my head around it

just for practice i want to hard code the 'part' since i know how to get the parts from files already.

this is what i have so far

1 form with 1 datagridview 1 textbox 1 button

i want to put a path to an xl file press the button and let the 'part' start an oledb connection read the xl file close connection and then send the object back
(as an object since i may use it for other types later) oledb is not a problem i can do that, its purely the MEF side of things.

so... the code haha wait for it
Code:

Public module module1
public interface IGetFileData
        Function GetFileData(fullpath as string)as object
end interface

<a program class i think needs to go here, its shown below, which will be instantiated when i press the button i think>

end module

and then i got stuck with the way microsoft showed it all on msdn
this i think i need but it has parts of the calculator example on it <below>

Code:

Public Class Program
        Dim _container As CompositionContainer

        <Import(GetType(ICalculator))>
        Public Property calculator As ICalculator


        Public Sub New()
            'An aggregate catalog that combines multiple catalogs
            Dim catalog = New AggregateCatalog()

            'Adds all the parts found in the same assembly as the Program class
            catalog.Catalogs.Add(New AssemblyCatalog(GetType(Program).Assembly))

            'IMPORTANT!
            'You will need to adjust this line to match your local path!
            catalog.Catalogs.Add(New DirectoryCatalog("C:\Users\Main Admin\Documents\Visual Studio 2010\Projects\MEF Calculator\SimpleCalculator2\Extensions"))


            'Create the CompositionContainer with the parts in the catalog
            _container = New CompositionContainer(catalog)

            'Fill the imports of this object
            Try
                _container.ComposeParts(Me)
            Catch ex As Exception

            End Try
        End Sub

    End Class

and i need to strip the code here to start the 'Part' that i actually need to use, but it looks like theres more interfaces then i actually need, im not sure the example used a console heres some more <below>

Code:

<Export(GetType(ICalculator))>
    Public Class MySimpleCalculator
        Implements ICalculator

        <ImportMany()>
        Public Property operations As IEnumerable(Of Lazy(Of IOperation, IOperationData))


        Public Function Calculate(ByVal input As String) As String Implements ICalculator.Calculate
            Dim left, right As Integer
            Dim operation As Char
            Dim fn = FindFirstNonDigit(input) 'Finds the operator
            If fn < 0 Then
                Return "Could not parse command."
            End If
            operation = input(fn)
            Try
                left = Integer.Parse(input.Substring(0, fn))
                right = Integer.Parse(input.Substring(fn + 1))
            Catch ex As Exception
                Return "Could not parse command."
            End Try
            For Each i As Lazy(Of IOperation, IOperationData) In operations
                If i.Metadata.Symbol = operation Then
                    Return i.Value.Operate(left, right).ToString()
                End If
            Next
            Return "Operation not found!"
        End Function

        Private Function FindFirstNonDigit(ByVal s As String) As Integer
            For i = 0 To s.Length
                If (Not (Char.IsDigit(s(i)))) Then Return i
            Next
            Return -1
        End Function


    End Class


heres the interfaces and 1 of the parts <below>

Code:

    Public Interface ICalculator
        Function Calculate(ByVal input As String) As String
    End Interface

    Public Interface IOperation
        Function Operate(ByVal left As Integer, ByVal right As Integer) As Integer
    End Interface

    Public Interface IOperationData
        ReadOnly Property Symbol As Char
    End Interface


    <Export(GetType(IOperation))>
    <ExportMetadata("Symbol", "+"c)>
    Public Class Add
        Implements IOperation

        Public Function Operate(ByVal left As Integer, ByVal right As Integer) As Integer Implements IOperation.Operate
            Return left + right
        End Function
    End Class

please help :), i understand what is happening a little
but i never used an interface before but i looked it up i think i get that, why are they using so many though

the import/export i understand is checking if the 'part' is allowed to be used, i dont understand the syntax though for them both,

Code:

<Export(GetType(IOperation))>
    <ExportMetadata("Symbol", "+"c)>

<ImportMany()>
        Public Property operations As IEnumerable(Of Lazy(Of IOperation, IOperationData))

i know someone who has done this should easily help me out here.

Viewing all articles
Browse latest Browse all 27409

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>