我正在编写一个集成测试,我将把一些对象插入数据库,然后检查以确定我的方法是否检索这些对象.
我与数据库的连接是通过NHibernate ...而我创建这样一个测试的常用方法是执行以下操作:
NHibernateSession.BeginTransaction();
//use nhibernate to insert objects into database
//retrieve objects via my method
//verify actual objects returned are the same as those inserted
NHibernateSession.RollbackTransaction();
Run Code Online (Sandbox Code Playgroud)
但是,我最近发现了TransactionScope,它显然可以用于这个目的......
public static int AddDepartmentWithEmployees(Department dept)
{
int res = 0;
DepartmentAdapter deptAdapter = new DepartmentAdapter();
EmployeeAdapter empAdapter = new EmployeeAdapter();
using (TransactionScope txScope = new TransactionScope())
{
res += deptAdapter.Insert(dept.DepartmentName);
//Custom method made to return Department ID
//after inserting the department "Identity Column"
dept.DepartmentID = deptAdapter.GetInsertReturnValue(); …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace clone_test_01
{
public partial class MainForm : Form
{
public class Book
{
public string title = "";
public Book(string title)
{
this.title = title;
}
}
public MainForm()
{
InitializeComponent();
List<Book> books_1 = new List<Book>();
books_1.Add( new Book("One") );
books_1.Add( new Book("Two") );
books_1.Add( new Book("Three") );
books_1.Add( new Book("Four") );
List<Book> books_2 = new List<Book>(books_1);
books_2[0].title = "Five";
books_2[1].title = "Six";
textBox1.Text = books_1[0].title;
textBox2.Text = books_1[1].title;
} …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我是否可以使用Ninject注册已经创建的类实例,以便每次需要注入时它都会使用此实例?
我想你可以称之为单身,但我已经创建了实例.所有文档都指向创建类的新实例.
我有以下(简化)情况:我有两个接口
interface IAmAnInterface
{
void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
和
interface IAmAnInterfaceToo
{
void DoSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)
和一个实现两者的类:
class IAmAnImplementation: IAmAnInterface, IAmAnInterfaceToo
{
public IAmAnImplementation()
{
}
public void DoSomething()
{
}
public void DoSomethingElse()
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在我使用Ninject将同一个类绑定到两个接口.因为我想要使用相同的IAmAnImplementationbeeing 实例,IAmAnInterface而且IAmAnInterfaceToo很明显我需要某种单例.我和ninject.extensions.namedscope一起玩,InScope()但没有成功.我的最后一次尝试是:
Bind<IAmAnImplementation>().ToSelf().InSingletonScope();
Bind<IAmAnInterface>().To<IAmAnImplementation>().InSingletonScope();
Bind<IAmAnInterfaceToo>().To<IAmAnImplementation>().InSingletonScope();
Run Code Online (Sandbox Code Playgroud)
但不幸的是,当我通过kernel.Get<IDependOnBothInterfaces>();它请求我的测试类的实例时实际上使用了不同的实例IAmAnImplementation.
class IDependOnBothInterfaces
{
private IAmAnInterface Dependency1 { get; set; }
private IAmAnInterfaceToo Dependency2 { get; set; }
public IDependOnBothInterfaces(IAmAnInterface i1, …Run Code Online (Sandbox Code Playgroud) c# dependency-injection ninject inversion-of-control ninject-3
我正在阅读" 更好,更快,更轻的Java "(由Bruce Tate和Justin Gehtland撰写)并且熟悉敏捷类型团队的可读性要求,例如Robert Martin在其清晰的编码书中所讨论的内容.在我现在的团队中,我被告知明确不使用+运算符,因为它在运行时创建了额外的(和不必要的)字符串对象.
但是这篇文章,写于04年,讨论了对象分配是关于10个机器指令的.(基本上免费)
它还讨论了GC如何帮助降低此环境中的成本.
什么是使用之间的实际性能的权衡+,StringBuilder还是StringBuffer?(就我而言,它StringBuffer仅限于Java 1.4.2.)
StringBuffer对我来说导致丑陋,不太可读的代码,正如Tate的书中的几个例子所示.而且StringBuffer是线程同步的,这似乎有它自己的成本是大于中使用了"危险" +操作.
思想/意见?
我刚开始使用NHibernate(使用SQLite)在我当前的项目中,我主要使用Query<>,因为我很熟悉在Linq中编写数据库查询.
当我遇到一些更复杂的查询时,我做了一些研究,QueryOver<>并认为它应该受到青睐,Query<>因为"QueryOver语法是NH特定的".而且,似乎没有任何东西Query<>可以做到QueryOver<>无法实现.
所以我开始相应地更换所有用法Query<>.不久之后,我有了第一个"问题",使用起来Query<>似乎更方便.示例(从CustomNumber表中的列中选择最高值BillingDataEntity):
int result = Session.Query<BillingDataEntity>().Select(x => x.CustomNumber).OrderByDescending(a => a).FirstOrDefault();
int result = Session.QueryOver<BillingDataEntity>().Select(x => x.CustomNumber).OrderBy(a => a.CustomNumber).Desc.Take(1).SingleOrDefault<int>();
Run Code Online (Sandbox Code Playgroud)
我不喜欢的是需要将结果显式地转换为int,并且Query <>版本更容易阅读.我的查询完全错了,换句话说:有更好的方法吗?
我看了一下生成的SQL输出:
NHibernate: select billingdat0_.CustomNumber as col_0_0_ from "BillingDataEntity" billingdat0_ order by billingdat0_.CustomNumber desc limit 1
NHibernate: SELECT this_.CustomNumber as y0_ FROM "BillingDataEntity" this_ ORDER BY this_.CustomNumber desc limit @p0;@p0 = 1 [Type: Int32 (0)]
Run Code Online (Sandbox Code Playgroud)
我究竟在看什么?这是NHibernate进一步转换为实际数据库查询的"内部"(依赖于方法)查询吗?
我想知道这个测试问题.我自己准备了这个例子并进行了测试,但我仍然不确定答案.
具有以下内容:
CREATE TABLE foo (
id INT PRIMARY KEY AUTO_INCREMENT,
name INT
)
CREATE TABLE foo2 (
id INT PRIMARY KEY AUTO_INCREMENT,
foo_id INT REFERENCES foo(id) ON DELETE CASCADE
)
Run Code Online (Sandbox Code Playgroud)
据我所知,答案是:
一个.创建了两个表
虽然也有:
湾 如果删除表foo2中foo_id为2的行,则会自动删除表foo中id = 2的行
d.如果删除表foo中id = 2的行,则删除表foo2中foo_id = 2的所有行
在我的例子中,我会使用删除语法:
DELETE FROM foo WHERE id = 2;
DELETE FROM foo2 WHERE foo_id = 2;
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我无法找到表格之间的任何关系,尽管看起来应该有一个.也许有一些MySQL设置或者可能ON DELETE CASCADE在表创建查询中没有正确使用?我不知道......
我有两个具有完全相同的参考元素的枚举,并想知道为什么Equals不是真的.
作为一个附带问题,下面的代码比较每个元素的工作原理,但必须有一个更优雅的方式
var other = (ActivityService) obj;
if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false;
for (int i = 0; i < AllAccounts.Count(); i++) {
if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) {
return false;
}
}
return true;
Run Code Online (Sandbox Code Playgroud) 我想用[Authorize]我的管理控制器中的每个动作除了Login动作.
[Authorize (Roles = "Administrator")]
public class AdminController : Controller
{
// what can I place here to disable authorize?
public ActionResult Login()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud) c# ×6
mysql ×2
nhibernate ×2
ninject ×2
.net ×1
asp.net-mvc ×1
cascade ×1
clone ×1
deep-copy ×1
equality ×1
ienumerable ×1
java ×1
linq ×1
list ×1
ninject-2 ×1
ninject-3 ×1
performance ×1
readability ×1
transactions ×1
wamp ×1