我正在构建一个简单的Guard API来防止传递给函数的非法参数等.
我有以下代码:
public static class Guard
{
public static GuardArgument<T> Ensure<T>(T value, string argumentName)
{
return new GuardArgument<T>(value, argumentName);
}
}
public class GuardArgument<T>
{
public GuardArgument(T value, string argumentName)
{
Value = value;
Name = Name;
}
public T Value { get; private set; }
public string Name { get; private set; }
}
// Example extension for validity checks
public static GuardArgument<T> IsNotNull<T>(this GuardArgument<T> guardArgument, string errorMessage)
{
if (guardArgument.Value == null)
{
throw new ArgumentNullException(guardArgument.Name, errorMessage); …Run Code Online (Sandbox Code Playgroud) 我在 ReactNative 中有一个 FlatList,它从 API 中提取文章列表。当滚动到达列表末尾时,会从 API 中拉出另一页文章,并将其附加到 Redux reducer 中的文章列表。
FlatList 设置为:
render() {
return(
<FlatList data={this.props.articles.articles} // The initial articles list via Redux state
renderItem={this.renderArticleListItem} // Just renders the list item.
onEndReached={this.pageArticles.bind(this)} // Simply calls a Redux Action Creator/API
onEndReachedThreshold={0}
keyExtractor={item => item.id.toString()}/>
)};
Run Code Online (Sandbox Code Playgroud)
Redux 的“文章”状态对象使用 React Redux 连接函数映射到组件。相关的减速器(为简洁起见删除了一些项目)看起来像:
/// Initial 'articles' state object
const INITIAL_STATE = {
articles: [],
loading: false,
error: '',
pageIndex: 0,
pageSize: 10
};
export default(state = INITIAL_STATE, action) => {
switch(action.type){
// …Run Code Online (Sandbox Code Playgroud) 我有两个实体和两个DTO.我正在将实体映射到DTO.DTO的简化版本如下所示:
public class FooDto {
// Other properties removed for clarity.
public string Description { get; set; }
public decimal Total { get; set; }
public ICollection<BarDto> Bars { get; set; }
}
public class BarDto {
// Other properties removed for clarity.
public decimal Total { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这些Foo和Bar类是:
public class Foo {
public ICollection<Bar> Bars { get; set; }
}
public class Bar {
// Unimportant properties
}
Run Code Online (Sandbox Code Playgroud)
映射
我在一个方法中将其映射为:
public FooDto …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的问题,使用Simple Injector将依赖项注入MVC控制器.我以前使用过SimpleMap的Simple Injector.
MVC的版本是4.5,它是从NuGet安装的Simple Injector的最新版本.
查看HomeController的/ Index视图时出现的错误是:
HomeController类型的构造函数包含IContext类型的参数,其名称为"context",未注册.请确保IContext已在容器中注册,或更改HomeController的构造函数.
控制器如下:
public class HomeController : Controller
{
public HomeController(IContext context) { }
public ActionResult Index() { }
}
Run Code Online (Sandbox Code Playgroud)
IContext只是一个简单的界面:
public interface IContext
{
}
Run Code Online (Sandbox Code Playgroud)
具体的IContext实现也很简单,只是常规DbContext的包装器.
public class DbContext : System.Data.Entity.DbContext, IContext
{
}
Run Code Online (Sandbox Code Playgroud)
有关信息,IContext接口与DbContext实现位于不同的VS Project/Assembly中.这些由MVC项目引用.
我在Global.asax.cs中有以下内容:
protected void Application_Start()
{
var container = new Container();
container.Register<IContext, DbContext>();
container.RegisterMvcControllers(System.Reflection.Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
// Regular MVC startup
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
[ActivationException: The constructor of the type HomeController contains the …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc dependency-injection simple-injector asp.net-mvc-4
我有以下Angular/HTML,它使用Bootstrap CSS类来指示表单是否有效使用Angular验证.
<form name="editor" novalidate>
<div class="form-group" ng-class="{'has-error': editor.name.$dirty && (editor.name.$error.invalid || editor.name.$error.required)}">
<input type="text" class="form-control" name="name" maxlength="150" data-ng-model="name" required />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
拥有超过一个div.form-group显然是有大量的代码重复.我想要做的是创建一个Angular属性指令,div.form-group如果元素中包含的输入无效,它将更新元素的类.
这是我想要的标记:
<div class="form-group" data-form-group data-input="editor.name">
...
</div>
Run Code Online (Sandbox Code Playgroud)
我有一个指令的shell,但我不知道如何监视editor.name(或input属性)以更新类.
myApp.directive("formGroup", function () {
return {
restrict: "A",
scope: {
input: "@"
},
replace: false,
link: function (scope, elem, attrs) {
}
};
});
Run Code Online (Sandbox Code Playgroud)
我假设我需要将相关代码放在链接函数中,并且可能使用$watch但除此之外我有点在黑暗中
我在通过实体框架/Linq-to-Entities 访问数据库的存储库中有许多复杂的查询。这些查询通常由许多非平凡的子查询构成。一般来说,子查询用于不同的存储库方法以及其他域逻辑。它们位于存储库层的外部,但可以访问存储库层是有意义的。
因此,我想使用规范模式来封装其中一些子查询。
我正在为我的规范类使用基类:
public abstract class Specification<T> : ISpecification<T> where T : class
{
public abstract Expression<Func<T, bool>> ToExpression();
public virtual bool IsSatisfiedBy(T candidate)
{
var predicate = ToExpression().Compile();
return predicate(candidate);
}
public Specification<T> And(Specification<T> specification)
{
return new AndSpecification<T>(this, specification);
}
public Specification<T> Or(Specification<T> specification)
{
return new OrSpecification<T>(this, specification);
}
}
Run Code Online (Sandbox Code Playgroud)
示例规范可能如下所示:
public class IsAssignmentSetForStudentSpecification : Specification<Assignment>
{
private readonly Student _student;
public IsAssignmentSetForStudentSpecification(Student student)
{
_student = student;
}
public override Expression<Func<Assignment, bool>> ToExpression()
{
return …Run Code Online (Sandbox Code Playgroud) 我有以下命令处理程序.处理程序接受命令对象并使用其属性来创建或更新实体.
它由Id可以为空的命令对象上的属性决定.如果为null,则创建,如果不是,则更新.
public class SaveCategoryCommandHandler : ICommandHandler<SaveCategoryCommand>
{
public SaveCategoryCommandHandler(
ICategoryRepository<Category> categoryRepository,
ITracker<User> tracker,
IMapProcessor mapProcessor,
IUnitOfWork unitOfWork,
IPostCommitRegistrator registrator)
{
// Private fields are set up. The definitions for the fields have been removed for brevity.
}
public override void Handle(SaveCategoryCommand command)
{
// The only thing here that is important to the question is the below ternary operator.
var category = command.Id.HasValue ? GetForUpdate(command) : Create(command);
// Below code is not important to the question. It …Run Code Online (Sandbox Code Playgroud) 我有一个角度控制器,显示背景图像和短信.控制器是:
var myArchiveController = function($scope) {
var setBackground = function() {
$scope.backgroundUrl = someUrlFromService;
$scope.backgroundMessage = someMessageFromService;
}
setBackground();
}
app.controller("myController", myController);
Run Code Online (Sandbox Code Playgroud)
如何setBackground()定期调用该功能,例如每分钟?
我有一个工作单元,它在Simple Injector中注册了一个由各种Web应用程序共享的通用程序集.
它注册为:
Container.Register(typeof(IUnitOfWork), typeof(UnitOfWork), new WebRequestLifestyle());
Run Code Online (Sandbox Code Playgroud)
我现在有一个控制台应用程序需要使用程序集中定义的容器,但显然无法使用WebRequestLifestyle.如果我尝试,我会收到以下错误:
它
IUnitOfWork被注册为"Web请求"生活方式,但实例是在Web请求的上下文之外请求的.
有没有我可以使用的替代方案WebRequestLifestyle,它将实现相同的功能,但对于Web和控制台应用程序?
我正在编写一个静态防护类/ api来验证发送给方法的参数.
到目前为止的代码如下:
public static class Guard
{
public static GuardArgument<T> Ensure<T>(T value, string argumentName)
{
return new GuardArgument<T>(value, argumentName);
}
public static T Value<T>(this GuardArgument<T> guardArgument)
{
return guardArgument.Value;
}
// Example extension method
public static GuardArgument<T> IsNotNull<T>(this GuardArgument<T> guardArgument, string errorMessage)
{
if (guardArgument.Value == null)
{
throw new ArgumentNullException(guardArgument.Name, errorMessage);
}
return guardArgument;
}
}
Run Code Online (Sandbox Code Playgroud)
它可以这样使用:
public void Test(IFoo foo) {
Guard.Ensure(foo, "foo").IsNotNull();
}
Run Code Online (Sandbox Code Playgroud)
现在的情况要求我需要从提供的界面转换为具体类型.不要问为什么,我只需要!
我想添加一个As扩展方法GuardArgument来执行此操作,例如:
public static GuardArgument<TOut> As<TOut, TIn>(this GuardArgument<TIn> guardArgument, …Run Code Online (Sandbox Code Playgroud) 我有以下Angular自定义指令代码:
模板(ReviewStandards.html)
<div class="review-standard" ng-repeat="standard in standards">
<button ng-click="mark(standard)">Mark Complete</button>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
app.directive("reviewStandards", function ($parse, $state) {
return {
restrict: "A",
templateUrl: function (elements, attrs) {
return "/Scripts/App/Directives/Templates/ReviewStandards.html";
},
transclude: false,
scope: {
standards: "="
},
replace: true,
link: function (scope, elem, attrs) {
scope.mark = function (standard) {
alert();
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
该指令用作:
<div review-standards standards="review.ReviewStandards"></div>
Run Code Online (Sandbox Code Playgroud)
哪里standards只是一个JSON standard对象数组.
问题ng-click是单击按钮时没有触发该功能.范围是孤立的 - 这是与这个或按钮处于一个事实有关ng-repeat吗?
我有以下方法在C#类中包含一个简单的foreach循环.该方法返回使用单独类上的函数计算的总计的总和.
private readonly ICalculateTotalService _calculateTotalService;
public decimal GetTotal(IOrder order)
{
decimal paidTotal = 0;
foreach (var line in order.Lines)
{
paidTotal += _calculateTotalService.GetTotal(line);
}
return paidTotal;
}
Run Code Online (Sandbox Code Playgroud)
Resharper建议将其重构为LINQ语句.这样做最好的方法是什么?
c# ×8
.net ×3
angularjs ×3
javascript ×2
linq ×2
asp.net-mvc ×1
automapper ×1
css ×1
fluent ×1
generics ×1
lambda ×1
react-native ×1
redux ×1
resharper ×1