让我们假设我有3个项目
Core
和Common
引用了Common
。想法是Web
项目包含Core
和Web
的常用接口。
假设在两个项目中我都有一些映射,我希望有一个共同的接口。例如,在我需要使用AutoMapper的Core
项目上,在 using AutoMapper;
namespace Common.Mapper
{
public interface IMapperConfiguration
{
IMapper GetMapper();
}
}
项目中我想使用其他东西。
对于AutoMapper,我需要界面。
Web
这在Common
项目中有效,但是我无法将此接口移动到Dictionary={"Array":"A Data Structure which holds only one data type","Assignment":"Associate names with values","Boolean":"An on or off answer, i.e Yes or No","Character":"A printed symbol","Constant":"A value which can't be altered during normal execution","Data Type":"A piece of data which is defined by the value or character(s) it is"}
Terms=(list(Dictionary.keys()))
Defs=(list(Dictionary.values()))
boot=Tk()
boot.attributes("-topmost", True)
boot.title("Count-Down!")
QuestionLabel=Label(boot, text="What is the corresponding definition to the below term?")
TermLabel=Label(boot, text=(random.choice(Terms)),bg="grey")
Answer1=Button(boot, text=(random.choice(Defs)), width=75)
Answer2=Button(boot, text=(random.choice(Defs)), width=75)
Answer3=Button(boot, text=(random.choice(Defs)), width=75)
Answer4=Button(boot, text=(random.choice(Defs)), width=75)
QuestionLabel.grid(row=1, column=1, columnspan=2)
TermLabel.grid(row=2, column=1, columnspan=2)
Answer1.grid(row=3, column=1)
Answer2.grid(row=3, column=2)
Answer3.grid(row=4, column=1)
Answer4.grid(row=4, column=2)
项目,因为此接口依赖于AutoMapper(IMapper来自AutoMapper)。
如何为此创建通用界面? 我可以使用一些设计模式吗?
Mapper只是一个例子。我想知道如何解决这样的问题。 谢谢你的帮助。
答案 0 :(得分:4)
您需要使您的界面通用。如果您没有使AutoMapper对两个项目都通用,则它无法返回IMapper的实现。请原谅我,因为我不使用automapper,但你会考虑将它包装在一个实现你自己的IMapper接口的类中,如:
namespace Common.Mapper
{
public interface IMapper
{
void Map(...args...);
}
}
namespace Web.Mapper
{
public class Mapper : Common.Mapper.IMapper
{
public void Map(...args...)
{
AutoMapper.Map(...args...);
}
}
}
namespace Core.Mapper
{
public class Mapper : Common.Mapper.IMapper
{
public void Map(...args...)
{
SomeOtherMapper.Map(...args...);
}
}
}