【发布时间】:2014-04-28 11:27:58
【问题描述】:
请注意,此代码是我通常如何编写代码的代码示例,但我刚刚删除了会从我的问题中转移焦点的代码。我期待着聆听。
我可以理解我需要至少 10 次重复。在我发布图片之前,我的图片说明了我的问题...所以请点击此链接在 codereview.stackexchange.com 上查看我的原始问题 - https://codereview.stackexchange.com/questions/44237/domain-modelling-with-repository
我一直在努力解决一些架构问题,我自己也很难搞清楚。
我正在尝试使用域模型和存储库模式构建项目的基本结构。
当我想实现一些业务逻辑和不同类型的 UI(例如 WinForms 和 MVC)时,构建 POCO 类和存储库很容易我觉得我错过了一些东西,因为我觉得我的代码是紧密耦合的当我需要获取一个对象并显示它时,我总是必须参考 POCO 类。
我首先在 C#(vs2012) 中构建以下项目:
型号
DAL
BL
测试控制台
这是我的模型一个模型类的示例:
namespace Panda.Model
{
public class Person : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public Person()
{
}
public Person(string name)
{
this.Name = name;
}
}
}
这是我的 BL 项目中 Persons 类的示例代码:
using Panda.DAL.Repositories;
using Panda.DAL.Contexts;
using Panda.Model;
namespace Panda.BL
{
public class Logic
{
private readonly IRepository<Person> _personRep;
public Logic()
{
_personRep = new Repository<Person>(new GenericContext());
}
public LinkedList<Person> ListOfPersons()
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.AddFirst(new Person("Nicklas"));
persons.AddFirst(new Person("Martin"));
persons.AddFirst( new Person("Kresten"));
return persons;
}
}
我的 DAL 项目由采用 IEntity 类型的类的通用存储库组成:
public class Repository<T> : IRepository<T> where T : class, IEntity
{
/// <summary>
/// The Generic Repository class that can use all Model classes when istantiating it.
/// It holds all the generic methods for insert, select, delete and update.
/// </summary>
internal DbSet<T> DbSet;
internal GenericContext Context;
public Repository(GenericContext context)
{
this.Context = context;
DbSet = context.Set<T>();
}
我在控制台应用程序中的 program.cs 文件的代码如下所示:
using Panda.BL;
namespace Panda.TestConsole
{
public class Program
{
static void Main(string[] args)
{
Logic lol = new Logic();
foreach (var item in lol.ListOfPersons())
{
Console.WriteLine(item.Name);
}
}
}
}
问题是我不知道如何将我的模型和 DAL 与我的 UI 项目(控制台等)进一步分离。每次我想获得 i.ex。当我想使用 BL 项目中的方法时,我当然必须从控制台项目中引用我的模型项目。
我对整个 DDD 和 3 层模式的理解是,当您想要添加新的 UI 项目(例如控制台、WebForms 或 MVC)时,您应该只能与 BL 交谈(参考)但是现在当我想在 BL 项目中使用方法时,我总是必须同时引用 Models 和 BL。
现在我觉得有很多依赖关系将事物紧密耦合..
我真的很期待听到你对这个让我困惑了一段时间的想法。
提前致谢
【问题讨论】:
标签: c# entity-framework domain-driven-design 3-tier