我正在使用基于洋葱架构this实施的架构。我决定挑战自己用NLog更改NHibernates默认记录器(log4net)。我理解如何配置NLog,但是我在LogerFactory类中解析我对记录器的依赖性有困难。目前,我的NHibernate项目只知道我的日志记录界面。如何让我的NHibernate项目解决依赖?
这是我现在的程序结构: Console App初始化ninject并设置我的模块,包括我的LoggingModule和我的RepositoryModule。我第一次需要来自我的存储库的东西,我的SessionHelper类构建我的会话并配置NHibernate。这是NHibernate调用我的LoggerFactory的地方(LoggerFactory实现了NHibernates ILoggerFactory)。 NHibernate调用LoggerFor(Type类型); type作为NHibernate.Cfg.Configuration传入。从这里开始,我需要通过我的ILoggingService获取NLog的实例,这是我感到困惑的地方。我如何解决此依赖关系?我的控制台应用程序知道解决方案,因为这是我构建内核并加载模块的地方。我在这里设置另一个内核吗?解决这个问题的最佳方法是什么?
编辑:这是我目前的代码:
Program.cs的
namespace ZeroBase.ManualConsole
{
class Program
{
static void Main(string[] args)
{
IKernel kernel = CreateKernel();
CoreService service = new CoreService(
kernel.Get<ZeroBase.Domain.Interfaces.IUserRepository>(),
kernel.Get<ZeroBase.Domain.Interfaces.ICommentRepository>());
//.........
}
static IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
static void RegisterServices(IKernel kernel)
{
// Bind Core Service
kernel.Bind<ICoreService>().To<CoreService>();
// Add data and infrastructure modules
var modules = new List<INinjectModule>
{
new ConfigModule(),
new LoggingModule(),
new RepositoryModule()
};
kernel.Load(modules);
}
}
}
LoggingModule.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject.Modules;
using ZeroBase.Infrastructure.Interfaces;
using NLog.Config;
using NLog;
using ZeroBase.Infrastructure.Logging;
namespace ZeroBase.Infrastructure.DependencyResolution
{
public class LoggingModule : NinjectModule
{
public override void Load()
{
ILoggingService logger = GetLoggingService();
Bind<ILoggingService>().ToConstant(logger);
}
private ILoggingService GetLoggingService()
{
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("utc_date", typeof(UtcDateRenderer));
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("web_variables", typeof(WebVariablesRenderer));
ILoggingService logger = (ILoggingService)LogManager.GetLogger("NLogLogger", typeof(LoggingService));
return logger;
}
}
}
RepositoryModule.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
using Ninject.Modules;
using ZeroBase.Infrastructure.Interfaces;
using ZeroBase.Domain.Interfaces;
using ZeroBase.Infrastructure.Data;
namespace ZeroBase.Infrastructure.DependencyResolution
{
public class RepositoryModule : NinjectModule
{
public override void Load()
{
SetupLogging();
BindRepositories();
}
private void SetupLogging()
{
// Get logging service
var loggingService = Kernel.Get<ILoggingService>();
}
private void BindRepositories()
{
// Get config service
var configService = Kernel.Get<IConfigService>();
// Bind repositories
Bind<IUserRepository>().To<UserRepository>()
.WithConstructorArgument("connectionString", configService.ZeroBaseConnection);
Bind<ICommentRepository>().To<CommentRepository>()
.WithConstructorArgument("connectionString", configService.ZeroBaseConnection);
}
}
}
RepositoryBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZeroBase.Domain.Interfaces;
namespace ZeroBase.Infrastructure.Data
{
public class RepositoryBase<T> : IRepositoryBase<T>
{
protected SessionHelper _nhibernate;
protected string _connectionString;
public RepositoryBase(string connectionString)
{
_connectionString = connectionString;
_nhibernate = new SessionHelper(_connectionString);
}
//....
}
}
SessionHelper.cs 构建NHibernate会话并进行配置。请注意,正在注入connectionString。我可以在这里轻松注入日志服务。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using ZeroBase.Infrastructure.Data;
using FluentNHibernate.Automapping;
using ZeroBase.Domain.Entities;
using ZeroBase.Infrastructure.Interfaces;
namespace ZeroBase.Infrastructure.Data
{
public class SessionHelper
{
private ISessionFactory _sessionFactory;
private string _connectionString;
public SessionHelper(string connectionString)
{
_connectionString = connectionString;
}
private ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
// Set up database connection
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString(x => x.Is(_connectionString))
//.ShowSql()
)
// Use class mappings
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<UserMap>()
.AddFromAssemblyOf<ZeroBase.Infrastructure.Data.RACS3.UserMap>())
.ExposeConfiguration(c => {
c.SetInterceptor(new AuditInterceptor());
}
)
.BuildSessionFactory();
}
public ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
LoggerFactory.cs 通过app.config配置NHibernate用于其日志记录。这是我遇到依赖问题的地方。这与NHibernate一起生活。我可以在这里轻松引用NLog并使用NHibernate连接它,但我不希望NHibernate关注我正在使用的日志框架。这就是为什么我只想处理我的界面并让Ninject注入我的具体实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using ZeroBase.Infrastructure.Interfaces;
namespace ZeroBase.Infrastructure.Data
{
public class LoggerFactory : ILoggerFactory
{
public IInternalLogger LoggerFor(Type type)
{
//ILoggingService logService;
// Get resolved LoggingService
//return new NLogLogger(LogManager.GetLogger(type.FullName));
//return new Logger(logService.GetLogger(type.FullName));
return null;
}
public IInternalLogger LoggerFor(string keyName)
{
//ILoggingService logService;
// Get resolved LoggingService
//return new NLogLogger(LogManager.GetLogger(keyName));
//return new Logger(logService.GetLogger(keyName));
return null;
}
}
public class Logger : IInternalLogger
{
private readonly ILoggingService logger;
public Logger(ILoggingService _logger)
{
logger = _logger;
}
//....
}
}
答案 0 :(得分:1)
通过Common.Logging与NHibernate一起使用NLog:
http://nhibernate.info/doc/howto/various/using-nlog-via-common-logging-with-nhibernate.html
答案 1 :(得分:1)
为什么不能在LoggerFactory类中添加静态Kernal属性,并在初始化Kernal后立即在Program.Main中设置它?
然后在你的LoggerFor实现中,你只需要引用你的静态内核属性来获得正确绑定的实现。