I have the following interfaces defined:
public interface IAudit {
DateTime DateCreated { get; set; }
}
public interface IAuditable {
IAudit Audit { get; set; }
}
The IAuditable
interface says which classes I will have an Audit
for. The IAudit
interface is the actual Audit
for that class. For example say I have the following implementations:
public class User : IAuditable {
public string UserName { get; set; }
public UserAudit Audit { get; set; }
}
public class UserAudit : IAudit {
public string UserName { get; set; }
public DateTime DateCreated { get; set; }
public UserAdit(User user) {
UserName = user.UserName;
}
}
Now given an object which is IAuditable
(User
from above), I'd like to be able to create an intance of IAudit
(UserAdit
from above) by feeding in the IAuditable
object into the constructor. Ideally i'd have something like:
if (myObject is IAuditable) {
var audit = new IAudit(myObject) { DateCreated = DateTime.UtcNow }; // This would create a UserAudit using the above example
}
However I have a bunch of problems:
IAudit
applies to which IAuditable
IAudit
interface must have a constructor which takes an IAuditable
I'm sure this is a design pattern many have had before but I can't get my head around it. I'd really appreciate if someone could show me how this can be achieved.
No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete. Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.
Interface can not be directly instantiated. We can create an instance of a class that implements the interface, then assign that instance to a variable of the interface type.
To create an object based on an interface, declare the object's type to be the interface, e.g. const obj1: Employee = {} . The object has to conform to the property names and the type of the values in the interface, otherwise the type checker throws an error.
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass) Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods.
You can't create an instance of an interface
Correct. You create an instance of an object implementing an interface:
IAuditable myUser = new User();
No where in the code does it define which IAudit applies to which IAuditable
You can't do this directly with just one interface. You will need to rethink your design.
You can use a open generic type in the interface and implement it with closed types:
public interface IAudit<T> {
DateTime DateCreated { get; set; }
}
public class UserAudit : IAudit<User> {
public string UserName { get; set; }
public DateTime DateCreated { get; set; }
public UserAdit(User user) {
UserName = user.UserName;
}
}
I can't specify that the IAudit interface must have a constructor which takes an IAuditable
Correct, you can't. See here. You need to create such a constructor on the implementers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With