因此,对于我的数据库访问,我有一个现在使用 EF 4.0 的存储库类,这是一个独立的 dll,因此我可以创建使用 EF 5 的其他存储库 dll,并在我的项目中导入这两个 dll,然后我可以根据实例化正确的存储库到我可以使用的 EF 5.0 版本。这是配置文件中的一个参数。这是最好的方法吗?
你可以走这条路,除非你认为这可能会导致未来的维护/开发问题,否则我并没有真正看到它的问题。您还可以考虑做其他几件事。我认为两者都是完全有效的,可能只是个人意见/偏好。
- 模块您可以采用模块化路线,您的存储库 DLL 可能会被动态加载。查看Microsoft 的 Unity 库。这应该允许您在每个存储库 DLL 中创建一个 IModule,以便根据需要设置您的应用程序。然后只需创建一个 UnityBootstrapper 类来告诉它如何找到您的模块(手动添加它们,查看目录等)。这应该允许您热交换您的存储库 DLL,而不必担心如果您不想设置配置文件。
- 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.