我最近被要求协助其他团队建立ASP .NET网站.他们已经编写了大量的代码 - 我被特别要求为该网站构建一些单独的页面.
在探索网站其余部分的代码时,正在构建的DataTables的数量向我跳出来.作为一个相对较新的领域,我从来没有像使用这个网站那样使用数据库的应用程序,所以我不确定这是多么常见.似乎每当从我们的数据库查询数据时,结果都存储在DataTable中.然后,此DataTable通常自行传递,或者传递给构造函数.使用DataTable初始化的类始终将DataTable 分配给私有/受保护字段,但是这些类中只有少数实现IDisposable.实际上,到目前为止我浏览过的数千行代码中,我还没有看到在DataTable上调用Dispose方法.
如果有的话,这似乎不是好的OOP.这是我应该担心的吗?或者我只是比我更注重细节?假设您是最有经验的开发人员,如果刚刚被指派帮助您访问您网站的人向您询问此"问题",您会有什么感受或反应?
我正在使用FluentValidation库对我的某个模型强制执行唯一约束:
public class Foo {
// No two Foos can have the same value for Bar
public int Bar { get; set; }
}
public class FooValidator : AbstractValidator<Foo> {
public FooValidator(ApplicationDbContext context) {
this.context = context;
RuleFor(m => m.Bar)
.Must(BeUnique).WithMessage("Bar must be unique!");
}
private readonly ApplicationDbContext context;
public bool BeUnique(int bar) {
return !context.Foos.Any(foo => foo.Bar == bar);
}
}
Run Code Online (Sandbox Code Playgroud)
该ApplicationDbContext值是使用StructureMap注入.为了确保在每个请求结束时处理上下文,我试图调用我的应用程序ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects()的EndRequest处理程序.
不幸的是,似乎Application_EndRequest在我的验证器类能够完成其工作之前调用该方法,并且在FooValidator.BeUnique执行时间时处理上下文.
有没有更好的方法来使用FluentValidation库执行数据库相关的验证,或者是将此逻辑移动到其他位置的唯一解决方案(控制器操作,数据库本身,或者其他地方)?
structuremap entity-framework dependency-injection fluentvalidation asp.net-mvc-3
我正在开发一个从第三方网站检索信息的类库.如果在设定的时间段(~0.5秒)内发出太多请求,则正在访问的网站将停止响应.
我的库的公共方法直接与Web服务器上的文件资源相关.换句话说,每次调用方法时,HttpWebRequest都会创建一个并将其发送到服务器.如果一切顺利,则会将XML文件返回给调用者.但是,如果这是小于0.5秒的第二个Web请求,则请求将超时.
我的困境在于我应该如何处理请求限制(如果有的话).显然,我不希望呼叫者坐在那里等待响应 - 特别是如果我完全确定他们的请求会超时.
我的库是否更有意义对我创建的Web请求进行排队和限制,或者如果客户端在API调用之间没有等待足够长的时间,我的库是否应该抛出异常?
我的同事和我正在调试他正在处理的WCF服务中的问题,其中字符串的长度未被正确评估.他正在运行此方法来对其WCF服务中的方法进行单元测试:
// Unit test method
public void RemoveAppGroupTest()
{
string addGroup = "TestGroup";
string status = string.Empty;
string message = string.Empty;
appActiveDirectoryServicesClient.RemoveAppGroup("AOD", addGroup, ref status, ref message);
}
// Inside the WCF service
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public void RemoveAppGroup(string AppName, string GroupName, ref string Status, ref string Message)
{
string accessOnDemandDomain = "MyDomain";
RemoveAppGroupFromDomain(AppName, accessOnDemandDomain, GroupName, ref Status, ref Message);
}
public AppActiveDirectoryDomain(string AppName, string DomainName)
{
if (string.IsNullOrEmpty(AppName))
{
throw new ArgumentNullException("AppName", "You must specify an application name");
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Redis pubsub通道将工作进程池中的消息发送到我的ASP.NET应用程序.收到消息后,我的应用程序将消息转发到带有SignalR的客户端浏览器.
我找到了这个解决方案来维护与Redis的开放连接,但它在重新创建连接时没有考虑订阅.
我正在处理Global.asax文件中的Redis pubsub消息:
public class Application : HttpApplication
{
protected void Application_Start()
{
var gateway = Resolve<RedisConnectionGateway>();
var connection = gateway.GetConnection();
var channel = connection.GetOpenSubscriberChannel();
channel.PatternSubscribe("workers:job-done:*", OnExecutionCompleted);
}
/// <summary>
/// Handle messages received from workers through Redis.</summary>
private static void OnExecutionCompleted(string key, byte[] message)
{
/* forwarded the response to the client that requested it */
}
}
Run Code Online (Sandbox Code Playgroud)
当前RedisConnection因任何原因关闭时会发生此问题.最简单的解决方案是RedisConnectionGateway在重置连接时从类中触发事件,并使用新的重新订阅RedisSubscriberChannel.但是,在重置连接时发布到通道的任何消息都将丢失.
是否有任何推荐的方法来处理这种情况?
我最近被团队负责人(不是我的团队)问我是否愿意承担编程项目.他的团队成员目前已经预先处理了其他更重要的项目.我两年前大学毕业,到目前为止,编程只是我的一个爱好.最近我决定想从事软件开发事业.我接受了他的提议,以便我可以获得一些真实的经验,并开始建立一个投资组合.
大约一个小时后,我计划与团队负责人会面,讨论他需要的细节.通过与他的简短电子邮件交流,我知道基础项目是更新现有的ASP.NET表单 - 但我认为还有更多内容.
考虑到我最终想把这个项目放在一个投资组合中,我应该在会议上采取什么样的笔记?
我正在尝试编写WSH登录脚本.整个公司的管理员需要能够自定义脚本的执行,并为特定位置和用户执行其他脚本.为了使他们的工作更轻松,我想提供一个管理员可以在他们的脚本中访问的API.如果我使用JScript编写API,是否可以初始化我通过VBScript定义的对象?例如,请考虑以下代码:
<!-- The WSF logon script file -->
<package>
<job>
<script language="JScript">
// A demonstration function
function OverNineThousand() {
return 9001;
}
// A demonstration "class"
function WorkstationClass() {
var os = "Windows XP";
this.getOperatingSystem = function() {
return os;
}
}
</script>
<script language="VBScript">
Dim bigNumber, workstation
'// This assignment works properly.
bigNumber = OverNineThousand()
'// This assignment causes an error. Am I doing it wrong?
Set workstation = New WorkstationClass()
'// Execution never gets this far
WScript.Echo workstation.getOperatingSystem() …Run Code Online (Sandbox Code Playgroud) 在过去一周左右的时间里,我一直在阅读很多关于存储库模式的文章和教程.许多文章将存储库模式与工作模式单元紧密联系在一起.在这些文章中,我通常会找到与此类似的代码:
interface IUnitOfWork<TEntity>
{
void RegisterNew(TEntity entity);
void RegisterDirty(TEntity entity);
void RegisterDeleted(TEntity entity);
void Commit();
void Rollback();
}
interface IRepository<TKey, TEntity>
{
TEntity FindById(TKey id);
IEnumerable<TEntity> FindAll();
void Add(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
}
class Repository : IRepository<int, string>
{
public Repository(IUnitOfWork<string> context)
{
this.context = context;
}
private IUnitOfWork<string> context;
public void Add(string entity)
{
context.RegisterNew(entity);
}
public void Update(string entity)
{
context.RegisterDirty(entity);
}
public void Delete(string entity)
{
context.RegisterDeleted(entity);
}
/* Entity retrieval methods */ …Run Code Online (Sandbox Code Playgroud) 我正在开发一个MVC应用程序,它从SQL Server中的表中检索数据,结构如下:
+-----------------------------------+
| Id | Name | Hierarchy | Depth |
|-----------------------------------|
| 01 | Justin | / | 0 |
| 02 | Chris | /1 | 1 |
| 03 | Beth | /1/1 | 2 |
+-----------------------------------+
Run Code Online (Sandbox Code Playgroud)
Hierarchy列中的示例数据是hierarchyid数据类型的字符串表示形式,并Depth使用该hierarchyid::GetLevel()方法计算列.
使用Entity Framework 4.1,我已将上表映射到此类:
public class Node {
public int Id { get; set; }
public string Name { get; set; }
public string HierarchyPath { get; set; } // String representation …Run Code Online (Sandbox Code Playgroud) 我正在研究一个粉碎XML文档的存储过程.正在处理的记录中的一个子元素有时可以使用该xsi:nil="true"属性进行标记.其他时候,它可以包含一个dateTime.我正在尝试将一个字符串插入到我的表的列中,这取决于该元素是否具有值.例如:
[Status] = CASE WHEN (Rt.Item.value('(./Date)[1]', 'nvarchar(max)') = '') THEN N'SUBMITTED' ELSE N'PROCESSED' END
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不起作用.检查元素在SQL Server中是否有值的正确方法是什么?
我正在编写一个类库,它抽象出网站上XML文件中包含的数据.每个XML文件使用相同的根元素:page.page依赖于我正在下载的特定文件的后代.例如:
<!-- http://.../groups.xml -->
<page url="/groups.xml">
<groups>
<group id="1" >
<members>
<member name="Adrian" />
<member name="Sophie" />
<member name="Roger" />
</members>
</group>
</groups>
</page>
<!-- http://.../project.xml?n=World%20Domination -->
<page url="/project.xml">
<projectInfo>
<summary classified="true" deadline="soon" />
<team>
<member name="Pat" />
<member name="George" />
</team>
</projectInfo>
</page>
Run Code Online (Sandbox Code Playgroud)
最后还有一些我想要下载和处理的XML文件.出于这个原因,我一直试图想出一个很好的,干净的方法来反序列化数据.我尝试了一些方法,但是当我回顾我的代码时,每种方法都让我觉得有点脏.我最新的化身使用以下方法:
internal class Provider
{
/// <summary>Download information from the host.</summary>
/// <typeparam name="T">The type of data being downloaded.</typeparam>
internal T Download<T>(string url) where T : IXmlSerializable, new()
{
try
{
var …Run Code Online (Sandbox Code Playgroud) c# ×5
asp.net ×2
asp.net-mvc ×2
xml ×2
booksleeve ×1
javascript ×1
json ×1
linq ×1
redis ×1
repository ×1
sql-server ×1
string ×1
structuremap ×1
t-sql ×1
throttling ×1
unit-of-work ×1
vbscript ×1
wcf ×1
wsh ×1
xpath ×1
xquery ×1