I am working on a DI problem, which I think I understand the reason, but I need suggestions for work.
I built a separate assembly that speaks to Sql (call this assembly a) and another assembly that contains business logic (call this assembly b). I created an interface for the db class in assembly b. Since the interface is not part of the db assembly, I do not need references to the db project, and I can load the link to the db assembly or the stub if I want to run unit tests at runtime and I don't need to know anything else about the assembly.
I can write code in a business logic library that compiles that looks like this: (pretend a and b are namespaces in their respective assemblies)
a.IDatabaseClass db_class = (a.IDatabase)new b.Database();
When I try to run this, I get an invalid lit exception. I think that it compiles because the interface matches the class perfectly, but does not execute at runtime because the signature of the object does not see the IDatabase in the inheritance chain of the database class.
In C ++, you can get rid of casting, but you want to, but C # is a bit more strict regarding object pointers. Despite the fact that the class has all the correct function signatures, it explodes because the objects do not match.
Now I can put the interface of the db object in the assembly with the db object, but then the business logic needs a reference to the db assembly. In addition, it just complicates the work, because if I write a dub db object in unit test, I need a link to the db assembly only for the interface that I am going to use in my test stub. It does not seem to disengage the couplings, making it ...
I could put all the interfaces in the third assembly, which is the parent for the db assembly, business logic and unit tests. Here's how you can solve circular addiction problems. However, this associates the db assembly with the parent assembly and makes it much less modular for use with other projects.
I am open to suggestions on how I can configure each assembly so that they function independently and can be used for DI. I believe that I could store the test stub objects in the same assembly as the real code, but that seems weird.
Decision. One of the answers below makes a comment that what I shot is basically a duck for inputting interfaces. C # does not currently support duck printing, but I thought it was possible, since the implementation of the interface works similar to what you might call a partial class pointer (or, more precisely, a set of function pointers). My experiment showed me something else, and that is why.
so until Redmond adds “more mallard” to C #, it seems like we cannot fully achieve the final elegant level of decoupling of assemblies.