简体   繁体   English

使用存储库模式从抽象类访问虚拟方法

[英]Accessing virtual method from abstract class using repository pattern

I'm stuck in understanding - how to access and use (or is it even possible to use) base class virtual method.我一直在理解 - 如何访问和使用(或者甚至可以使用)基类虚拟方法。

So the code is: Base class:所以代码是: 基类:

public abstract class Vehicle
{
    public string VehicleIdentificationNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public abstract string DisplayName { get; }

    public virtual bool HasAnAmazingColor(){         
    }
}

Repository pattern:存储库模式:

public class VehicleRepository : IVehicleRepository, ICollection
{
    private readonly List<Vehicle> _vehicles;

    public int Count => _vehicles.Count;

    public object SyncRoot => ((ICollection)_vehicles).SyncRoot;

    public bool IsSynchronized => ((ICollection)_vehicles).IsSynchronized;

    public void CopyTo(Array array, int index)
    {
        ((ICollection)_vehicles).CopyTo(array, index);
    }

    public IEnumerator GetEnumerator()
    {
        return ((ICollection)_vehicles).GetEnumerator();
    }

    public VehicleRepository(List<Vehicle> aVehicles)
    {
        _vehicles = aVehicles;
    }

Then int unittest I try to get the virtual method, but do know that I'm not understanding something, but cannot figure out what, how can I use the method without overriding it?然后 int unittest 我尝试获取虚拟方法,但知道我不理解某些东西,但无法弄清楚是什么,如何在不覆盖它的情况下使用该方法?

  [TestClass()]
public class VehicleRepositoryTests
{

    private VehicleRepository vehicleList = new VehicleRepository(new List<Vehicle>());

    [TestMethod()]
       public void HasAmazingColor()
    {
        //arrange

        //act
        vehicleList.??? -- I'm missing something 

        //assert           
    }

I can access virtual method in any of the derived class that implements Vehicle, but is there a way to use it in repository pattern?我可以在任何实现 Vehicle 的派生类中访问虚方法,但是有没有办法在存储库模式中使用它?

Well, you have abstract base calls Vehicle .好吧,您有抽象的基本调用Vehicle

From careful observation of your code, I found that you have not derived your VehicleRepository Class from Vehicle .通过仔细观察你的代码,我发现你没有从Vehicle派生出你的VehicleRepository类。

So you will not be able to access the virtual method from Vehicle on VehicleRepository instance.因此,您将无法从VehicleRepository实例上的Vehicle访问虚拟方法。

The method you are looking for HasAmazingColor will be available on every single object in List<Vehicle> _vehicles;您正在寻找的方法HasAmazingColor将适用于List<Vehicle> _vehicles;每个对象List<Vehicle> _vehicles;

May be you can expose the List of Vehicles as property and then use it in the unit test, just in case you want to use it.也许您可以将车辆列表作为属性公开,然后在单元测试中使用它,以防万一您想使用它。

Or或者

If your design does not allow to explose the _vehicles as public collection, then you can have a public method in VehicleRepository class which internally calls the HasAmazingColor method on appropriate Vehicle object or objects.如果您的设计不允许将_vehicles公开为公共集合,那么您可以在VehicleRepository类中有一个公共方法,该方法在适当的Vehicle对象或多个对象上内部调用HasAmazingColor方法。

The solution will depend on your application design.解决方案将取决于您的应用程序设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM