0

I'm looking for a way to do one thing I have in mind and I hope anybody here can help me. This is my scenario: I have two libraries, the first performs some actions, the second is a simple logger. In the first library I have a winform which makes some operations to a database, and the user can add or delete data using this form; I would like to use the second library to log all the operations the user makes but without referencing the log library from the first one. What I am looking for is a way to let the main application know what the user makes in the first library and then call the second one to write some data in the log. Another thing to keep in mind is that the form in the first library is opened as a dialog form. Is it possible to do? And if yes, how can I do this? Thanks in advance.

[EDIT]

I realize that maybe my question was not so clear as it was in my mind. In order to keep the two libraries separated (I don't want to call the logging library from the first library in any way), I was thinking about something in the first library that raises an event or something similar or something else in the main application, so if I want i can call the logging methods in the second library from the main application when needed. I hope this is more clear.

Hydra
  • 3
  • 5
  • Just add the second project as a reference to first project.. – null1941 Nov 23 '15 at 10:22
  • Take a look at the sample I added to my answer. – Access Denied Nov 23 '15 at 14:24
  • I got the solution that fits for my case: since I don't want to add any logging method to the database library, I created some custom events in the winform, so when I press a button in that form an event is rised in the main application and in that event I call the logging methods. I also pass all the parameters I need in the event, so I have all the data I need to fit the log. – Hydra Nov 26 '15 at 13:13

2 Answers2

1

I'm only wondering why you want this slower, 'hackier' way of doing this if you can just add a reference to that library. Why not use the logical approach of referencing the needed library? Nonetheless, it's possible to use classes and methods in a library without having to reference that library, using reflection.

You can use something like this

Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");

Source: https://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx

Do understand that using reflection is considered slow, so I'd advice not to use this in loops or recursive pieces of code.

Allmighty
  • 1,499
  • 12
  • 19
  • Let's assume I'm using PCL library, which doesn't know about android and LogCat. My main app is android and it can implement logger: Android.log(). Another app is Windows store app which has different implementation of logger and so on. How would you refer to Mono.Android.dll in PCL project? The answer is simple: just use dependency injection. – Access Denied Nov 23 '15 at 11:08
0

Dependency injection is the best candidate for your problem: http://www.martinfowler.com/articles/injection.html

Here you can find how to implement it for .net

https://visualstudiomagazine.com/articles/2010/08/01/inversion-of-control-patterns-for-the-microsoft-net-framework.aspx

In two words: you need to define interface in core library (Core.dll):

interface ILogger
{
   void log(string message);
}

You libraryA refers to Core.dll, but not to libraryB. and resolve ILogger interface in your library

var logger = ServiceContainer.Resolve<ILogger>()

In your app (refers to Core,libraryA, libraryB) you should register your logger:

ServiceContainer.Register(typeof(ILogger),<MyLogger is implemented in AssemblyB or in the main app, implements ILogger interface>);

I've made sample, which demonstrates how to call Logger from PCL library. Logger is implemented in console application:

https://github.com/Mogikan/Stackoverflow-samples/tree/master/Logger

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • And how exactly am I going to be able to reference the `MyLogger` class in library B that is **not** referenced? – Allmighty Nov 23 '15 at 10:30
  • This is called dependency injection, it allows you to get implementation from service container in your library which doesn't know about MyLogger and how it's implemented. Injection happens in your app which knows how implement it. – Access Denied Nov 23 '15 at 10:32
  • One more link which you can find helpful http://stackoverflow.com/questions/743951/dependency-injection-in-net-with-examples – Access Denied Nov 23 '15 at 10:37
  • This will not compile as class `MyLogger` is not included in the solution. The question is not about having some inversion of control, it's about using stuff in libraries that is not included in the project. – Allmighty Nov 23 '15 at 10:41
  • As far as I get application have enough information how to log, while libraries don't. So it could log if logger is implemented or it may not logging if it's not implemented in the main application. So, MyLogger can be referenced in Application, and it shouldn't be referenced by library as it's required in the question. – Access Denied Nov 23 '15 at 10:47