当我需要一个扩展现有类行为的新类的名称时,我通常很难为它提出一个名称.
例如,如果我有一个MyClass类,那么新类可以命名为MyClassAdapter,MyClassCalculator,MyClassDispatcher,MyClassParser,......
这个新名称当然应该代表类的行为,理想情况下与使用它的设计模式相同(Adapter,Decorator,Factory,...).但既然我们不过度使用设计模式,这并不总是解决方案:)
那么,你知道一个字典或一个常用词列表,我们可以用它来表示类的行为,包含对预期行为的简短描述吗?一些例子:replicator,shadow,token,acceptor,worker,mapper,driver,bucket,socket,validator,wrapper,parser,verifier,...
您还可以将此列表视为隐喻的备忘单,使用该备忘录可以更好地了解您的问题域.
我想创建一个组件,它由一块板和它的周围角落组成.板的大小(因此也是边界的大小)在运行时定义.一些例子(板子很亮,边框很暗): 替代文字http://img340.imageshack.us/img340/3862/examplegw.png
该板由BoardCell类型的对象组成,边框由BorderCell类型的对象组成.电路板的数据结构是BoardCell [,] - 一个简单的二维数组.
我怎样才能代表边界?我从这样的事情开始:
public BorderCell TopLeft // top left corner cell
public BorderCell TopRight // top right corner cell
public BorderCell BottomRight // bottom right corner cell
public BorderCell BottomLeft // bottom left corner cell
public BorderCell[] Top // top border (without corners)
public BorderCell[] Bottom // bottom border (without corners)
public BorderCell[] Left // left border (without corners)
public BorderCell[] Right // right border (without corners)
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种边界的表现,你能提出更好的建议吗?
附加:我想在边框对象上有一个方法SetSomethingForTheCell:
public void SetSomethingForTheCell(...)
Run Code Online (Sandbox Code Playgroud)
但是根据我目前的数据结构,我不知道该作为参数传递什么.
我有一个字符串列表,我想将其转换为某种分组列表,其中值将按列表中的位置进行分组(不是正常分组,但在某种程度上,相同的项目只在一个组中如果他们在一起).请考虑以下示例:
LinkedList<string> myList = new LinkedList<string>();
myList.AddLast("aaa");
myList.AddLast("aaa");
myList.AddLast("bbb");
myList.AddLast("bbb");
myList.AddLast("aaa");
myList.AddLast("aaa");
myList.AddLast("aaa");
LinkedList<MyTuple> groupedList = new LinkedList<MyTuple>();
groupedList.AddLast(new MyTuple("aaa", 2));
groupedList.AddLast(new MyTuple("bbb", 2));
groupedList.AddLast(new MyTuple("aaa", 3));
Run Code Online (Sandbox Code Playgroud)
这个转换可以用LINQ完成,还是应该用循环编写算法?
在我的页面上,我有:
搜索工作正常 - 我可以输入一些文本或选中过滤器部分中的复选框,并显示相应的结果.
分页工作正常只有在我加载页面时才使用它(这意味着在我点击搜索按钮之前,在这种情况下,网址是'...主页').
但是如果首先单击搜索(在这种情况下,url将变为'... Home/Search')然后尝试转到网格上的另一个页面,那么我在Search方法中得到一个异常,因为model.Filter参数是null(System.NullReferenceException:对象引用未设置为对象的实例.)
我试图以许多不同的方式解决问题(使用RedirectToAction方法,将过滤器存储到会话并在Search方法中使用它,...)但是没有解决方案适用于所有场景.有任何想法吗?
我的简化代码:
HomeController的:
public ActionResult Index()
{
// On page load display all data without filters.
var filter = new OverviewFilterModel
{
Type1 = true,
Type2 = true,
WorkingOrder = ""
};
ViewBag.Results = GetResults(filter);
return View(new HomeModel { Filter = filter });
}
public ActionResult Search(HomeModel model)
{
ViewBag.Results = GetResults(model.Filter);
return View("Index");
}
public class OverviewFilterModel
{
public bool Type1 { get; set; …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
double result = Math.Sqrt(4746073226998689451);
Run Code Online (Sandbox Code Playgroud)
结果我得到2178548422而不是2178548421.999999854etc ...我怎样才能得到更精确的结果?
表:
create table Documents
(Id int,
SomeText varchar(100),
CustomerId int,
CustomerName varchar(100)
)
insert into Documents (Id, SomeText, CustomerId, CustomerName)
select 1, '1', 1, 'Name1'
union all
select 2, '2', 2, 'Name2'
Run Code Online (Sandbox Code Playgroud)
类别:
public class Document
{
public int Id { get; set; }
public string SomeText { get; set; }
public Customer { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我如何与Dapper Documents一起完成所有工作 …
我有一个web方法,从jquery的ajax方法调用,如下所示:
$.ajax({
type: "POST",
url: "MyWebService.aspx/DoSomething",
data: '{"myClass": ' + JSON.stringify(myClass) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (result) {
alert("success");
},
error: function () {
alert("error");
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的网络方法:
[WebMethod(EnableSession = true)]
public static object DoSomething(MyClass myClass)
{
HttpContext.Current.Request.InputStream.Position = 0;
using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
Logger.Log(reader.ReadToEnd());
}
}
Run Code Online (Sandbox Code Playgroud)
如果javascript中的myClass被序列化为正确的格式,则DoSomething方法执行并将原始json保存到数据库.但是如果myClass出错了,那么该方法根本不会执行,我无法记录有问题的json ......
即使序列化失败,总是以某种方式获取并记录我的Web方法接收的原始json的最佳方法是什么?
这应该很简单:如何在 DockPanel 内拉伸 StackPanel,以便它填充整个父级的内容并保持其 HorizontalAlignment?
例子:
<DockPanel LastChildFill="True">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Yellow">
<Button Height="30">Button1</Button>
<Button Height="30">Button2</Button>
</StackPanel>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
在此示例中,StackPanel 的宽度与两个按钮的组合宽度相同(此处的背景为黄色),但 DockPanel 中的剩余空间保持白色。看起来 LastChildFille 属性在我的示例中不起作用。
我正在将对WCF Web服务的所有请求(包括参数)记录到数据库中.这就是我这样做的方式:
这工作正常,但有时参数非常大,例如一个自定义类,带有几个带有照片,指纹的字节数组等.我想从序列化中排除所有这些字节数组数据类型,这将是最好的方法做到了吗?
序列化json的示例:
[
{
"SaveCommand":{
"Id":5,
"PersonalData":{
"GenderId":2,
"NationalityCode":"DEU",
"FirstName":"John",
"LastName":"Doe",
},
"BiometricAttachments":[
{
"BiometricAttachmentTypeId":1,
"Parameters":null,
"Content":"large Base64 encoded string"
}
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
期望的输出:
[
{
"SaveCommand":{
"Id":5,
"PersonalData":{
"GenderId":2,
"NationalityCode":"DEU",
"FirstName":"John",
"LastName":"Doe",
},
"BiometricAttachments":[
{
"BiometricAttachmentTypeId":1,
"Parameters":null,
"Content":"..."
}
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
编辑:我不能更改用作Web服务方法的参数的类 - 这也意味着我不能使用JsonIgnore属性.
我需要一个具有以下要求的简单数据结构:
我对多线程的经验非常有限,但这就是我的目标:
public class Tickets
{
private ConcurrentQueue<uint> _tickets;
public Tickets(uint from, uint to)
{
Initialize(from, to);
}
private readonly object _lock = new object();
public void Initialize(uint from, uint to)
{
lock(_lock)
{
_tickets = new ConcurrentQueue<uint>();
for (uint i = from; i <= to; i++)
{
_tickets.Enqueue(i);
}
}
}
public uint Dequeue()
{
uint number;
if (_tickets.TryDequeue(out number))
{
return number;
}
throw new ArgumentException("Ticket queue empty!");
}
}
Run Code Online (Sandbox Code Playgroud)
第一个问题:这段代码好吗?
Secod问题:我怎样才能对这个类进行单元测试(例如,两个线程在队列中定期执行出队操作,元素为(1,2,3,4,5,6),第一个线程只能得到奇数和第二个线程只有偶数)?我试过这个,但断言没有执行:
[Test]
public void Test() …Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×4
dictionary ×2
asp.net ×1
asp.net-mvc ×1
dapper ×1
dockpanel ×1
jquery ×1
json ×1
json.net ×1
linq ×1
list ×1
orm ×1
postsharp ×1
queue ×1
sql-server ×1
sqrt ×1
stackpanel ×1
telerik-grid ×1
terminology ×1
trace ×1
unit-testing ×1
vocabulary ×1
wcf ×1
wpf ×1