1

好吧,我有一个项目,目前我正在使用 .NET 4.0,因为我希望这个应用程序与 windows XP 兼容,因为 EF 5.0 仅适用于 windows 7 及更高版本。

但是,我想用 .NET 4.5 的特性来实现应用程序的某些部分,例如 EF 5.0。

因此,对于我的数据库访问,我有一个现在使用 EF 4.0 的存储库类,这是一个独立的 dll,因此我可以创建使用 EF 5 的其他存储库 dll,并在我的项目中导入这两个 dll,然后我可以根据实例化正确的存储库到我可以使用的 EF 5.0 版本。这是配置文件中的一个参数。这是最好的方法吗?

我问这个是因为我不知道我必须在哪里声明我的接口。因为我的存储库类需要实现这个接口,但是这会将我的 dll 绑定到我的应用程序,但是我需要在两个不同的应用程序中使用这个存储库,所以我想实现一次,并在许多应用程序中使用。我想要独立的dll,因为现在是两个应用程序,但将来可以更多。

想要在使用存储库的应用程序中使用接口的原因是因为我想根据配置文件设置在运行时实例化正确的存储库。所以在未来我可以实现新的存储库,并且不需要更改代码。

EDIT1:我读到了多目标,但是如果在我的项目中我使用 .NET 4.0 的功能并且我想为 3.5 编译,我会收到一个错误,因为 3.5 中不存在此功能。这是正确的。那么唯一的方法是维护两个不同的项目吗?这将是一项双重工作。

谢谢。戴姆洛克。

4

1 回答 1

1

因此,对于我的数据库访问,我有一个现在使用 EF 4.0 的存储库类,这是一个独立的 dll,因此我可以创建使用 EF 5 的其他存储库 dll,并在我的项目中导入这两个 dll,然后我可以根据实例化正确的存储库到我可以使用的 EF 5.0 版本。这是配置文件中的一个参数。这是最好的方法吗?

你可以走这条路,除非你认为这可能会导致未来的维护/开发问题,否则我并没有真正看到它的问题。您还可以考虑做其他几件事。我认为两者都是完全有效的,可能只是个人意见/偏好。

  1. 模块您可以采用模块化路线,您的存储库 DLL 可能会被动态加载。查看Microsoft 的 Unity 库。这应该允许您在每个存储库 DLL 中创建一个 IModule,以便根据需要设置您的应用程序。然后只需创建一个 UnityBootstrapper 类来告诉它如何找到您的模块(手动添加它们,查看目录等)。这应该允许您热交换您的存储库 DLL,而不必担心如果您不想设置配置文件。
  2. Preprocessor Directives With preprocessor directives you get to define how your code will compile. Depending on how you have your classes structured this may be something fairly simple to set up or a complete nightmare that makes you want to abstract and refactor your classes. This question: Detect target framework version at compile time has an answer for handling different compile results depending on the target framework. Personally though, I like the modular route.

I ask this because I don't know where I must declare my interface. because my repository classes need to implement this interface, but then this tie my dlls to my application, but I need to use this repositories in two different applications, so I want to implement once, and use in many applications. I want independent dlls, because now are two applications, but in the future, can be more.

The reason to want to use an interface in the application that uses the repositories is because I would like to instantiate at runtime the correct repository, according to the config file settings. So in the fututre I can implement new repositories and there is no needed to change the code.

Sounds like you need to create another library that is used to communicate between your UI and your Repository libraries. This can be a little tricky and overwhelming to set up just right. Basically you want your gateway DLL to house the interfaces and business objects. Your Application would reference this DLL and this DLL would reference your repositories.

Depending on your needs you may actually need to set up another intermediary DLL that would actually just house your interfaces and most basic utility classes. This would allow you to have your EF objects implement the same interface that your application is using without the need for your gateway DLL having to map your business objects and EF objects back and forth.

EDIT1: I read about multi targeting, but if in my project I use features for example of .NET 4.0 and I want to complie for 3.5, I get an error because this feature does not exist in 3.5. That's correct. Then the only way is to mantain two different projects? It would be a double work.

I believe you can get around this by using the Preprocessor Directives I mentioned above. Below is just an example of making a method handle work differently depending on if the framework is .NET 2.0; it's just an example and not tested. The DefineConstants will need to be set up, but this should allow you to handle 1 project for multiple framework targets while also being able to use newer .NET features as they are released.

public Person FindPersonByName(List<Person> people, string name)
{
#if DOTNET_20
    foreach(Person person in people)
    {
        if (person.Name == name)
            return person;
    }
    return null;
#else
    return people.FirstOrDefault(p => p.Name == name);
#endif
}

I hope this was helpful and the best of luck in finding the right solution.

于 2012-11-10T12:30:16.173 回答