这是一个非常具体的问题.不太确定如何说出来.基本上我正在实现工作单元和存储库模式,我有一个动态对象,我转换为int,但如果我使用var它将在尝试调用方法时抛出异常.
我试图删除所有这些问题,我可以解决这个问题.出于某种原因,我只看到这两种设计模式.我得到的例外是Additional information: 'BlackMagic.ITacoRepo' does not contain a definition for 'DoStuff'
这是代码:
class BlackMagic
{
static void Main(string[] args)
{
dynamic obj = new ExpandoObject();
obj.I = 69;
UnitOfWork uow = new UnitOfWork();
int i1 = Convert.ToInt32(obj.I);
var i2 = Convert.ToInt32(obj.I);
if(i1.Equals(i2))
{
uow.TacoRepo.DoStuff(i1); // Works fine
uow.TacoRepo.DoStuff(i2); // Throws Exception
}
}
}
class UnitOfWork
{
public ITacoRepo TacoRepo { get; set; }
public UnitOfWork()
{
TacoRepo = new TacoRepo();
}
}
class Repo<T> : IRepo<T> …Run Code Online (Sandbox Code Playgroud) c# unit-of-work repository-pattern expandoobject dynamicobject
直觉上,我认为当你创建一个 MaxByteReader 并传入 http.ResponseWriter 时,它会为你写出状态码。但事实并非如此,作者实际上是做什么的?
例子:
func maxBytesMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 1)
next.ServeHTTP(w, r)
})
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
var i interface{}
err := json.NewDecoder(r.Body).Decode(&i)
if err != nil {
fmt.Println(err.Error())
}
}
func TestMaxBytesMiddleware(t *testing.T) {
handlerToTest := maxBytesMiddleware(http.HandlerFunc(mainHandler))
req := httptest.NewRequest(http.MethodPost, "http://test.com", bytes.NewReader(json.RawMessage(`{"hello":"world"}`)))
recorder := httptest.NewRecorder()
handlerToTest.ServeHTTP(recorder, req)
if recorder.Result().StatusCode != http.StatusRequestEntityTooLarge {
t.Errorf("expected %d got %d", http.StatusRequestEntityTooLarge, recorder.Result().StatusCode)
}
}
Run Code Online (Sandbox Code Playgroud)
但是当这个测试运行时,我得到了这个:
http: request body …Run Code Online (Sandbox Code Playgroud)