简体   繁体   English

具有存储库模式的域模型

[英]Domain Model with Repository Pattern

I have an object: Library 我有一个对象:图书馆

One library can have multiple Books, CDs and Papers. 一个图书馆可以有多本书籍,CD和论文。 Each class has Id, Name and Price property except Library class that has Address property. 每个类具有Id,Name和Price属性,但具有Address属性的Library类除外。

I want to create Repository pattern to abstract where the data is stored from the layers above. 我想创建存储库模式以抽象出从以上各层存储数据的位置。 Right now, data is stored in xml files. 现在,数据存储在xml文件中。

<library>
<books></books>
<cds></cds>
<papers></papers
</library>

Later on we are thinking of storing it in databases. 稍后我们考虑将其存储在数据库中。 We will then have a library table, a books table, a cd table and a papers table. 然后,我们将有一个图书馆表,一个书本表,一个CD表和一个纸质表。

User creates a library object on the screen and then creates books, cds and papers on the screen. 用户在屏幕上创建一个库对象,然后在屏幕上创建书籍,cd和论文。

He then clicks on Save. 然后,他单击保存。 At that point, I want to take the newly created library object and save it in XML so I want to have Repository and using dependency injection, I will inject an implementation of xml repository in the layers above so data is stored into correct place. 到那时,我想获取新创建的库对象并将其保存为XML,以便拥有Repository并使用依赖项注入,我将在上面的层中注入xml存储库的实现,以便将数据存储到正确的位置。 I can serialize the Library object and get xml and call simple Save method to save the library object. 我可以序列化Library对象并获取xml,然后调用简单的Save方法来保存该库对象。

but imagine then I replace XMLRepository with DatabaseRepository. 但是想象一下,然后用DatabaseRepository替换XMLRepository。

In that case, I want the rest of the layers above to remain unchanged. 在这种情况下,我希望上面的其余各层保持不变。 I am hoping that LibraryRepository.Save() will take care whether it needs to go in XML or CSV or DB. 我希望LibraryRepository.Save()会注意是否需要使用XML或CSV或DB。 I am confused how I will create a Repository pattern to solve LibraryRepository.Save() method. 我很困惑如何创建一个存储库模式来解决LibraryRepository.Save()方法。

Everyone everywhere says that Repository class should have a single responsibility. 每个地方的每个人都说存储库类应该承担一个责任。 It should only save one object type and shouldn't take an object, take its related classes and save them too. 它只应保存一种对象类型,而不应采用对象,采用其相关类并也保存它们。 Each class should have its own Repository class. 每个类都应具有自己的存储库类。

Also each library object has multiple Books objects. 同样,每个库对象都有多个Books对象。 I don't want to use a for loop and go through each book in LibraryRepository.Save method. 我不想使用for循环来浏览LibraryRepository.Save方法中的每本书。

I am not sure how I should create my domain model and call the Library Save() method. 我不确定如何创建域模型并调用Library Save()方法。

Please guide. 请指导。

As repository is an abstraction of persistency for your BL, use repository interface. 由于存储库是BL持久性的抽象,因此请使用存储库接口。

Each repository implementation should take care of object structure mapping to data storage, or use specific mappers. 每个存储库实现都应注意将对象结构映射到数据存储,或使用特定的映射器。 BL/Service classes would work with repository via interface, without knowledge of internal implementation of a particular repository. BL / Service类将通过接口与存储库一起使用,而无需了解特定存储库的内部实现。

So you'd have something like 所以你会有类似

class LibraryService {
    public LibraryService (ILibraryRepository repo) {} 
    public DoSomeWork(somedata) {
        Library lib = repo.Load(somedata.libid);
        ....
        repo.Save(lib);
    }
}

And several implementations 以及几种实现

class DBLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //hibernate session 
        //and you'll have mapping defined for all entities
        //and relations between library and stuff, so books are saved automatically
        _session.Save(lib)
    }
}

class XMLLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //serialized does all the work, so no additional loops here
        var xml = Serialize(lib);
        SaveToFile(xml);
    }
}

class CSVLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //for example, save library to lib.csv, books - to books.csv.
        //add mappers to do real work, but we'd take care of separation 
        //of books and library manually.
        // (in case of ORM - it has own mappers, 
        //  for XML - serializator is mapper itself)
        var data = LibraryCSVDataMapper.Map(lib);
        data.Save();
        foreach(var book in lib.Books){
            data = BookCSVDataMapper.Map(book);
            data.Save();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM