Как использовать шаблон репозитория правильно?

При использовании lighthttpd можно также использовать FastCGI для обслуживания Django. Я не уверен, как скорость выдерживает сравнение с mod_wsgi, но если не изменяет память правильно, Вы извлекаете несколько пользы, которую Вы извлекли бы с mod_wsgi, который Вы не получите с mod_python. Основной, являющийся этим, можно дать каждому приложению его собственный процесс (который действительно полезен для хранения памяти различных разделенных приложений, а также для использования в своих интересах многоядерных компьютеров.

Редактирование: Только для добавления в отношении обновления о nginix, если не изменяет память, правильно снова, nginix использует "greenlets" для обработки параллелизма. Это означает, что Вы, возможно, должны немного больше стараться удостовериться, что одно приложение не съедает время всего сервера.

86
задан chobo2 30 September 2009 в 00:22
поделиться

6 ответов

One thing that i did wrong when played around with repository pattern - just like you, i thought that table relates to repository 1:1. When we apply some rules from Domain Driven Design - grouping repositories problem often disappears.

Repository should be per Aggregate root and not table. It means - if entity shouldn't live alone (i.e. - if you have a Registrant that participates in particular Registration) - it's just an entity, it doesn't need a repository, it should be updated/created/retrieved through repository of aggregate root it belongs.

Of course - in many cases, this technique of reducing count of repositories (actually - it's more a technique to structure your domain model) can't be applied because every entity is supposed to be an aggregate root (that highly depends on your domain, I can provide blind guesses only). In your example - License seems to be an aggregate root because you need to be able to check them with no context of Registration entity.

But that does not restrict us to cascade repositories (Registration repository is allowed to reference License repository if needed). That does not restrict us to reference License repository (preferable - through IoC) directly from Registration object.

Just try not to drive your design through complications provided by technologies or misunderstanding something. Grouping repositories in ServiceX just because you don't want to construct 2 repositories ain't good idea.

Much better would be to give it a proper name - RegistrationService i.e.

But services should be avoided in general - they often are cause that leads to anemic domain model.

EDIT:
Do start to use IoC. It truly eases the pain of injecting dependencies.
Instead of writing:

var registrationService = new RegistrationService(new RegistrationRepository(),  
      new LicenseRepository(), new GodOnlyKnowsWhatElseThatServiceNeeds());

you will be able to write:

var registrationService = IoC.Resolve<IRegistrationService>();

P.s. Would be better to use so called common service locator but that's just an example.

41
ответ дан 24 November 2019 в 08:07
поделиться

Одна вещь, которую я начал делать, чтобы решить эту проблему, - это фактически разрабатывать службы, которые обертывают N репозиториев. Надеюсь, ваши фреймворки DI или IoC помогут сделать это проще.

public class ServiceImpl {
    public ServiceImpl(IRepo1 repo1, IRepo2 repo2...) { }
}

Есть ли в этом смысл? Кроме того, я понимаю, что разговоры об услугах в этом поместье могут соответствовать, а могут и не соответствовать принципам DDD, я просто делаю это потому, что кажется, что это работает.

5
ответ дан 24 November 2019 в 08:07
поделиться

Что я делаю, так это то, что у меня есть абстрактный базовый класс, определенный следующим образом:

public abstract class ReadOnlyRepository<T,V>
{
     V Find(T lookupKey);
}

public abstract class InsertRepository<T>
{
     void Add(T entityToSave);
}

public abstract class UpdateRepository<T,V>
{
     V Update(T entityToUpdate);
}

public abstract class DeleteRepository<T>
{
     void Delete(T entityToDelete);
}

Затем вы можете получить свой репозиторий из абстрактного базового класса и расширить свой единственный репозиторий до тех пор, пока общие аргументы различаются, например:

public class RegistrationRepository: ReadOnlyRepository<int, IRegistrationItem>,
                                     ReadOnlyRepository<string, IRegistrationItem> 

и т. д.

Мне нужны отдельные репозитории, потому что у нас есть ограничения на некоторые из наших репозиториев, и это дает нам максимальную гибкость. Надеюсь, это поможет.

2
ответ дан 24 November 2019 в 08:07
поделиться

I suggest you to look at Sharp Architecture. They suggest using one repository per entity. I'm using it currently in my project and wery pleased with results.

0
ответ дан 24 November 2019 в 08:07
поделиться

I have this as my repository class and yeah I extend in the table / area repository but still I sometimes have to break DRY.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcRepository
{
    public class Repository<T> : IRepository<T> where T : class
    {
        protected System.Data.Linq.DataContext _dataContextFactory;

        public IQueryable<T> All()
        {
            return GetTable.AsQueryable();
        }

        public IQueryable<T> FindAll(Func<T, bool> exp)
        {
            return GetTable.Where<T>(exp).AsQueryable();
        }

        public T Single(Func<T, bool> exp)
        {
            return GetTable.Single(exp);
        }

        public virtual void MarkForDeletion(T entity)
        {
            _dataContextFactory.GetTable<T>().DeleteOnSubmit(entity);
        }

        public virtual T CreateInstance()
        {
            T entity = Activator.CreateInstance<T>();
            GetTable.InsertOnSubmit(entity);
            return entity;
        }

        public void SaveAll()
        {
            _dataContextFactory.SubmitChanges();
        }

        public Repository(System.Data.Linq.DataContext dataContextFactory)
        {
            _dataContextFactory = dataContextFactory;
        }

        public System.Data.Linq.Table<T> GetTable
        {
            get { return _dataContextFactory.GetTable<T>(); }
        }

    }
}

EDIT

public class AdminRepository<T> : Repository<T> where T: class
{
    static AdminDataContext dc = new AdminDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["MY_ConnectionString"].ConnectionString);

    public AdminRepository()
        : base( dc )
    {
    }

I also have a datacontext which was created using Linq2SQL.dbml class.

So now I have a standard repository implementing standard calls like All and Find and in my AdminRepository I have specific calls.

Doesn't answer the question of DRY though I don't think.

2
ответ дан 24 November 2019 в 08:07
поделиться

получить инструкции DROP с помощью

python manage.py sqlclear app_name

then попробуйте

python manage.py dbshell

и выполните инструкцию DROP

извлечь http://docs.djangoproject.com/en/dev/ref/django-admin/

-121--1056607-

Я знаю, что я немного опоздал, но это мой рабочий процесс для избирательного слияния

#make a new branch ( this will be temporary)
git checkout -b newbranch
# grab the changes 
git merge --no-commit  featurebranch
# unstage those changes
git reset HEAD
(you can now see the files from the merge are unstaged)
# now you can chose which files are to be merged.
git add -p
# remember to "git add" any new files you wish to keep
git commit
-121--1751739-

Вот пример общей реализации репозитория с использованием FluentNHibernate. Он способен сохранять любой класс, для которого вы написали мэппер. Он даже может генерировать базу данных на основе классов mapper.

1
ответ дан 24 November 2019 в 08:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: