If I have a method like public static IBook create(IDBRepository repository) { } How do I create an instance of IBook?
You don’t create instances of interfaces per se. Think of an interface as a contract that defines an ability that the instance of a type has.
For example: If I have an interface IDriveable
that has methods Accelerate()
, Brake()
, TurnLeft()
and TurnRight()
then I might have classes Car
, Truck
and Bicycle
that implement that interface – what that’s saying is that any instance of IDriveable
(i.e. any object that implements IDriveable
) can do those things. When I have a method (static or otherwise) that returns an instance of IDriveable
then the method would return an instance of Car
, Truck
or Bicycle
(instantiating that object if required).
Edit: So such a method might look like:
The answer from /u/maddaneccles1 is correct.
The point of static members is you don’t need to create instances to use them. It used to be true you could NOT make static members on interfaces, but since C# 8 added default interface implementations this became a thing.
Let’s say we wrote this:
If we want to call that method, we treat it just as if it were a static member of a class:
You aren’t supposed to call static members from instances. The compiler issues a warning if you do. In theory it’s not bad, but if you asked me to spend an hour or two I could show you how it’s a great way to confuse yourself. Sort of like using default interface implementations.
However, I wonder if you’re asking a different question. Let’s say I had this:
Maybe you’re asking how you implement a method like:
The answer is: “it depends”. This method signature is sort of weird. Let’s walk through how interfaces work.
If all you have is the interface, you CAN’T create instances that implement it. You can only instantiate classes or structs. So you’d have to have something like this:
(Slight aside: note there isn’t a way for Book
to access static methods defined on IBook
. Static methods DO NOT participate in inheritance and this is especially true for interfaces.)
Now that we have some kind of class that can implement the interface, we can instantiate that:
The reason your method confuses me is you’re indicating some kind of Repository is the input. That thing is supposed to know how to create the types. In general, it works with the concrete types, not interfaces, for the reasons above: it can’t create an instance of an interface. So your original method would likely have to be more like:
There are some caveats here, but I have to write something like 80% of a functional application to show off all the ways you might end up creating this method. The trick is this method might return an IBook
, but it can only do that if your IDbRepository
can return IBook
or a concrete type like Book
that implements it.
It sounds like you’re asking about factory methods. Do you want something like this?
In this case, you want to create a constructor as normal, but make it protected or private, so that only the class itself can access it. Then the Create method can make use of the constructor, and no-one else can.
C# devs
null reference exceptions