问题描述
使用Rama Krishna Vavilala的文章作为参考,我一直在努力开发一个数据存储库模型供我的组织使用,除了一个例外,它可以按预期工作.我提供了一个简单的模型版本来说明问题.
问题:为什么在ConsoleDemo解决方案中必须包含对DemoRepository.Devel和DemoRepository.Draft项目的引用?如果包含它们,它会起作用;类型初始化器引发异常,如果无法加载,则无法加载文件或程序集或其依赖项之一."这似乎与Rama的MessageBoard示例不一致.存储库模式的重点不是使使用者(例如ConsoleDemo)看不到各个存储库(例如DemoRepository.Devel和DemoRepository.Draft)的实际实现吗?
解决方案:ConsoleDemo
控制台应用程序:ConsoleDemo
添加了参考:DemoRepository,DemoRepository.Devel,DemoRepository.Draft)
Program.cs
Using Rama Krishna Vavilala''s article as a reference, I have been working on developing a data repository model for use at my orgnaization and, with one exception, it works as expected. I have included a simple version of the model that illustrates the issue.
The question: Why do references to the DemoRepository.Devel and DemoRepository.Draft projects have to be included in the ConsoleDemo solution? It works if they are included; the type initializer throws an exception, "Could not load file or assembly [...] or one of it''s dependencies.", if they aren''t. This does not seem consistent with Rama''s MessageBoard example. Isn''t the point of the repository pattern to make the actual implementation of the individual repositories (e.g. DemoRepository.Devel and DemoRepository.Draft) invisible to the consumer (e.g ConsoleDemo)?
Solution: ConsoleDemo
Console Application: ConsoleDemo
Added references: DemoRepository, DemoRepository.Devel, DemoRepository.Draft)
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DemoRepository;
namespace ConsoleDemo
{
class Program
{
static void Main(string[] args)
{
int demoValue = DemoSource.GetDemoValue();
Console.WriteLine(demoValue);
Console.WriteLine("...");
Console.ReadLine();
}
}
}
解决方案:DemoRepository
类库:DemoRepository
DemoSource.cs
Solution: DemoRepository
Class Library: DemoRepository
DemoSource.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoRepository
{
public static class DemoSource
{
private static IDemoRepository _Demo = CreateDemoProvider();
private static IDemoRepository CreateDemoProvider()
{
string typeName =
"DemoRepository.Devel.DemoProvider,DemoRepository.Devel,"+
"Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
//"DemoRepository.Draft.DemoProvider,DemoRepository.Draft,"+
//"Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
Type type = Type.GetType(typeName, true);
return (IDemoRepository)Activator.CreateInstance(type);
}
public static int GetDemoValue()
{
return _Demo.GetDemoValue();
}
}
}
IDemoRepository.cs
IDemoRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoRepository
{
public interface IDemoRepository
{
int GetDemoValue();
}
}
类库:DemoRepository.Devel
添加的参考:DemoRepository
DemoRepository.Devel.cs
Class Library: DemoRepository.Devel
Added reference: DemoRepository
DemoRepository.Devel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoRepository.Devel
{
public class DemoProvider : IDemoRepository
{
public int GetDemoValue()
{
return (int)12345;
}
}
}
类库:DemoRepository.Draft
添加的参考:DemoRepository
DemoRepository.Draft.cs
Class Library: DemoRepository.Draft
Added reference: DemoRepository
DemoRepository.Draft.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoRepository.Draft
{
public class DemoProvider : IDemoRepository
{
public int GetDemoValue()
{
return (int)98765;
}
}
}
推荐答案
这篇关于简单数据存储区实现中的TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!