我最近(几个月前)改变了工作,继承了一个代码库,它违反了SOLID原则的每一个,尽可能多次.似乎编写此代码的人决定详细研究每一个良好的编码实践,并尽可能经常地和最根本地违反它们.
我是该产品的唯一开发人员 - 组织中没有人知道代码,代码库太大而且复杂,无法完全重写.我正在研究可以使代码库变得灵活和健壮的最高价值变化.放弃此产品也不是一种选择.
产品中所有问题的根源于一组类,这些类是核心业务逻辑数据结构.这些类有很多问题,但我真正感兴趣的是以下内容:
public static class NetCollection
{
private static Logger LogFile { get { return Logger.GetMethodLogger(2); } }
// Declare local variables.
private static Dictionary<string, NetObject> netObjectHashTable;
private static Dictionary<string, NetTitle> titlePropertyHashTable;
private static Dictionary<string, NetObject> referenceDataHashTable;
private static Dictionary<int, SortedDictionary<string, int>> picklistHashTable;
public static IEnumerable<NetObject> NetObjects
{
get
{
return netObjectHashTable.Values;
}
}
static NetCollection()
{
netObjectHashTable = new Dictionary<string, NetObject>();
titlePropertyHashTable = new Dictionary<string, NetTitle>();
referenceDataHashTable = new Dictionary<string, NetObject>();
picklistHashTable = new Dictionary<int, SortedDictionary<string, …Run Code Online (Sandbox Code Playgroud) 我正在做代码优先实体框架设计。
我有一个表 Account,它有一个属性 Supervisor:
public class Account
{
public int Id { get; set; }
public Account Supervisor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这很好用。
但是,我希望在班级中添加一名替代主管:
public class Account
{
public int Id { get; set; }
public Account Supervisor { get; set; }
public Account AlternateSupervisor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我运行 Add-Migration AddAlternateSupervisor 时,生成的代码给了我以下内容:
public partial class AddAlternateSupervisor : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Accounts_Accounts_SupervisorId",
table: "Accounts");
migrationBuilder.DropIndex(
name: "IX_Accounts_SupervisorId",
table: "Accounts");
migrationBuilder.AddColumn<int>(
name: …Run Code Online (Sandbox Code Playgroud) entity-framework ef-code-first entity-framework-core .net-core
我认为我得到了关于依赖倒置和使用IoC容器的大部分内容,但有一件事对我来说仍然不明显.如何使用autofac自动执行以下工厂:
public class WidgetFactory
{
public static IWidget Create(int foo, double bar)
{
return new Widget(foo, bar);
}
}
public class Widget
{
private readonly int foo;
private readonly double bar;
public Widget(int foo, double bar)
{
this.foo = foo;
this.bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud)
别处...
public class FoobarUser
{
public void Method()
{
var widget = WidgetFactory.Create(3, 4.863);
// Do something with my widget
// Possibly add it to a widget collection
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要创建数千个小部件,我不确定这样做的最佳方式.我如何使用autofac创建小部件工厂?如何在Method中使用它,请记住Method不包含对IContainer的引用?