很抱歉这个问题在这里到处都是......但我感觉就像一条狗在追我的尾部,此时我很困惑。
我正在尝试寻找开发 3 层解决方案(IL、BL、DL)的最简洁的方法,其中 DL 使用 ORM 来抽象对数据库的访问。
在我所见的任何地方,人们都使用 LinqToSQL 或 LLBLGen Pro 来生成代表数据库表的对象,并在所有 3 层中引用这些类。 似乎 40 年的编码模式被忽略了——或者发生了范式转变,我错过了关于为什么这样做完全可以的解释部分。
然而,似乎仍然有一些基础希望成为不可知的数据存储机制——看看 LinqToSQL 刚刚发生了什么:很多代码都是针对它编写的——只针对 MS 放弃它...所以我想尽可能地隔离 ORM 部分,只是不知道如何。
所以,回到绝对基础,这里是我希望以非常非常干净的方式组装的基本部分:
我开始的程序集: UL.dll BL.dll DL.dll
主要类:
一个具有公开 MessageAddress 对象集合(称为 MessageAddresses)的属性的 Message 类:
class Message
{
public MessageAddress From {get;}
public MessageAddresses To {get;}
}
每层的功能:
BL 向 UI 公开了一个名为 GetMessage (Guid id) 的方法,该方法返回 Message 的一个实例。
BL 依次包裹 DL。
DL 有一个 ProviderFactory,它包装了一个 Provider 实例。 DL.ProviderFactory 公开了(可能......我的部分问题)两个静态方法称为 GetMessage(Guid id),和 SaveMessage(消息消息) 最终目标是能够将一个为 Linq2SQL 编写的提供程序换成一个为 LLBLGen Pro 编写的提供程序,或另一个不针对 ORM 工作的提供程序(例如 VistaDB)。
设计目标: 我想要层分离。 我希望每一层只依赖于它下面的层,而不是上面的层。 我希望 ORM 生成的类只在 DL 层中。 我希望 UL 与 BL 共享 Message 类。
因此,这是否意味着:
a) 消息在 BL 中定义 b) DB 表的 Db/Orm/Manual 表示('DbMessageRecord',或'MessageEntity',或任何其他 ORM 调用它)在 DL 中定义。 c) BL 依赖于 DL d) 在调用没有 ref 或不知道 BL 的 DL 方法之前,BL 必须将它们转换为 BL 实体(例如:DbMessageRecord)?
UL:
Main()
{
id = 1;
Message m = BL.GetMessage(id);
Console.Write (string.Format("{0} to {1} recipients...", m.From, m.To.Count));
}
BL:
static class MessageService
{
public static Message GetMessage(id)
{
DbMessageRecord message = DLManager.GetMessage(id);
DbMessageAddressRecord[] messageAddresses = DLManager.GetMessageAddresses(id);
return MapMessage(message,
}
protected static Message MapMessage(DbMessageRecord dbMessage. DbMessageAddressRecord[] dbAddresses)
{
Message m = new Message(dbMessage.From);
foreach(DbMessageAddressRecord dbAddressRecord in dbAddresses){
m.To.Add(new MessageAddress (dbAddressRecord.Name, dbAddressRecord.Address);
}
}
DL:
static class MessageManager
{
public static DbMessageRecord GetMessage(id);
public static DbMessageAddressRecord GetMessageAddresses(id);
}
问题: a) 显然这是迟早要做的大量工作。 b) 更多错误 c) 较慢 d) 由于 BL 现在依赖于 DL,并且正在引用 DL 中的类(例如 DbMessageRecord),似乎因为这些是由 ORM 定义的,所以你不能撕掉一个 Provider,并将其替换为另一个,...使整个练习变得毫无意义……不妨在整个 BL 中使用 ORM 的类。 e) 或者...在 BL 和 DL 之间需要另一个程序集,并且需要另一个映射以使 BL 独立于底层 DL 类。
希望我能把问题问得更清楚……但我现在真的迷路了。任何帮助将不胜感激。
最佳答案
这有点到处都是,让我想起了我第一次涉足 orm 和 DDD。 我个人使用核心域对象、消息传递对象、消息处理程序和存储库。 因此,我的 UI 向处理程序发送一条消息,该处理程序又通过存储库对我的对象进行水合,并在该域对象中执行业务逻辑。我使用 NHibernate 进行数据访问,使用 FluentNHibernate 进行类型化绑定(bind),而不是使用松散的 .hbm 配置。
所以消息传递是我的 UI 和我的处理程序之间共享的所有内容,并且所有 BL 都在域中。
我知道我可能因为我的解释而敞开心扉接受惩罚,如果不清楚我稍后会辩护。
就我个人而言,我不是代码生成对象的忠实粉丝。
我必须继续补充这个答案。 尝试将您的消息传递视为命令而不是代表数据库的数据实体。我会给你一个我的简单类和基础设施决策的例子,它对我来说非常有效,我不能相信:
[Serializable]
public class AddMediaCategoryRequest : IRequest<AddMediaCategoryResponse>
{
private readonly Guid _parentCategory;
private readonly string _label;
private readonly string _description;
public AddMediaCategoryRequest(Guid parentCategory, string label, string description)
{
_parentCategory = parentCategory;
_description = description;
_label = label;
}
public string Description
{
get { return _description; }
}
public string Label
{
get { return _label; }
}
public Guid ParentCategory
{
get { return _parentCategory; }
}
}
[Serializable]
public class AddMediaCategoryResponse : Response
{
public Guid ID;
}
public interface IRequest<T> : IRequest where T : Response, new() {}
[Serializable]
public class Response
{
protected bool _success;
private string _failureMessage = "This is the default error message. If a failure has been reported, it should have overwritten this message.";
private Exception _exception;
public Response()
{
_success = false;
}
public Response(bool success)
{
_success = success;
}
public Response(string failureMessage)
{
_failureMessage = failureMessage;
}
public Response(string failureMessage, Exception exception)
{
_failureMessage = failureMessage;
_exception = exception;
}
public bool Success
{
get { return _success; }
}
public string FailureMessage
{
get { return _failureMessage; }
}
public Exception Exception
{
get { return _exception; }
}
public void Failed(string failureMessage)
{
_success = false;
_failureMessage = failureMessage;
}
public void Failed(string failureMessage, Exception exception)
{
_success = false;
_failureMessage = failureMessage;
_exception = exception;
}
}
public class AddMediaCategoryRequestHandler : IRequestHandler<AddMediaCategoryRequest,AddMediaCategoryResponse>
{
private readonly IMediaCategoryRepository _mediaCategoryRepository;
public AddMediaCategoryRequestHandler(IMediaCategoryRepository mediaCategoryRepository)
{
_mediaCategoryRepository = mediaCategoryRepository;
}
public AddMediaCategoryResponse HandleRequest(AddMediaCategoryRequest request)
{
MediaCategory parentCategory = null;
MediaCategory mediaCategory = new MediaCategory(request.Description, request.Label,false);
Guid id = _mediaCategoryRepository.Save(mediaCategory);
if(request.ParentCategory!=Guid.Empty)
{
parentCategory = _mediaCategoryRepository.Get(request.ParentCategory);
parentCategory.AddCategoryTo(mediaCategory);
}
AddMediaCategoryResponse response = new AddMediaCategoryResponse();
response.ID = id;
return response;
}
}
我知道这种情况会一直持续下去,但在过去一年左右的时间里,这个基本系统对我来说非常有用
您可以看到处理程序允许域对象处理域特定逻辑
关于c# - ORM 和图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/923502/