简单数据存储区实现中的TypeInitializationException [英] TypeInitializationException in a simple Data Repository implementation

查看:79
本文介绍了简单数据存储区实现中的TypeInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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;
        }
    }
}

推荐答案

在存储库框架(通常用于需要敏捷测试的大型项目)中,通常将处理存储库,业务模型,服务和表示形式作为单独的项目.在领域驱动的设计中,业务模型是核心和独立的,并且其之上的其他层取决于模型.对外层的依赖性增加,UI可能直接或间接依赖于其下的其他层.这是为了使核心模块具有可重用性和维护性.因此,外层必须添加内层项目的引用.

在这种情况下,您说您已经添加了这些引用.但是您没有使用它.对于其他名称空间,您也必须尝试使用​​ using DemoRepository; 相同的内容.如果需要从其他名称空间访问类,则需要使用using关键字,否则需要使用fully qualifying name like Namespace Name.className

祝您好运.
In a repository frameworks (which is usually for larger projects which need agile testing) usually the repository, business model, services and presentation all will be treated as separate projects. In domain driven design the business model is the core and independent and other layers above it dependent on the model. The dependency increase towards the outer layers and the UI may directly or indirectly depend on the other layers below it. This is to make re-usability and maintenance of the core modules. So the outer layers has to add references of the inner layer projects.

In you case you said you had added those references. But you are not using it. You have to try with using DemoRepository; same thing for other namespaces as well. Where ever you need to access classes from other namespaces then you need to use the using keyword or else thefully qualifying name like Namespace Name.className

Good luck.


如果您在链接到文章的最底部的消息部分中提出问题,则可能会更快地找到解决方案.

没有人比拉玛更了解这些东西.
You might get a solution more quickly if you ask your question in the messages section at the very bottom of the article you linked to.

Nobody should know this stuff better than Rama.


这篇关于简单数据存储区实现中的TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆