Is there a Design Pattern matching with this approach?
I have one DTO which has only the simple properties on it.
I would like to make some tests on DTO instances let's say like: IsOld, IsFromBataFamily etc.
I was thinking to extend the DTO class and implement those tester methods.
public class T开发者_如何学运维estableDTO : DTO
{
public bool IsOld() { // some logic }
public bool IsFromBataFamily () { // some logic }
}
Is there any design pattern matching with this approach (Extending a DTO class to add some testers on it to classify its category)?
Can you name it, please?
That's just plain inheritance, not sure if it has a pattern associated with it. If your TestableDTO
held a DTO
reference internally with the aim of adding members to it, that would be the Decorator pattern, or at least a flavour of it.
public class TestableDTO
{
private DTO _dto;
public bool IsOld() { // some logic }
public bool IsFromBataFamily () { // some logic }
// Expose DTO public members again...
}
So the short answer is no, currently this isn't a "design pattern" per se, but instead an OO paradigm of class inheritance, or extensibility.
Just to clarify, what you have done to add members to the class is perfectly fine. However, the reasons for it (just for testing) might be considered a little odd, as you should just be able to directly test DTO
.
This is similar to the decorator pattern but not quite. The closest thing I can think of is extension by inheritance or specialisation.
Roy Osherove describes a method called Extract and Override which you can read about in his book the Art of Unit Testing (which I recommend greatly). The online sample chapter describes this.
This should give some better ideas on how to to use inheritance to make objects more testable.
This doesn't answer your question, but wouldn't the following be a better approach?
[Flags]
public enum TestDTOFlags
{
None = 0,
Old = 1,
FromBataFamily = 2
}
public class TestableDTO : DTO
{
public TestDTOFlags DetermineFlags() { /* some logic */ }
}
To even further separate this from your business logic, maybe this could be an extension method in your test suite:
public static class DTOExtensions
{
public static TestDTOFlags DetermineFlags(this DTO target) { /* some logic */ }
}
If you are trying to extend a specific DTO for unittest purposes, the simplest and cleanest way, is to implement an extension method - e.g.
public static Class DtoExtensionMethods {
public static bool IsTooOld(this SomeSpecificDto specificDto)
{
return specificDto.LastModified < DateTime.Now - TimeSpan.FromHours(2) ;
}}
精彩评论