好吧,已经有一段时间了,对不起!
我成功地创建了一个通用方法。而不是每个实体都这样:
public static ILanguage GetNewLanguage()
{
return new Language();
}
我现在正在使用这种通用方法(但我认为它仍然是一种笨拙的方法):
public static T CreateNew<T>(out string errmsg) where T : class
{
errmsg = string.Empty;
// Loading the DAL assembly as you cannot allways be sure that it is loaded,
// as it can be used from the GAC and thereby not accessible as a loaded assembly.
AppDomain.CurrentDomain.Load("DAL");
// From loaded assemblies get the DAL and get the specific class that implements
// the interface provided, <T>.
// It is assumed for the time being, that only one BLL dataobject implements the interface.
var type = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(T).IsAssignableFrom(p) && p.IsClass)
.FirstOrDefault();
try
{
// Create an instance of the class that implements the interface in question, unwrap it
// and send it back as the interface type.
var s = Activator.CreateInstance("DAL", type.FullName);
return (T)s.Unwrap();
}
catch (Exception ex)
{
errmsg = ex.ToString();
return null;
}
}
表示层的调用现在看起来像这样:
string errmsg = string.Empty;
ILanguage language = BLL.CreateNew<ILanguage>(out errmsg);
我解决了明显的问题,但仍然没有以一种奇特的方式。我有一个使用 DI 将程序集彼此分离的想法,但我不确定如何执行此操作。评论非常受欢迎。如果我找到一个解决方案,我会在新线程中发布解决方案。
而且,当我弄清楚这一点后,我将在一个新线程中发布一个解决方案,以解决 Crud 关于如何将 BLL 与 DAL 与存储库类分离的想法!
干杯芬恩。