我正在开发一个Windows服务来做一些定期操作,我可以使用Unity从另一个库中注入我的类吗?
我想在我的服务上使用[Dependency]属性,在Windows服务启动的入口点注册组件.
例:
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
UnityConfig.RegisterComponents();
ServicesToRun = new ServiceBase[]
{
new EventChecker()
};
ServiceBase.Run(ServicesToRun);
}
}
public static class UnityConfig
{
public static void RegisterComponents()
{
UnityContainer container = new UnityContainer();
container.RegisterType<IEventBL, EventBL>();
}
}
public partial class EventChecker : ServiceBase
{
private Logger LOG = LogManager.GetCurrentClassLogger();
[Dependency]
public Lazy<IEventBL> EventBL { get; set; }
protected override void OnStart(string[] args)
{
var events = EventBL.Value.PendingExecution(1);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,EventBL始终为null,因此不能通过统一的[Dependency]来解决.有没有办法使它工作?
谢谢!
解决方案:
写完答案后我发现了一个可能的解决方案,调用构建容器的方法来创建服务类的工作原理: …
我正在开发一个ASP.NET MVC 3应用程序,我首先使用实体框架代码来创建我的应用程序的类,并且我还有一个存储库以便对其执行操作,保持DBContext和DBEntities的清洁定义.
我的疑问是关于视图的渲染以及保存编辑模型的方式.
如果我有这个实体代表我的数据库中存储的用户:
//Entity:
public class User
{
[Key]
public int IdUser { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想显示一个包含FirstName,LastName,Email和NewPassword,ConfirmPasword和CurrentPassword的视图,以便让用户更改他的数据,输入CurrentPassword来确认更改,所以我怀疑是,像ConfirmPasword和CurrentPassword这样的人在我的实体中,所以我需要为此View创建一个新模型,并将我想要的信息从我的新模型复制到我的数据库实体以保存它?喜欢:
public class UpdateUserModel
{
[Required]
[Display(Name = "Name")]
public string FirstName{ get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName{ get; set; } …Run Code Online (Sandbox Code Playgroud) model-view-controller asp.net-mvc design-patterns entity-framework
我正在开发一个MVC 3 Web应用程序,我想创建这样的东西:
/Controller
/Blog
BogController.cs
ViewsController.cs
ArticlesController.cs
/Customers
SalesController.cs
ProductsController.cs
HomeController.cs
/Views
/Blog
Index.aspx
Summary.aspx
/Views
Index.aspx
Admin.aspx
Show.aspx
/Articles
Show.aspx
Admin.aspx
/Customers
/Sales
Index.aspx
Totals.aspx
/Products
Index.aspx
Promotions.aspx
/Home
Index.aspx
Run Code Online (Sandbox Code Playgroud)
但是他们回答这个人的解决方案是针对MVC 2和MVC 3中的MapAreas属性不会退出(或者至少它不会出现在我看来)
那么我可以做些什么来构建像/ Admin/Users/EditUser这样的结构?例如id = 2?
如果我需要创建一个路由规则,你能不能给我写一个如何做的例子.