C# 如何在实现类似reddits posts sort的功能时避免切换情况
我有所有排序策略(SortPostsByTop、SortPostsByBest、SortPostsByNew)的接口ISortPostsStrategy我需要带有选项的排序来为所有排序类型选择时间框架,但没有一个,我尝试使用策略模式,但问题是ISortPostsStrategy有SortPostByNew不需要的时间框架参数,它最终被未使用C# 如何在实现类似reddits posts sort的功能时避免切换情况,c#,design-patterns,switch-statement,C#,Design Patterns,Switch Statement,我有所有排序策略(SortPostsByTop、SortPostsByBest、SortPostsByNew)的接口ISortPostsStrategy我需要带有选项的排序来为所有排序类型选择时间框架,但没有一个,我尝试使用策略模式,但问题是ISortPostsStrategy有SortPostByNew不需要的时间框架参数,它最终被未使用 public interface ISortPostsStrategy { Task<IEnumerable<Post>>
public interface ISortPostsStrategy
{
Task<IEnumerable<Post>> SortAsync(string userId, DateTime startDate);
}
public class SortPostsByNew : ISortPostsStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByNew(UnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId, DateTime startDate)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubcribedUserOrderedByNewAsync(userId);
return dbPosts;
}
}
public class SortPostsByBest : ISortPostsStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByBest(UnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId, DateTime startDate)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubscribedUserOrderedByBestAsync(userId, startDate);
return dbPosts;
}
}
公共接口ISortPostsStrategy
{
任务排序同步(字符串userId,DateTime startDate);
}
公共类SortPostsByNew:ISortPostsStrategy
{
私有只读UnitOfWork UnitOfWork;
公共SortPostByNew(UnitOfWork UnitOfWork)
{
this.unitOfWork=unitOfWork;
}
公共异步任务SortAsync(字符串userId,DateTime startDate)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedUserOrderedByNewAsync(userId);
返回岗位;
}
}
公共类SortPostsByBest:ISortPostsStrategy
{
私有只读UnitOfWork UnitOfWork;
公共排序桩B(UnitOfWork UnitOfWork)
{
this.unitOfWork=unitOfWork;
}
公共异步任务SortAsync(字符串userId,DateTime startDate)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedSerOrderedByBestAsync(userId,startDate);
返回岗位;
}
}
这就是我在战略模式出现之前试图避免的情况
IEnumerable<Post> dbPosts = null;
if (sortType == PostSortType.New)
{
dbPosts = await this.redditCloneUnitOfWork.Posts
.GetBySubcribedUserOrderedByNewAsync(dbUser.Id);
}
else if (sortType == PostSortType.Top)
{
dbPosts = await this.redditCloneUnitOfWork.Posts
.GetBySubcribedUserOrderedByTopAsync(dbUser.Id, startDate);
}
else if (sortType == PostSortType.Controversial)
{
dbPosts = await this.redditCloneUnitOfWork.Posts
.GetBySubscribedUserOrderedByControversialAsync(dbUser.Id, startDate);
}
else if (sortType == PostSortType.Best)
{
dbPosts = await this.redditCloneUnitOfWork.Posts
.GetBySubscribedUserOrderedByBestAsync(dbUser.Id, startDate);
}
var models = this.mapper.Map<IEnumerable<PostConciseViewModel>>(dbPosts);
IEnumerable dbPosts=null;
if(sortType==PostSortType.New)
{
dbPosts=等待this.redditCloneUnitOfWork.Posts
.GetBySubscribedUserOrderedByNewAsync(dbUser.Id);
}
else if(sortType==PostSortType.Top)
{
dbPosts=等待this.redditCloneUnitOfWork.Posts
.getBySubscribedUserOrderedByTopAsync(dbUser.Id,startDate);
}
else if(sortType==PostSortType.confirative)
{
dbPosts=等待this.redditCloneUnitOfWork.Posts
.GetBySubscribedSerOrderedByConferenceAsync(dbUser.Id,startDate);
}
else if(sortType==PostSortType.Best)
{
dbPosts=等待this.redditCloneUnitOfWork.Posts
.GetBySubscribedSerOrderedByBestAsync(dbUser.Id,startDate);
}
var models=this.mapper.Map(dbPosts);
根据我的理解,问题在于DateTime是一个(或一些)具体策略的实现细节。因此,高级接口不应包含此信息。但是,如果您仍然需要跨多个策略使用此日期时间,则可以创建一个BaseTimeDependentPostSortingStrategy
(或您应该选择的任何名称)
您正在使用向方法中提供日期时间。您可以将需要timespan的类型注入构造函数
public interface ISortPostsStrategy
{
Task<IEnumerable<Post>> SortAsync(string userId);
}
public abstract class BaseTimeDependentPostSortingStrategy : ISortPostsStrategy
{
private readonly DateTime _startDate;
protected BaseTimeDependentPostSortingStrategy(DateTime startDate)
{
_startDate = startDate;
}
public abstract Task<IEnumerable<Post>> SortAsync(string userId);
}
public class SortPostsByNew : ISortPostsStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByNew(UnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubcribedUserOrderedByNewAsync(userId);
return dbPosts;
}
}
public class SortPostsByBest : BaseTimeDependentPostSortingStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByBest(UnitOfWork unitOfWork, DateTime startDate) : base(startDate)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubscribedUserOrderedByBestAsync(userId, _startDate);
return dbPosts;
}
}
公共接口ISortPostsStrategy
{
任务排序同步(字符串用户ID);
}
公共抽象类BaseTimeDependentPostSortingStrategy:ISortPostsStrategy
{
私有只读日期时间_startDate;
受保护的BaseTimeDependentPostSortingStrategy(日期时间开始日期)
{
_开始日期=开始日期;
}
公共抽象任务SortAsync(字符串userId);
}
公共类SortPostsByNew:ISortPostsStrategy
{
私有只读UnitOfWork UnitOfWork;
公共SortPostByNew(UnitOfWork UnitOfWork)
{
this.unitOfWork=unitOfWork;
}
公共异步任务排序同步(字符串用户ID)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedUserOrderedByNewAsync(userId);
返回岗位;
}
}
公共类SortPostsByBest:BaseTimeDependentPostSortingStrategy
{
私有只读UnitOfWork UnitOfWork;
公共排序PostsByBest(UnitOfWork UnitOfWork,DateTime startDate):基(startDate)
{
this.unitOfWork=unitOfWork;
}
公共异步任务排序同步(字符串用户ID)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedSerOrderedByBestAsync(userId,_startDate);
返回岗位;
}
}
免责声明:这可能无法编译,我还没有测试过它
如果这不能回答您的问题,请提供其他信息,以便我提供帮助。据我所知,问题在于日期时间是一个(或一些)具体策略的实施细节。因此,高级接口不应包含此信息。但是,如果您仍然需要跨多个策略使用此日期时间,则可以创建一个
BaseTimeDependentPostSortingStrategy
(或您应该选择的任何名称)
您正在使用向方法中提供日期时间。您可以将需要timespan的类型注入构造函数
public interface ISortPostsStrategy
{
Task<IEnumerable<Post>> SortAsync(string userId);
}
public abstract class BaseTimeDependentPostSortingStrategy : ISortPostsStrategy
{
private readonly DateTime _startDate;
protected BaseTimeDependentPostSortingStrategy(DateTime startDate)
{
_startDate = startDate;
}
public abstract Task<IEnumerable<Post>> SortAsync(string userId);
}
public class SortPostsByNew : ISortPostsStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByNew(UnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubcribedUserOrderedByNewAsync(userId);
return dbPosts;
}
}
public class SortPostsByBest : BaseTimeDependentPostSortingStrategy
{
private readonly UnitOfWork unitOfWork;
public SortPostsByBest(UnitOfWork unitOfWork, DateTime startDate) : base(startDate)
{
this.unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Post>> SortAsync(string userId)
{
var dbPosts = await this.unitOfWork.Posts.GetBySubscribedUserOrderedByBestAsync(userId, _startDate);
return dbPosts;
}
}
公共接口ISortPostsStrategy
{
任务排序同步(字符串用户ID);
}
公共抽象类BaseTimeDependentPostSortingStrategy:ISortPostsStrategy
{
私有只读日期时间_startDate;
受保护的BaseTimeDependentPostSortingStrategy(日期时间开始日期)
{
_开始日期=开始日期;
}
公共抽象任务SortAsync(字符串userId);
}
公共类SortPostsByNew:ISortPostsStrategy
{
私有只读UnitOfWork UnitOfWork;
公共SortPostByNew(UnitOfWork UnitOfWork)
{
this.unitOfWork=unitOfWork;
}
公共异步任务排序同步(字符串用户ID)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedUserOrderedByNewAsync(userId);
返回岗位;
}
}
公共类SortPostsByBest:BaseTimeDependentPostSortingStrategy
{
私有只读UnitOfWork UnitOfWork;
公共排序PostsByBest(UnitOfWork UnitOfWork,DateTime startDate):基(startDate)
{
this.unitOfWork=unitOfWork;
}
公共异步任务排序同步(字符串用户ID)
{
var dbPosts=wait this.unitOfWork.Posts.getBySubscribedSerOrderedByBestAsync(userId,_startDate);
返回岗位;
}
}
迪斯莱
ISortStrategy GetSortStrategy(GetSortedPostsRequestDto dto)
{
switch case dto.SortMode
{
case SortMode.New: return new SortByNewStrategy();
case SortMode.Top: return new SortByTopStrategy();
case SortMode.Recent: return new SortByRecentStrategy(dto.StartDate);
//etc.
}
}