我真的希望能够在我的Entity Framework Code First数据库项目中使用NodaTime,但还没有找到一种"干净"的方法来实现它.我真正想做的是:
public class Photoshoot
{
public Guid PhotoshootId{get; set;}
public LocalDate ShootDate{get; set;} //ef ignores this property
}
Run Code Online (Sandbox Code Playgroud)
是否有任何支持或推荐的方法将NodaTime与EF Code First一起使用?
我意识到Web API是以REST为重点的,但我仍然希望配置一个可以处理命令/响应方案的控制器方法.到目前为止,我还没有成功......有没有办法让单个API端点识别以下类结构?
[Serializable]
public abstract class Command{
public int CommandId{get; set;}
}
[Serializable]
public class RegisterNewPersonCommand:Command{
public string Name{get; set;}
}
//etc... various Command subclasses. Similar thing for Responses.
//now have a single endpoint to Handle Commands
public class CommandsController : ApiController{
public Response HandleCommand(Command command){
//handle Command based on which subclass it is
//return appropriate Response subclass
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,似乎序列化系统似乎无法处理这种情况,但我希望那里有人找到了办法:)
Controller方法是否可以处理从特定基类派生的所有已发布项目?我们的想法是能够通过将命令发布到端点来调度命令.当我尝试以下操作时,Post方法中的"cmd"参数始终为null.
例
//the model:
public abstract class Command{
public int CommandId{get; set;}
}
public class CommandA:Command{
public string StringParam{get; set;}
}
public class CommandB:Command{
public DateTime DateParam{get; set;}
}
//and in the controller:
public HttpResponseMessage Post([FromBody]Command cmd)
{
//cmd parameter is always null when I Post a CommandA or CommandB
//it works if I have separate Post methods for each Command type
if (ModelState.IsValid)
{
if (cmd is CommandA)
{
var cmdA = (CommandA)cmd;
// do whatever
}
if (cmd …
Run Code Online (Sandbox Code Playgroud) 默认的MVC模板使用“ @ DateTime.Now.Year”来显示版权年份,但是我宁愿到处都使用NodaTime。
我目前正在使用Ninject将IClock的实例注入到执行时间或日期特定内容的Controller中。是否有建议的方式来访问MVC中类似于“ DateTime.Now”的“全局IClock”?我想我可以将IClock注入到每个Controller中,然后将其传递到每个视图中,但是有时可以访问全局对象会很不错。
我知道我可以在Layout模板中使用SystemClock.Instance ...而是引用一个全局的,可测试的IClock会更好。
我不确定如何提出这个问题,所以我会尽可能清楚地举一个例子.
在像facebook这样的应用中,个人资料可以有多个ProfilePictures.在任何给定时间,其中一个是"选定的"ProfilePicture(假设已经上传了ProfilePicture).
本能地,我会模仿这样:
Table: Profile
--------------
ProfileID
SelectedProfilePictureId //fk to ProfilePicture
Name, Age, Etc
Table: ProfilePicture
---------------------
ProfilePictureId
ProfileId //fk to Profile, indicating which Profile this picture belongs to
Url, DateTaken, Etc
Run Code Online (Sandbox Code Playgroud)
在这一点上,这些表格相互指向对我而言似乎"错误".它可以轻松查询没有SelectedProfilePictures的Profiles或获取Profile的SelectedPicture,但插入和更新有点不稳定.
这是不好的形式?Profile表是否应该与ProfilePicture表完全独立?是否存在根据数据库设计理论对其进行建模的"正确"方法,还是由程序员的排泄决定?