Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I've been wondering if it was possible to have a MVVM architecture with no reference of the ViewModel in the ViewController, I understand that it needs this reference because ViewModel works as a DataContext but I'm trying to figure it out, I want to isolate as much as possible responsibilities and dependency injection between Views and ViewModel. Have you any idea on how it is possible to achieve that in a MVVM pattern in Swift?
Let`s have a look your ideas step by step in MVVM aspects
"No reference of ViewModel in the ViewController or View."
It is not a good idea. By referencing to viewmodel in your view you notify viewmodel. That does not mean you need to do logic in your view or viewcontroller.
"I want to isolate as much as possible responsibilities and dependency injection between Views and ViewModel."
Exactly! ViewModel handle the all logic. View needs to be lightweight and no logic. There are many approaches in terms of binding your view to viewmodel and separating concerns. I can give an example below.
Move your datasource from view to viewmodel. For example if you working a tableviewdatasource then make a generic datasource( you can find many examples by google ) for your tableViewDataSource. Your viewcontroller will be something like below:
private var dataSource : YourTableViewDataSource<YourData>!
private var yourViewModel: YourViewModel!
self.yourTableViewDataSource = dataSource
func updateDataSource() { }
Related
First of all i know MVC well and have been using it in project but when it comes to organizing classes and there role i am bit not sure of there proper implementation. Lets take a scenario to proceed with:
A sample which will display All Employee and Department. Data will be fetched from Web Services(Json) and will be stored as offline(Core Data).
So MVC pattern would be:
View will be my storyboard with Employee and Department UIViewController.
Controller will be EmployeeViewController.swift and DepartmentViewController.swift
Model will be Employee.swift and Department.swift
class Employee: NSObject {
var name: String?
}
class Department: NSObject {
var departmentName: String?
}
ServiceManager which will make calls to the web service.
ParseData which will parse the web service response and convert it into Employee and Department objects
CoreDataManager is singleton class to manage CRUD operation on offline DB.
Here are series of question on the above scenario which i have:
Is my understanding correct? Is the structure which i am trying to build follows proper MVC?
How the controller will interact with these components (Service Manager, ParseData, CoreDataManager). Should there be another class which will facilitate the communication between controller and data management(if controller does this then it will a tightly-coupled structure and massive as well).
Should Model be having any code other then property and initialization method as most of the model which i have seen only have property declaration?
Should there be separate UIView classes instead of storyboard to create a proper MVC structure?
Is my understanding correct? Is the structure which i am trying to
build follows proper MVC?
First I will say that "proper" MVC will depend on who you're asking. Its origin is commonly attributed to Trygve Reenskaug when he introduced this into Smalltalk in the 70's. However, his type of MVC was widely different from the bloated versions most commonly used today. The modern way of thinking about MVC is
Model = mostly a dumb class which primarily encapsulates data
View = whatever we present on the screen
Controller = the big lump of code that does almost everything,
sometimes offloaded by a manager class or two of some sort
Reenskaug, however, would have a model and a view and a controller for a button. For a label. For a field. I'm not saying that is what we should strive for, but there should be better ways to structure a project than using the Massive ViewController pattern (as it is jokingly referred to in the iOS community).
Luckily, there are.
Uncle Bob is preaching Clean Architecture. There are several implementations of this, and various people have made their own implementations of this for iOS, like VIPER and Clean Swift.
How the controller will interact with these components (Service
Manager, ParseData, CoreDataManager). Should there be another class
which will facilitate the communication between controller and data
management(if controller does this then it will a tightly-coupled
structure and massive as well).
Following the principles of Clean Architecture, you should encapsulate these functionalities into layers, in a way that enables you not just to split the code into multiple components, but also enables you to replace them with other components when that is needed. (But yes, at the very least avoid putting all of this in your controller!)
Should Model be having any code other then property and initialization
method as most of the model which i have seen only have property
declaration?
Again, there is not a single answer here. Some proponents of "real" OOP will say that each object should be self-served (i.e. a model object should know how to persist itself), while others extract the knowledge of such operations into "managers". Putting code to persist an object into the object could mean littering persistence functionality into many objects, or require you to rely on subclassing or other solutions to avoid this coupling.
Should there be separate UIView classes instead of storyboard to
create a proper MVC structure?
Storyboard or not does not determine whether you're using "proper" MVC. Also, what kind of class you're choosing (UIView or UIViewController) to represent the View is also not important. Your ViewController can be dumbed down to such a degree that it contains no logic (forwarding the logic that it DOES have to another class, i.e. the Presenter in VIPER).
I would recommend reading about the Clean Architecture and maybe watch a video of Uncle Bob explaining it, read other people's reports on implementing it, and then consider whether MVC is the correct pattern for your iOS project.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Dependency Injection design pattern is said to be helpful for loose coupling, however I cannot understand how it can be achieved since the calling object has to pass the dependencies in the constructor to the service?
Please explain?
I cannot understand how it can be achieved since the calling object has to pass the dependencies in the constructor to the service?
The calling object does not have to pass the dependencies in the constructor of the service.
The calling object will have the an implementation of the service injected into its constructor like this:
public class CallingObject
{
private readonly IService m_Service;
public CallingObject(IService service)
{
m_Service = service;
}
public void DoSomething()
{
m_Service.AskForService();
}
}
The entity that is responsible for wiring all objects together is the composition root.
So its the composition root that has to pass the dependencies into the constructor to the service.
The only disadvantage I found is that highlights design problems and causes many programmers to blame dependency injection for the design problems.
I'm sure that Krzysztof Koźmic can explain it better than me. Please read this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have one repository that internally (on some methods) will need to use another repository.
I am trying to put all dependencies on the constructor, passing interfaces as arguments. However I don't know how to deal with this specific scenario.
Should I pass also this repository as an argument? Even though I will not use it on every method inside?
Thanks
You don't have any problem for making a repository depend on another repository. And, yes, pass it as a parameter
I'm assuming you're making dependency injection, specifically constructor injection.
Instancing an extra repository, which some times won't be used (because not all method use it) it's not such a terrible overhead that makes you to avoid it. If it was a more expensive resource (like opening a file or DB connection) you could use some alternative technique. For example expose the second repository in a property with a backing field which is populated in the first call to the property getter using service location, i.e. finding it directly in your container, or an smarter solution, provided but some of the DI frameworks, which does this kind of thing automatically, like Unity's Lazy and similar solutions.
But I insist, in this case, the overhead doesn't justify it.
NOTE: you could also use the property or Lazy technique if you had a dependency loop (circular dependency), to break the loop and make it work. However, int this case, it's much better to refactor your classes, (extracting a thrid class) to avoid the circular references. This is not your case.
I would probably make a service layer on top of the repositories. Inject both repositories into the service layer.
public LibraryCatalogueService {
IBookRepository _books;
IAuthorsRepository _authors;
public LibraryCatalogueService (IBookRepository books, IAuthorRepository authors)
{
_books = books;
_authors = authors;
}
public List<BookWithAuthor> GetBooksWithAuthors()
{
//do stuff to get books and get authors and then join them.
//return the list
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In our Delphi 2007 application we are using a lot of the following constructs
FdmBasic:=TdmBasicData(FindOwnerClass(AOwner,TdmBasicData));
The FindOwnerClass travels the Owner hierarchy of the current component upwards to find a specific class (in the example TdmBasicData). The resulting object is stored in the Field variable FdmBasic. We use this primarily to pass datamodules along.
Example:
When generating a report, the resulting data is compressed and stored in a Blob field of a table accessed through a datamodule TdmReportBaseData. In a separate module of our application, there is functionality to show the data from the report in a Paged form using ReportBuilder. The main code of this module (TdmRBReport), uses a class TRBTempdatabase to convert the compressed blob data into different tables that are usable in the Reportbuilder runtime reportdesigner.
TdmRBReport has access to TdmReportBaseData for all kinds of report-related data (type of report, report calculationsettings, etc). TRBTempDatabase is constructed in TdmRBReport but has to have access to TdmReportBasedata. So this is now done using the construction above:
constructor TRBTempDatabase.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FdmReportBaseData := TdmRBReport(FindOwnerClass(Owner, TdmRBReport)).dmReportBaseData;
end;{- .Create }
My feeling is that this means that TRBTempDatabase knows a lot of its owner, and I was wondering if this is some sort of code smell or Anti-pattern.
What are your thoughts about this? Is this a code smell? If so, what is a better way?
On the description presented here I regard this as mildly smelly. However, it seems easy to fix.
I'd be inclined to pass the dmReportBaseData object into the constructor of any component that needs it. This makes the contract clear at compile time rather than enforcing it at runtime as you currently do.
As it currently stands, the contract you enforce is stronger than it needs to be. Although TRBTempDatabase only requires a dmReportBaseData instance, it will only function if it can get that instance from a TdmRBReport report object.
Making this change would also allow TRBTempDatabase and TdmRBReport to have a divorce and still function successfully. And as #Lieven points out in the comments, this would likely make testing easier.
If all you're doing in a base class is maintaining a reference to a parent object then no, it's not code-smell, it's a perfectly legitimate use. You can explicitly design a base class to carry information about "something that might come later."
If the base class is relying on some characteristic of the derived class that isn't present in itself (i.e. the generalized class relies on a specialization of one of its children) then yeah, that might be a bit funky.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
can someone suggest the ideal Unit test cases that may fit in across each of the layers .
(which otherwise can be called as a standard).
for instance, in an ASP.NET MVC applictaion using a Repository pattern -
Controller - can assert for View names and format of the data returned to the views , from the controller action methods( i couldnt think of more , if u can please suggest).
Services Layer - ?? what can be written. because they in turn depend on the layers underneath.. ( can some one suggest a Unit Case with example for sevices layer)?.
One trivial question to finish off. Irrespective of the layers , the method being tested makes calls to other instance methods/static methods say,
public List<string> MethodUnderTest()
{
instance.SomeOtherMethod();
StaticMethod();
}
in each case it is neccesary to mock the methods calls by moving that to interfaces .? any thoughts on that . ( coz unit Testing by nomenclature should not depend on anything)
Can some
I recommend reading the Art of Unit Testing. It covers this stuff in detail.