I’m building a DLL with several ‘master’ objects that need access to the app LINQ DataContext. The DLL will serve several projects with different dataContexts, so I need the DLL can call the object that it’s inside the EXE.
What is the best or elegant way to do it?
Edit: Clarification
Code example of what I’m trying to do:
'DLL '--- Public MustInherit Class MasterObject(Of T As Class) Friend db As DataContext Public Sub New() 'How do I do something like this? db = New DataContextInTheExe() End Sub ... Public MustOverride Sub Save() end class 'In the Exe '--- Public Class Order Inherits MasterObject(Of Order) Public Overrides Sub Save() ... Me.db.SubmitChanges() End Sub end class
The most appropriate way to do this is for the exe to pass the data-context to the dll, for example as a method argument to whatever the dll is doing, or as a constructor argument to whatever class (in the dll) needs it. If you have different types of data-contexts, then the type would have to be
DataContextor a subclass. Extension methods (on DataContext) are one way of doing this (but not the only way).Alternatively – in a standard ‘repository’ implementation (where the dll is the repository), it is entirely possible that the caller (the exe) doesn’t even know about the data-context – just the
ICustomerRepositoryinterface (or whatever) that the dll exposes.Re the edit; by this alone, it would have no way of knowing the type of data-context to create. You possibly need to either pass a data-context in, or tell it (via generics) the type of data-context; excuse my C#, but:
However, I strongly suspect that the dependency-injection route is simpler – i.e. let the caller tell us: