我正在创建一个旅游规划器。始终Tour在相同的坐标处开始和结束。开始和结束之间有Stops。停靠站包含旅行期间的具体Sight参观地点 + 参观时间。将景点添加到游览时,我插入一个新的停靠点,并根据停靠点之间的距离重新计算停靠点的到达时间。为此,我ITravelService在private recalculateStopTimes(int fromIdx)方法中使用注入的实例。
我的问题如下:这一直有效,直到我想通过 ORM 将 Tour 对象保留在数据库中。由于私有 ITravelService,该功能将在检索后丢失。我考虑过通过 InsertSight/RemoveSight/RemoveStop 方法注入服务,但随后我需要用我创建的每个修改 Stops 的公共方法注入它。有没有更好的方法将这样的服务注入到实体中?或者我应该注射它吗?如果没有,我怎样才能获得相同的功能(重新计算停靠点)?
public interface ITravelService
{
public TimeSpan CalculateTimeBetween(Coordinates from, Coordinates to);
}
public class Tour : ITour
{
private readonly List<Stop> _stops;
private ITravelService _travelService;
public IReadOnlyList<Stop> Stops { get { return _stops; } }
public bool IsWithinLimit { get { return _stops.Last().TimeRange.From < (StartTime.TimeOfDay + Length); } }
public Tour(DateTime startTime, TimeSpan length, Coordinates start, …Run Code Online (Sandbox Code Playgroud) c# domain-driven-design entity-framework dependency-injection clean-architecture
我收到此错误:
数据库操作预计影响 1 行,但实际影响 0 行
当向实体集合添加新元素时。
项目中的基类Domain:
public class Tour
{
private List<Stop> _stops;
public Guid Id { get; private set; }
public Tour(Guid id)
{
Id = id;
_stops = new List<Stop>();
}
public IReadOnlyList<Stop> Stops { get { return _stops; } }
public void AddStop(Stop newStop)
{
//some logic and checking
_stops.Add(newStop);
}
}
public class Stop
{
public Stop(Guid id)
{
Id = id;
}
public Guid Id { get; private set; }
} …Run Code Online (Sandbox Code Playgroud) c# ×2