我正在尝试找到在使用NHibernate的Web应用程序中处理事务的最佳解决方案.
我们使用IHttpModule,在HttpApplication.BeginRequest中我们打开一个新会话,然后用ManagedWebSessionContext.Bind(context,session)将它绑定到HttpContext; 我们关闭并取消绑定HttpApplication.EndRequest上的会话.
在我们的Repository基类中,我们总是围绕我们的SaveOrUpdate,Delete,Get方法包装一个事务,例如,根据最佳实践:
public virtual void Save(T entity)
{
var session = DependencyManager.Resolve<ISession>();
using (var transaction = session.BeginTransaction())
{
session.SaveOrUpdate(entity);
transaction.Commit();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您需要在某个应用程序服务中的某个位置放置一个事务来包含多个存储库调用以保存,删除等,则这不起作用.
所以我们尝试使用TransactionScope(我不想编写自己的事务管理器).为了测试这是否有效,我使用外部TransactionScope,它不会调用.Complete()来强制回滚:
存储库保存():
public virtual void Save(T entity)
{
using (TransactionScope scope = new TransactionScope())
{
var session = DependencyManager.Resolve<ISession>();
session.SaveOrUpdate(entity);
scope.Complete();
}
}
Run Code Online (Sandbox Code Playgroud)
使用存储库的块:
TestEntity testEntity = new TestEntity { Text = "Test1" };
ITestRepository testRepository = DependencyManager.Resolve<ITestRepository>();
testRepository.Save(testEntity);
using (var scope = new TransactionScope())
{
TestEntity entityToChange = testRepository.GetById(testEntity.Id);
entityToChange.Text = "TestChanged"; …Run Code Online (Sandbox Code Playgroud) 我即将开始一个项目,我可能会使用大量的C++/CLI.我真的很想念VS 2010中的C++/CLI Intellisense.我听说过一些类似Resharper的C++产品,但不知道它们是否提供完整的Intellisense.他们也使用C++/CLI吗?
人们在做什么来克服这个限制?
我目前正在研究malloc()Windows下的实现.但在我的研究中,我偶然发现了困扰我的事情:
首先,我知道在API级别,windows主要使用HeapAlloc()和VirtualAlloc()调用来分配内存.我从这里开始收集微软的实现malloc()(包含在CRT中 - C运行时)基本上要求HeapAlloc()块> 480字节,否则管理VirtualAlloc()为小分配分配的特殊区域,以防止碎片.
那一切都很好,很好.但是还有其他一些实现malloc(),例如nedmalloc,它声称比微软快了125%malloc.
所有这些让我想到了一些事情:
为什么我们不能只召唤HeapAlloc()小块?在碎片方面表现不佳(例如通过"先适合"而不是"最适合")?
是什么nedmalloc比微软更快malloc?
从上面的内容来看,我的印象是HeapAlloc()/ VirtualAlloc()速度太慢,以至于malloc()只是偶尔调用一次然后管理分配的内存本身要快得多.这个假设是真的吗?或者是malloc()因为碎片而需要的"包装"?有人会认为像这样的系统调用会很快 - 或者至少会有一些想法被放入其中以使它们有效.
平均而言,典型malloc调用执行了多少(一个数量级)的内存读/写(可能是已分配段数的函数)?我会直观地说它是一个普通程序的十位,我是对的吗?
windows malloc memory-management dynamic-memory-allocation nedmalloc
我一直想知道为什么我们必须始终定义的原因D3DVERTEX.是因为微软希望有机会把它放在一个类和重载运算符中,还是有其他原因?
谢谢
我写了一个Int128类型,效果很好。我以为我可以通过一个简单的想法来改进它的性能:改进一些笨拙的换档操作。
因为它们在乘法和除法中大量使用,所以改进会产生连锁反应。因此,我开始创建一个动态方法(低调并高调旋转),只是发现没有OpCodes.Rol或OpCodes.Ror指令。
IL有可能吗?
datetime的类型模块中的等效类型是什么?例:
import datetime
import types
t=datetime.datetime.now()
if type(t)==types.xxxxxx:
do sth
Run Code Online (Sandbox Code Playgroud)
我没有在datetime类型中找到类型模块的相关类型; 任何人都可以帮助我吗?
我遇到了一个意想不到的结果,在往返Int32.MaxValue成System.Single:
Int32 i = Int32.MaxValue;
Single s = i;
Int32 c = (Int32)s;
Debug.WriteLine(i); // 2147483647
Debug.WriteLine(c); // -2147483648
Run Code Online (Sandbox Code Playgroud)
我意识到它必须溢出,因为有效Single位中没有足够的位来保存Int32值,并且它会向上舍入.当我在IL中更改为conv.r4to conv.r4.ovf时,OverflowExcpetion会抛出一个.很公平...
但是,当我正在研究这个问题时,我在java中编译了这段代码并运行它并得到以下内容:
int i = Integer.MAX_VALUE;
float s = (float)i;
int c = (int)s;
System.out.println(i); // 2147483647
System.out.println(c); // 2147483647
Run Code Online (Sandbox Code Playgroud)
我对JVM了解不多,但我想知道它是如何做到的.这似乎不那么令人惊讶,但是在四舍五入到2.14748365E9后它如何保留额外的数字?是否保留了某种内部表示,然后在重新投入时替换它int?或者它只是向下舍入Integer.MAX_VALUE以避免溢出?
我们有一个包含许多不同库的大型解决方案.我们刚刚迁移到64位系统上的开发,这让我们考虑了我们应该为解决方案中的每个项目指定的平台目标.
目前,我们只有一个引用32位DLL文件的库项目.我们引用了此DLL文件的Interop,但是当将x86作为此项目的平台时,我们收到错误,同时将解决方案中的其余项目保留为"Any CPU".
我的问题是,除了引用32位互操作的项目之外,我们可以在所有项目上定位任何CPU吗?或者应该只为x86构建一切,因为这似乎没有任何错误.
我有一个复合组件,它具有多对一参考.
class MyComposite
{
SomeEntity ManyToOne { get; set; }
SomeOtherUserType Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
为了便于映射,我制作了一个ICompositeUserType包含此组件的自定义:
class MyCompositeUserType : ICompositeUserType
{
// ...
private static readonly IType[] _propertyTypes = new[]
{
new ManyToOneType("SomeEntity"),
new CustomType(typeof(SomeOtherUserType))
};
// ...
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个composite-element包含此组件的s 集合:
<class name="Container">
...
<set name="Pairings"
cascade="all-delete-orphan"
generic="true"
lazy="false"
table="Pairings"
fetch="join">
<key column="ContainerId" />
<composite-element class="Pair">
<property name="Item1" type="mycomposite" lazy="false">
<column name="Entity1Id" />
<column name="Amount1" />
</property>
<property name="Item2" type="mycomposite" lazy="false">
<column name="Entity2Id" /> …Run Code Online (Sandbox Code Playgroud)