我正在考虑构建一些通用扩展,它们将采用所有这些null,抛出检查和断言,而是使用流畅的API来处理这个问题.
所以我想做这样的事情.
Shall() - Not quite sure about this one yet
.Test(...) - Determines whether the contained logic executed without any errors
.Guard(...) - Guards the contained logic from throwing any exception
.Assert(...) - Asserts before the execution of the code
.Throw(...) - Throws an exception based on a certain condition
.Assume(...) - Similar to assert but calls to Contract.Assume
Run Code Online (Sandbox Code Playgroud)
用法:father.Shall().Guard(f => f.Shop())
问题是我在运行时不想要这些额外的调用,我知道AOP可以为我解决这个问题,我想直接将这些调用内联到调用者,如果你有更好的方法,请告诉我.
现在,在我研究或做任何事情之前,我想知道某人是否已经这样做或知道正在做的工具?
我真的想构建类似的东西并将其发布给公众,因为我认为它可以节省大量时间和头痛.
一些例子.
DbSet<TEntity> set = Set<TEntity>();
if (set != null)
{
if (Contains(entity))
{
set.Remove(entity);
}
else
{
set.Attach(entity);
set.Remove(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
更改以下内容.
Set<TEntity>().Shall().Guard(set =>
{
if (Contains(entity))
{
set.Remove(entity);
}
else
{
set.Attach(entity);
set.Remove(entity);
}
});
Run Code Online (Sandbox Code Playgroud)
有些人可以真正了解成熟度,而不是有趣并试图取笑其他人,你可以分享你的经验并告诉我它有什么好坏,我会接受.
我不是要重新创建代码契约,我知道我每天都在使用它,我正在尝试移动写入一个地方的样板代码.
有时你有方法,每次调用你必须检查返回的对象,而不是你的代码,所以你不能确保被调用者不会产生null,所以在调用者你必须对返回的对象执行空检查所以我想到了一些可以让我在链接电话时轻松执行这些检查的东西.
更新:我将不得不考虑更多,并更改API以使意图清晰,代码更具可读性.
我认为这个想法根本没有完善,而且我确实用这些方法走得太远了.
无论如何,我现在就把它留下来.