小编Mih*_*gos的帖子

使用Autofac创建具有依赖项的类的实例

问题:

假设班级:

public class MyAwesomeClass
{
   private IDependCls _dependCls;
   public MyAwesomeClass(IDependCls dependCls)
   {
       _dependCls = dependCls;
   }

}
Run Code Online (Sandbox Code Playgroud)

在其他地方我需要获得该类的实例,如下所示:

public class SomewhereElse
{
    public void AwesomeMethod()
    {
        //...
        // AwesomeStuff
        //...

        var GetErDone = new MyAwesomeClass();  // PROBLEM! No constructor with 0 arguements
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,我

建议的解决方案1:

A)必须制作一个额外的构造来解决依赖关系吗?例如:

   public MyAwesomeClass() // new constructor
   {
       _dependCls = DependencyResolver.Current.GetService<IDependCls>();
   }


public class SomewhereElse
{
    public void AwesomeMethod()
    {
        var GetErDone = new MyAwesomeClass();  // IT WORKS!!
    }
}
Run Code Online (Sandbox Code Playgroud)

建议的解决方案2:

B)使用AwesomeMethod之前 …

c# asp.net-mvc dependency-injection inversion-of-control autofac

12
推荐指数
1
解决办法
2万
查看次数

EF4.3.1中的MaxLength属性

 The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute' 
 exists in both 

 [path...]\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll 

 and

'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'  
Run Code Online (Sandbox Code Playgroud)

现在,我已经在msdn上读到可以安全地排除EntityFramework引用(通过nuget包添加).但是,当我这样做时,我无法正确创建DBContext,因为DbModelBuilder类存在于EntityFramework dll中.此外,当我删除EntityFramework参考时,其他一些关键类缺失,所以现在这是一个古老而无关的解决方案.

更新(消歧义):都System.ComponentModel.DataAnnotations.dllEntityFramework.dll 包括System.ComponentModel.DataAnnotations.MaxLengthAttribute.问题是每个dll还包括对EF代码优先设计至关重要的其他类.例如:

EntityFramework.dll:
 - System.Data.Entity.DbModelBuilder

System.ComponentModel.DataAnnotations.dll:
 - System.ComponentModel.DataAnnotations.RegularExpressionAttribute
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc entity-framework ef-code-first .net-4.5

9
推荐指数
2
解决办法
8746
查看次数

EF代码首先是模块化设计

首先使用ef4代码,您可以创建和编译类和dbcontext.当你想在模型集的已经编译的dll中添加一些类/表和关系时会发生什么?

到目前为止,我提出的解决方案是使用"部分"类,后面会称赞它们,第二个是编写一个全新的dbcontext,包括第一个以某种方式或扩展它,但这意味着额外的每个模块的db连接(每个db上下文).有关于此的任何想法?什么是最佳做法?此外,我需要能够使用迁移.

更明确地说,可能的情况如下:

A)您创建一个.dll,其中包含一些dbContextBase类和表(类).

B)您以自己的方式创建依赖/扩展dbContextBase的其他.dll*

C)您在项目中引用.dll并将其扩展.

所以基本上你可以有一个核心dbContext,然后添加一个菜单模块,然后你添加一个博客模块(但它可以通过菜单模块看到,以创建最新的博客文章菜单等).最重要的是,如果您想要一个特定的博客一次性功能,您可以快速集成它,但也可以保持您的博客模块可更新.

我希望看到它的最佳方法是使用每个模块的模型(等)的源代码Nuget包,而不是编译的dll.

architecture asp.net-mvc database-migration entity-framework-4 ef-code-first

5
推荐指数
2
解决办法
1119
查看次数

Teamcity和MSBuild 4.5

有没有办法定位框架4.5并在Teamcity中使用MSBuild 4.5?到目前为止,我的搜索是徒劳的.

msbuild teamcity continuous-integration

5
推荐指数
1
解决办法
573
查看次数

Nhibernate查询具有包含值的Dictionary属性的项目

我需要一种方法来在Nhibernate中查询具有包含值的Dictionary属性的项目.

假设:

public class Item
{
     public virtual IDictionary<int, string> DictionaryProperty {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

和映射:

    public ItemMap()
    {
        HasMany(x => x.DictionaryProperty)
            .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
            .AsMap<string>(
                index => index.Column("IDNumber").Type<int>(),
                element => element.Column("TextField").Type<string>().Length(666)
                )
            .Cascade.AllDeleteOrphan()
            .Fetch.Join();
    }
Run Code Online (Sandbox Code Playgroud)

我想查询所有Item具有字典值"SomeText"的s.Linq中的以下示例失败:

session.Query<Item>().Where(r => r.DictionaryProperty.Any(g => g.Value == "SomeText"))
Run Code Online (Sandbox Code Playgroud)

有错误

cannot dereference scalar collection element: Value
Run Code Online (Sandbox Code Playgroud)

那么在NHibernate中有没有办法实现这一点?Linq不是独家要求,而是优先考虑.并不是说我对查询可以使用的字典不感兴趣.ContainsKey.Φορ 是相似但不相同的

nhibernate fluent-nhibernate linq-to-nhibernate nhibernate-criteria

5
推荐指数
1
解决办法
1109
查看次数

.NET数据库迁移支持部分版本

场景很简单,一个模块化的应用程序,每个模块都必须在升级期间管理自己的数据库部分.

正在使用的ORM是NHibernate,所以如果有一些与之相配的东西,那就是一个额外的优势.

此外,它需要能够在没有外部工具(MSBuild等)的情况下执行,而应该通过ADO.NET(如NHibernate的SchemaUpdate)运行.

nhibernate database-migration self-updating fluent-nhibernate

2
推荐指数
1
解决办法
1037
查看次数