那麼,它已經有一段時間,很抱歉的!
我成功地創建了一個通用方法。取而代之的是對每一個實體:
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中分離出來,並在一個新的線程中解決這個問題!
Cheers Finn。
通用的方式肯定有它的缺點,因爲它比直線速度慢20-30倍。所以它取決於性能和你的個人風格實施什麼... –