如何添加相关的设置像 commonsetting 一样,只需要定义一个实现空接口 ISettings 的类,然后的数据表中添加相关的配置,数据库中 Name 值对应:类名 + . + 属性名,和接口注入方式一样,不过这里使用类注入。另外需要注意的是在 Nop.Core 项目 ComponentModel 目录下的 GenericDictionaryTypeConverter 和 GenericListTypeConverter 类中重写了泛型 List 和泛型 Dictionary 的 CanConvertFrom 和 ConvertFrom 方法,使得数据库中存储字符串能转换成对应的 List 和 Dictionary,如 List 存储时我们只需要将多个数据使用英文符号 (,) 分隔符即可。
- public class SettingsSource : IRegistrationSource
- {
- static readonlyMethodInfo BuildMethod =typeof(SettingsSource).GetMethod(
- "BuildRegistration",
- BindingFlags.Static | BindingFlags.NonPublic);
- publicIEnumerable RegistrationsFor(
- Service service,
- Func> registrations)
- {
- varts = serviceas TypedService;
- if(ts !=null&&typeof(ISettings).IsAssignableFrom(ts.ServiceType))
- {
- varbuildMethod = BuildMethod.MakeGenericMethod(ts.ServiceType);
- yield return(IComponentRegistration)buildMethod.Invoke(null,null);
- }
- }
- staticIComponentRegistration BuildRegistration() whereTSettings : ISettings,new()
- {
- return RegistrationBuilder
- .ForDelegate((c, p) =>
- {
- ////var currentStoreId = c.Resolve<IStoreContext>().CurrentStore.Id;
- //uncomment the code below if you want load settings per store only when you have two stores installed.
- //var currentStoreId = c.Resolve<IStoreService>().GetAllStores().Count > 1
- // c.Resolve<IStoreContext>().CurrentStore.Id : 0;
- //although it's better to connect to your database and execute the following SQL:
- //DELETE FROM [Setting] WHERE [StoreId] > 0
- returnc.Resolve().LoadSetting();
- })
- .InstancePerLifetimeScope()
- .CreateRegistration();
- }
- public boolIsAdapterForIndividualComponents {get{return false; } }
- }
View Code
- public class CommonSettings: ISettings {
- public CommonSettings() {
- IgnoreLogWordlist = new List < string > ();
- }
- /// <summary>
- /// Gets or sets a value indicating whether stored procedures are enabled (should be used if possible)
- /// </summary>
- public bool UseStoredProceduresIfSupported {
- get;
- set;
- }
- /// <summary>
- /// Gets or sets a value indicating whether to use stored procedure (if supported) for loading categories (it's much faster in admin area with a large number of categories than the LINQ implementation)
- /// </summary>
- public bool UseStoredProcedureForLoadingCategories {
- get;
- set;
- }
- /// <summary>
- /// Gets or sets a value indicating whether 404 errors (page or file not found) should be logged
- /// </summary>
- public bool Log404Errors {
- get;
- set;
- }
- /// <summary>
- /// Gets or sets ignore words (phrases) to be ignored when logging errors/messages
- /// </summary>
- public List < string > IgnoreLogWordlist {
- get;
- set;
- }
- }
数据库配置(IDataProvider)之前使用 web.config 配置的 connectionStrings,现在改为 IDataProvider 的实现来统一配置相关的参数,特别提示数据库配置文件在 App_Data 目录下的 Settings.txt,如果没有新建一个,系统运行后会根据 EntityFramework 的数据映射自动创建相关表。
- public class HomeController : Controller
- {
- public ILogger _logger;
- public IUserActivityService _userActivityService;
- public CommonSettings _commonSettings;
- public HomeController(
- ILogger logger,
- IUserActivityService userActivityService,
- CommonSettings commonSetting)
- {
- _logger = logger;
- _userActivityService = userActivityService;
- _commonSettings = commonSettings;
- }
- public ActionResult Index()
- {
- TestSettings();
- TestLogger();
- return View();
- }
- private void TestSettings()
- {
- vars = _commonSettings.IgnoreLogWordlist;
- }
- private void TestLogger()
- {
- _logger.InsertLog(LogLevel.Information, "index visit");
- _userActivityService.InsertActivity(ActivityLogTypeEnum.AddUser, "添加用户{0},{1}",new string[2] {"aaaa","bbb" });
- }
- }
- DataProvider: sqlserver DataConnectionString: Data Source = .;
- Initial Catalog = nopFramework;
- Integrated Security = False;
- Persist Security Info = False;
- User ID = sa;
- Password = sa1234
ILogger 接口,日志接口。 NullLogger 类,Logger 接口空实现。DefaultLogger 类,Nop 默认的日志接口实现。 LoggingExtensions 类,日志扩展,使用日志的扩展方法来存储不同级别的日志记录。IUserActivityService 接口,用户操作服务接口。UserActivityService 类,用户操作服务实现。Nop 没有使用类似于 Log4net 的开源日志记录组件,当然如果你想使用可以通过实现接口来引入。IUserActivityService 用于记录用户操作的记录,需要在 ActivityLogType 表配置好用户操作的类型,这里优化了一下,将类型通过枚举列出来,这样在记录用户操作的时候不至于输错参数。在首页的控制器中加入测试代码,日志和操作记录都添加到了数据库中。
至此,开发框架中添加了常用的缓存、网站设置、系统日志、用户操作日志功能。
- private void TestLogger() {
- _logger.InsertLog(LogLevel.Information, "index visit");
- _userActivityService.InsertActivity(ActivityLogTypeEnum.AddUser, "添加用户{0},{1}", new string[2] {
- "aaaa",
- "bbb"
- });
- }
来源: http://www.cnblogs.com/dreling/p/6958829.html