我正在努力确保我的编码遵循正确的对象处理方式,因此我将这些规则强制执行为错误.但我在使用这段代码时遇到了麻烦
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
class MyClass
{
public String ToXml()
{
var objSerializer =
new DataContractSerializer(GetType());
var objStream = new MemoryStream();
StreamReader objReader;
String strResult;
try
{
// Serialize the object
objSerializer.WriteObject(objStream, this);
// Move to start of stream to read out contents
objStream.Seek(0, SeekOrigin.Begin);
objReader = new StreamReader(objStream);
try
{
// Read Contents into a string
strResult = objReader.ReadToEnd();
}
finally
{
objReader.Dispose();
}
}
finally
{
if (objStream != null)
{
// objStream.Dispose(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将SimpleInjector与WebFormsMvp结合起来.
为了方便DI WebFormsMvp提供了IPresenterFactory界面.
它包含Create提供要解析的演示者类型和视图实例的方法.
我需要注入的视图实例进入了一个构造的主持人.
演示者还具有需要由容器创建的其他依赖项.
这是我到目前为止所得到的,但它并不理想.这个问题
的正确解决方案是什么?
Presenter构造函数:
public FooPresenter(IFooView view, IClientFactory clientFactory) : base(view)
Run Code Online (Sandbox Code Playgroud)
厂:
public class SimpleInjectorPresenterFactory : IPresenterFactory
{
private readonly Container _container;
private IView _currentView;
public SimpleInjectorPresenterFactory()
{
_container = new Container();
Func<Type, bool> isIView =
type => typeof(IView).IsAssignableFrom(type);
_container.ResolveUnregisteredType += (s, e) => {
if …Run Code Online (Sandbox Code Playgroud) 例如,我将"当前用户"存储在Session中.业务层对象由Unity实例化.如何让业务层对象知道"当前用户"?
asp.net-mvc dependency-injection unity-container asp.net-mvc-4
我正在创建一个我的玩具项目的新次要版本.该项目在NuGet上发布,与.NET 4.0及更高版本兼容.我正在介绍的一些新功能需要.NET 4.5(用户应该能够解析,IReadOnlyCollection<T>以及IReadOnlyList<T>.NET 4.5中引入的两个接口),但我需要保持项目与.NET 4.0兼容,因为不是所有的开发人员可以轻松迁移到最新的.NET框架.
所以我面临的问题是如何解决这个"前向兼容性"问题.我想到了两种解决方案,但两者都不是很有吸引力,所以希望任何人都可以在这里给我一些想法或指导.
以下是我提出的两个解决方案:
解决方案1:使用#if编译器指令并根据.NET框架版本构建DLL,并使用NuGet包发布这些版本并在项目站点下载.
这种方法的缺点是当开发人员将他们的Visual Studio项目从.NET 4.0更新到.NET 4.5时,他们不会自动获得.NET 4.5版本(具有.NET 4.5特定功能).这违反了最不惊讶的原则,并且当开发人员在几个月后尝试使用它时,会让开发人员对这个功能无效的原因感到茫然.
解决方案2:使用单个DLL并动态发出类型,当它们存在于当前应用程序域中时实现这两个新接口.这允许将单个DLL发送给用户,并允许在开发人员在其项目中切换.NET框架版本时提供功能.这将使事情'正常工作'.这是我目前正朝着顺便前进的方向.
因为我需要返回一个需要实现接口的类型,所以缺点是必须在运行时使用Reflection.Emit,ModuleBuilder,TypeBuilder等创建该类型.这是非常讨厌的shizzle.但除此之外,由于这种类型必须在一个新的(匿名)程序集中创建,我必须将一些内部类型设为public(它需要继承的类型和它需要实现的接口).使这些内部类型公开污染项目的API,并禁止我对这些类型进行更改.
我相信这些是我的选择,但我可能会遗漏一些明显的东西.所以我的问题是,我错过了一个可能性吗?有没有办法绕过解决方案1的问题,或者更好地使用运行时类型的硬核根发射?
编辑:我正在学习依赖注入.我理解在开发大型系统时它至关重要的基本原因.
似乎依赖注入使用反射来实现其一些目标.反思是一种逆向工程(可能只对我而言).
如果有人解释为什么以及如何使用Reflection以及它是否是可选的,那将对我非常有帮助.
我正在开发一个从网站导入文本数据的C#类.这很好用.我丢失的地方是如何在字符串变量中包含所有文本后在Unity 4.6中显示文本.任何建议表示赞赏.
请考虑以下示例:
public class CommunicationClient : IClient
{
public CommunicationClient(IServerSettings settings) { ... }
// Code
}
public class SettingsManager : ISettingsManager
{
SettingsManager(IDbSettingManager manager)
// Code
public IDictionary<string, string> GetSettings() { ... }
}
Run Code Online (Sandbox Code Playgroud)
问题:在执行注册(使用SimpleInjector)时,我需要提供从实例SetingsManager和填充ServerSettings实例(具体类型IServerSettings)获得的值,但如果我GetInstance<ISettingsManager>在注册之前调用CommunicationClient它,它会给我一个错误,我不能这样做
错误:
容器在第一次调用GetInstance,GetAllInstances和Verify后无法更改.)
一种解决方案可能是ISettingsManager作为依赖注入,CommunicationClient但我真的不想传递它,因为它将提供超过所需的信息.
编辑:集装箱登记
container.Register(typeof(ICommunicationClient), typeof(CommunicationClient));
ISettingsManager settingsManager = container.GetInstance<ISettingsManager>();
string url = settingsManager.GetSetting("url");
string userName = settingsManager.GetSetting("username");
string password = settingsManager.GetSetting("password");
container.Register(typeof(IServerConfiguration), () => …Run Code Online (Sandbox Code Playgroud) 可以手动注册依赖项:
services.AddTransient<IEmailService, EmailService>();
services.AddTransient<ISmsService, SmsService>();
Run Code Online (Sandbox Code Playgroud)
当依赖关系太多时,手动注册所有依赖项变得很困难.
在MVC 6(beta 7)中实现基于约定的绑定的最佳方法是什么?
PS在我以前使用的项目Ninject有ninject.extensions.conventions.但我找不到适用于MVC 6的Ninject适配器.
c# dependency-injection ninject asp.net-core-mvc asp.net-core
我刚刚遇到了垃圾收集者的这种奇怪的"行为" System.Threading.ThreadLocal<T>,我无法解释.在正常情况下,ThreadLocal<T>实例将在超出范围时进行垃圾收集,即使它们没有正确处理,除非它们是循环对象图的一部分.
以下示例演示了此问题:
public class Program
{
public class B { public A A; }
public class A { public ThreadLocal<B> LocalB; }
private static List<WeakReference> references = new List<WeakReference>();
static void Main(string[] args) {
for (var i = 0; i < 1000; i++)
CreateGraph();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
// Expecting to print 0, but it prints 1000
Console.WriteLine(references.Count(c => c.IsAlive));
}
static void CreateGraph() {
var a = new A { LocalB = new ThreadLocal<B>() }; …Run Code Online (Sandbox Code Playgroud) 我有以下代码来使用 NLog 设置数据库日志记录。我使用 NLog.Extension.Logging 的 Alpha 版本。是否可以让内置日志框架记录到数据库,这样我就不需要使用NLog?
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
asp.net-core ×2
.net-4.5 ×1
.net-core ×1
asp.net-mvc ×1
frameworks ×1
memory-leaks ×1
microsoft-extensions-logging ×1
ninject ×1
nlog ×1
nuget ×1
stylecop ×1
text ×1
webformsmvp ×1