iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Hey Guys,Is it possible to use constructor injection on view models, aside from using property injection? Just a thought, please let us know. Thanks!
Could you please provide us with more information regarding your scenario which involves constructor injection on ViewModel? Who knows there is a chance to implement the scenario by using different approach/techniques.
Hi Yudi,
Well, the reason why we're asking is because we are trying to construct our view models with parameters, instead of using property injections. Since we are using old school WCF to retrieve data from the server, we are implementing it this way while we haven't updated to your Enterprise architecture yet, because we started from Crosslight v1. In which it was easier to reference a WCF web service to just grab data by using operation contracts. Or we thought it was easier, and now we are running into problems.
public class ExpenseListViewModel : EditableListViewModelBase<ExpenseItem>{ private readonly IExpenseRepository _expenseRepository; public ExpenseListViewModel(IExpenseRepository expenseRepository) { this._expenseRepository = expenseRepository; } ... }
Now, if this implementation is not a good practice based on Crosslight's architecture, please let us know how we can implement CRUD operations on remote data without using WebAPI. We already have operation contracts within the WCF web service based on CRUD operations (e.g. GetExpenseByID, GetAllExpenses, DeleteExpense, RemoveExpense, etc.). That said, we also have implemented a class within the client (mobile app) that acts as an adapter, which implements asynchronous methods such as GetByIdAsync, GetAllAsync, DeleteAsync. Now, the only piece that's missing is the link between Crosslight and the adapter, which acts as a repository class for the view models.
Do we need to implement IEditableDataRepository interface with EditableListViewModelBase? We were looking at the MyInventory_MasterDetail sample, but turns out to be for local files. There is the MyInventory_WebAPI sample, but this was designed for the Crosslight's Enterprise architecture. Is there any samples that implement other REST services and/or WCF web services, aside from WebAPI? We would very much like to have such examples.
Again, many thanks! As always.
You can safely pass the repository class for the ViewModel by using Injection of Container technique.
The contructor of ExpenseListViewModel should look like following:
public ExpenseListViewModel() { ... this.SourceItems = this.Repository.GetAll().ToObservable(); ... } private IItemRepository Repository { get { if (Container.Current.CanResolve<IItemRepository>()) return Container.Current.Resolve<IItemRepository>(); else return new ItemRepository(); // for designer support } }
The IItemRepository object is registered in the contructor of AppService, such as shown in the following snippet code:
public sealed class MyInventoryAppService : ApplicationServiceBase { public MyInventoryAppService(IApplicationContext context) : base(context) { Container.Current.Register<IItemRepository, ItemRepository>().WithLifetimeManager(new ContainerLifetime()); ... } }
ItemListViewModel.cs and AppService.cs (MyInventory.Core) implements the same technique and can be used as reference. Hope this help.
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname