我正在使用Microsoft Unity.我有一个接口ICustomerService及其实现CustomerService.我可以使用以下代码为Unity容器注册它们:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
如果CustomerService在其构造函数中有某个参数(例如ISomeService1),我使用以下代码(我需要指定SomeService1):
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));
Run Code Online (Sandbox Code Playgroud)
这里没问题.
当CustomerService类在其构造函数中有两个参数(不是前一个示例中的一个参数)时会出现问题(例如ISomeService1和ISomeService2).当我使用以下代码时它工作正常:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1(), new SomeService2()));
问题是我不想指定SomeService2()第二个参数.我只想指定第一个参数 - SomeService1().但我得到的错误是我需要指定一个或两个参数.
如何只指定构造函数的第一个参数?
我有一个界面.
public interface ISomeInterface {...}
Run Code Online (Sandbox Code Playgroud)
和两个实现(SomeImpl1和SomeImpl2):
public class SomeImpl1 : ISomeInterface {...}
public class SomeImpl2 : ISomeInterface {...}
Run Code Online (Sandbox Code Playgroud)
我也有两个服务,我注入ISomeInterface(通过contructor):
public class Service1 : IService1
{
public Service1(ISomeInterface someInterface)
{
}
...
}
Run Code Online (Sandbox Code Playgroud)
和
public class Service2 : IService2
{
public Service2(ISomeInterface someInterface)
{
}
...
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Autofac作为我的IoC工具.这个问题.如何配置Autofac注册,以便SomeImpl1自动注入Service1,SomeImpl2将自动注入Service2.
谢谢!
我使用VS 2012并创建了两个应用程序:
应用#1.MVC 3,NET 4.5
应用#2.MVC 4,NET 4.5
如果我比较这些应用程序的web.config,我看到app#2(MVC 4)注册了以下处理程序:
有人可以解释它们的使用方式和时间吗?
还有一个问题(可选).如果现在需要这些处理程序,我应该在哪里放置处理程序?他们之前还是之后?
在C#中你可以使用
System.TimeZone.CurrentTimeZone.GetUtcOffset(someDate).Hours
Run Code Online (Sandbox Code Playgroud)
但是如何在javascript中获取特定日期(Date对象)的UTC小时数?
我有两个实体(Customer和CustomerRole),并希望声明它们之间的多对多关系.我可以使用以下代码:
modelBuilder.Entity<CustomerRole>()
.HasMany(cr => cr.Customers)
.WithMany(c => c.CustomerRoles)
.Map(m => m.ToTable("Customer_CustomerRole_Mapping"));
Run Code Online (Sandbox Code Playgroud)
但它创建了关系(和第三个映射表),默认情况下关闭级联删除.如何在使用多对多时告诉EF创建与级联删除关系的关系?
我有以下接口:
public interface IConfigurationProvider<TSettings> where TSettings : ISettings, new()
{
TSettings Settings { get; }
}
public interface ISettings
{
}
Run Code Online (Sandbox Code Playgroud)
我有以下IConfigurationProvider实现:
public class ConfigurationProvider<TSettings> : IConfigurationProvider<TSettings> where
TSettings : ISettings, new()
{
public ConfigurationProvider()
{
this.BuildConfiguration();
}
public TSettings Settings { get; private set; }
private void BuildConfiguration()
{
this.Settings = new TSettings();
//...load and assign properties to 'this.Settings'
//...skipped
// now 'Settings' property contains configured 'ISettings' instance
}
}
Run Code Online (Sandbox Code Playgroud)
我也可以有不同的类实现'ISettings'接口.例如,
public class UserSettings : ISettings
{
public int …Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework CTP5(代码优先),我有两个类:
public class Order
{
public int Id {get;set;}
public decimal SomeOtherProperty1 {get;set;}
//navigation property
public virtual ICollection<OrderLine> OrderLines { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class OrderLine
{
public int Id {get;set;}
public int OrderId {get;set;}
public decimal SomeOtherProperty2 {get;set;}
//navigation property
public virtual Order Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有OrderLine类的以下配置类:
public partial class OrderLineMap : EntityTypeConfiguration<OrderLine>
{
public OrderLineMap()
{
this.HasKey(ol=> ol.Id);
this.HasRequired(ol=> ol.Order)
.WithMany(o => o.OrderLines)
.HasForeignKey(ol=> ol.OrderId);
}
}
Run Code Online (Sandbox Code Playgroud)
目前,如果您创建"OrderLine"实例,则必须指定"订单"实例.
问题:如何使ol.Order属性可选(在某些情况下为null)?可能吗?
我有两个问题:
public class Address
{
public int Id { get; set; }
public string FirstName { get; set;
public string LastName { get; set; }
}
public partial class Customer
{
public int Id { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public virtual Address BillingAddress { get; set; }
public virtual Address ShippingAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
下面是映射类:
public partial class AddressMap : EntityTypeConfiguration<Address>
{
public AddressMap()
{ …Run Code Online (Sandbox Code Playgroud) 以前我在我的项目中使用了Entity Framework 4.0 CTP5(代码优先)和SQL Compact.我使用NuGet安装了以下软件包:
现在发布EF 4.1 RC时,我想将所有库更新到最新版本.NuGet feed不再包含SQLCE.4.0.8435.1的任何更新,但它有一个新包' SqlServerCompact - 4.0.8482.1 '(新包,而不是现有包的新版本).假设它刚刚重命名,我应该安装它.这样对吗?假设,是的.
NuGet也不包含"EFCodeFirst"和"EFCodeFirst.SqlServerCompact"包的任何更新.但它有一个新的' EntityFramework 4.1.10311.0 '包.
那么我应该安装什么?" EntityFramework 4.1.10311.0 "和" SqlServerCompact - 4.0.8482.1 "是否足以首先在SQL Compact中使用EF代码?
sql-server-ce entity-framework-4 nuget sql-server-ce-4 entity-framework-4.1
我在VS2012中创建了两个应用程序
现在我打开任何.cshtml文件并添加以下代码:
<div>
@if (true)
{
@string.Format("{0}", "test")
}
</div>
Run Code Online (Sandbox Code Playgroud)
它在应用程序#1(mvc3)中工作正常,我看到"test"字显示.但它在Application#2(mvc4)中不起作用.
任何人都能解释为什么会发生这种情况以及应该改变什么?
更新:我刚刚发现了一个非常奇怪的事情.如果用@ String.format("some text")替换@ string.format("some text"),一切正常(注意大写字符串)
asp.net ×5
c# ×4
code-first ×3
asp.net-mvc ×2
autofac ×2
client-side ×1
javascript ×1
nuget ×1
razor ×1