最近在看nopCommerce项目,其中不明白它的接口的使用,它的代码如下#region Fields private readonly IBlogService _blogService;
private readonly ILanguageService _languageService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly ICustomerContentService _customerContentService;
private readonly ILocalizationService _localizationService;
private readonly IPermissionService _permissionService;
private readonly AdminAreaSettings _adminAreaSettings; #endregion #region Constructors public BlogController(IBlogService blogService, ILanguageService languageService,
IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
ILocalizationService localizationService, IPermissionService permissionService,
AdminAreaSettings adminAreaSettings)
{
this._blogService = blogService;
this._languageService = languageService;
this._dateTimeHelper = dateTimeHelper;
this._customerContentService = customerContentService;
this._localizationService = localizationService;
this._permissionService = permissionService;
this._adminAreaSettings = adminAreaSettings;
} #endregion
拿接口 IBlogService 来说 一般我是这么使用的 IBlogService _blogService = new BlogService();
请问我怎么用它这种方式使用
程序用MVC3写的 下面贴出部分代码
Controllers 部分namespace Nop.Admin.Controllers
{
[AdminAuthorize]
public partial class BlogController : BaseNopController
{
#region Fields private readonly IBlogService _blogService;
private readonly ILanguageService _languageService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly ICustomerContentService _customerContentService;
private readonly ILocalizationService _localizationService;
private readonly IPermissionService _permissionService;
private readonly AdminAreaSettings _adminAreaSettings; #endregion #region Constructors public BlogController(IBlogService blogService, ILanguageService languageService,
IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
ILocalizationService localizationService, IPermissionService permissionService,
AdminAreaSettings adminAreaSettings)
{
this._blogService = blogService;
this._languageService = languageService;
this._dateTimeHelper = dateTimeHelper;
this._customerContentService = customerContentService;
this._localizationService = localizationService;
this._permissionService = permissionService;
this._adminAreaSettings = adminAreaSettings;
} #endregion
#region Blog posts public ActionResult Index()
{
return RedirectToAction("List");
}
IBlogService 部分namespace Nop.Services.Blogs
{
/// <summary>
/// Blog service interface
/// </summary>
public partial interface IBlogService
{
/// <summary>
/// Deletes a blog post
/// </summary>
/// <param name="blogPost">Blog post</param>
void DeleteBlogPost(BlogPost blogPost); /// <summary>
/// Gets a blog post
/// </summary>
/// <param name="blogPostId">Blog post identifier</param>
/// <returns>Blog post</returns>
BlogPost GetBlogPostById(int blogPostId);BlogService 部分namespace Nop.Services.Blogs
{
/// <summary>
/// Blog service
/// </summary>
public partial class BlogService : IBlogService
{
#region Constants
private const string BLOGPOST_BY_ID_KEY = "Nop.blogpost.id-{0}";
private const string BLOGPOST_PATTERN_KEY = "Nop.blogpost.";
#endregion #region Fields private readonly IRepository<BlogPost> _blogPostRepository;
private readonly ICacheManager _cacheManager;
private readonly IEventPublisher _eventPublisher; #endregion #region Ctor public BlogService(IRepository<BlogPost> blogPostRepository,
ICacheManager cacheManager, IEventPublisher eventPublisher)
{
_blogPostRepository = blogPostRepository;
_cacheManager = cacheManager;
_eventPublisher = eventPublisher;
} #endregion #region Methods
private readonly ILanguageService _languageService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly ICustomerContentService _customerContentService;
private readonly ILocalizationService _localizationService;
private readonly IPermissionService _permissionService;
private readonly AdminAreaSettings _adminAreaSettings; #endregion #region Constructors public BlogController(IBlogService blogService, ILanguageService languageService,
IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
ILocalizationService localizationService, IPermissionService permissionService,
AdminAreaSettings adminAreaSettings)
{
this._blogService = blogService;
this._languageService = languageService;
this._dateTimeHelper = dateTimeHelper;
this._customerContentService = customerContentService;
this._localizationService = localizationService;
this._permissionService = permissionService;
this._adminAreaSettings = adminAreaSettings;
} #endregion
拿接口 IBlogService 来说 一般我是这么使用的 IBlogService _blogService = new BlogService();
请问我怎么用它这种方式使用
程序用MVC3写的 下面贴出部分代码
Controllers 部分namespace Nop.Admin.Controllers
{
[AdminAuthorize]
public partial class BlogController : BaseNopController
{
#region Fields private readonly IBlogService _blogService;
private readonly ILanguageService _languageService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly ICustomerContentService _customerContentService;
private readonly ILocalizationService _localizationService;
private readonly IPermissionService _permissionService;
private readonly AdminAreaSettings _adminAreaSettings; #endregion #region Constructors public BlogController(IBlogService blogService, ILanguageService languageService,
IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
ILocalizationService localizationService, IPermissionService permissionService,
AdminAreaSettings adminAreaSettings)
{
this._blogService = blogService;
this._languageService = languageService;
this._dateTimeHelper = dateTimeHelper;
this._customerContentService = customerContentService;
this._localizationService = localizationService;
this._permissionService = permissionService;
this._adminAreaSettings = adminAreaSettings;
} #endregion
#region Blog posts public ActionResult Index()
{
return RedirectToAction("List");
}
IBlogService 部分namespace Nop.Services.Blogs
{
/// <summary>
/// Blog service interface
/// </summary>
public partial interface IBlogService
{
/// <summary>
/// Deletes a blog post
/// </summary>
/// <param name="blogPost">Blog post</param>
void DeleteBlogPost(BlogPost blogPost); /// <summary>
/// Gets a blog post
/// </summary>
/// <param name="blogPostId">Blog post identifier</param>
/// <returns>Blog post</returns>
BlogPost GetBlogPostById(int blogPostId);BlogService 部分namespace Nop.Services.Blogs
{
/// <summary>
/// Blog service
/// </summary>
public partial class BlogService : IBlogService
{
#region Constants
private const string BLOGPOST_BY_ID_KEY = "Nop.blogpost.id-{0}";
private const string BLOGPOST_PATTERN_KEY = "Nop.blogpost.";
#endregion #region Fields private readonly IRepository<BlogPost> _blogPostRepository;
private readonly ICacheManager _cacheManager;
private readonly IEventPublisher _eventPublisher; #endregion #region Ctor public BlogService(IRepository<BlogPost> blogPostRepository,
ICacheManager cacheManager, IEventPublisher eventPublisher)
{
_blogPostRepository = blogPostRepository;
_cacheManager = cacheManager;
_eventPublisher = eventPublisher;
} #endregion #region Methods
class MyBlogService : IBlogService
{
public void DeleteBlogPost(BlogPost blogPost)
{
//添加你的代码,根据传入的blogpost对象,访问数据库,删除它
} ...
}
楼主可以搜一下autofac / unity container 来找相关的资料