我想将值传递给DbContext的ctor,然后让该值对相关的DbSets强制执行"过滤".这可能......还是有更好的方法?
代码可能如下所示:
class Contact {
int ContactId { get; set; }
int CompanyId { get; set; }
string Name { get; set; }
}
class ContactContext : DbContext {
public ContactContext(int companyId) {...}
public DbSet<Contact> Contacts { get; set; }
}
using (var cc = new ContactContext(123)) {
// Would only return contacts where CompanyId = 123
var all = (from i in cc.Contacts select i);
// Would automatically set the CompanyId to 123
var contact = new Contact { …Run Code Online (Sandbox Code Playgroud) 我正在使用Simple Injector来管理我注入的依赖项的生命周期(在这种情况下UnitOfWork),我很高兴有一个单独的装饰器而不是我的服务或命令处理程序,在编写业务逻辑时保存和处理使代码更容易图层(我遵循本博文中概述的架构).
通过在构造根容器的构造过程中使用Simple Injector MVC NuGet包和以下代码,上面的工作完美(并且非常容易),如果图中存在多个依赖项,则相同实例将全部注入 - 完美的实体框架模型上下文.
private static void InitializeContainer(Container container)
{
container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
// register all other interfaces with:
// container.Register<Interface, Implementation>();
}
Run Code Online (Sandbox Code Playgroud)
我现在需要运行一些后台线程并从Simple Injector 文档中了解可以代理命令的线程,如下所示:
public sealed class TransactionCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> handlerToCall;
private readonly IUnitOfWork unitOfWork;
public TransactionCommandHandlerDecorator(
IUnitOfWork unitOfWork,
ICommandHandler<TCommand> decorated)
{
this.handlerToCall = decorated;
this.unitOfWork = unitOfWork;
}
public void Handle(TCommand command)
{
this.handlerToCall.Handle(command);
unitOfWork.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
ThreadedCommandHandlerProxy:
public class ThreadedCommandHandlerProxy<TCommand>
: ICommandHandler<TCommand> …Run Code Online (Sandbox Code Playgroud) 我希望我的应用程序写出名为MachineName_UserName_yyyymmdd_hhmmss.txt的不同跟踪文件,其中用户名是当前登录的用户,时间是应用程序的开始时间..Net侦听器TextWriterTraceListener似乎只支持配置文件中指定的硬编码文件名.有没有办法在不编写自定义跟踪侦听器的情况下执行此操作.
假设我必须编写一个自定义跟踪侦听器,我已经实现了一个这样的tracelistener:
Imports System.Diagnostics
Public Class MyCustomTraceListener
Inherits TextWriterTraceListener
Public Sub New()
'Need to do it this way as the Base constructor call has to be the first statement
MyBase.New(String.Format("AppNameTraceFile_{0}_{1}_{2}{3}{4}-{5}{6}{7}.txt", _
Environment.MachineName, _
Environment.UserName, _
DateTime.Now.ToString("yyyy"), _
DateTime.Now.ToString("MM"), _
DateTime.Now.ToString("dd"), _
DateTime.Now.ToString("HH"), _
DateTime.Now.ToString("mm"), _
DateTime.Now.ToString("ss")))
Me.IndentSize = 4
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
在配置文件中,我已经像这样配置了跟踪源:
<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="MyTraceSource"
switchName="mySwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<clear/>
<add name="MyTraceListener"
type="MyNameSpace.MyCustomTraceListener"
traceOutputOptions="ProcessId, DateTime, Callstack" />
</listeners>
</source>
</sources>
<switches>
<add name="mySwitch" value="Warning" /> …Run Code Online (Sandbox Code Playgroud) 我目前正在使用C#开发一个独立的应用程序,但遇到的问题是我找不到解决方案。确实,我的脚本使用了两个库(由外部公司提供),但是,一个库是为64位系统制作的,另一个库是为32位系统制作的。所以这是我的问题,当我尝试同时使用两个库时,总是会出现错误:
System.BadImageFormatException
Run Code Online (Sandbox Code Playgroud)
我已经尝试为“任何CPU”,“ x64”和“ x86”构建项目,但是我始终遇到相同的问题。
因此,我想知道是否有可能解决此问题,以便能够在同一脚本中使用两个库,或者是否有另一种技术来实现相同的结果?
预先感谢您,克莱蒙