I'm busy learning Prism 4 and the in and outs of everything, but I've yet to see a tutorial/walk through on what I want to accomplish. Hoping someone here has or has worked on a similar project.
My application is a basic CRUD application that I've broken down in to separate areas of concern and hence modules. However, I want all the modules to share one common local SQL Express database. The database will start off with just a limited number of tables to start with and each module will check the database for the tables it needs and if they are not there, create them. How can I go about accomplishing this?
I've thought about just adding in all my tables initially but this seems to break the principal of modularity in my mind. Perhaps my thinking is wrong, but what would be the point of loosely coupled modules if the database is already fully aware and strongly coupled to a given module from db creation?
Looking for some insight.
You seem to be asking two questions. The first is: How can I use PRISM to ensure that my module specific schema exists in the database, and if not, create it. The second question is: how can I best structure my data layer such that it is decoupled in a modular application.
To answer your first question about how to do the module schema check, I say this:
If you’ve been going through Prism, you’ve no doubt thought up a couple of ways to accomplish it. As with anything in programming, there are many ways to accomplish it. If I needed to do this with Prism, I’d probably do the following: Create a class (MyPlugInModule.cs) in my module assembly which implements the Microsoft.Practices.Prism.Modularity.IModule interface. I would then put code in either the constructor, or in the Initialize method, which checks the database to see if the module schema exists. If it does not, then create it.
To answer your second question about how to best structure your data modularity, I say this:
Like Goblin says, it really depends on what type of modularity you are trying to accomplish. If you are selling this application and you want to sell modules as independent packages, then you probably do not want to create a data model to support a package until the end user has paid for it.
You should be able to use Entity Framework to ensure that your modules are able to share entities with the base application modules. Additionally, depending on what your requirements are, or if your architecture will allow, you may want to abstract your model/data layer into assemblies that are not perfectly aligned with your modules. This will reduce code duplication and dependencies.
On an application that I am currently working on, we're using WPF with MVVM, PRISM with MEF, and WCF data services. Our client modules share a data assembly which communicates with our main data service endpoint that sits on top of the base application model (authentication/role tables, application data, etc). When tables in our database are created that are specific to the domain of a module, a new model and service endpoint are created on the server, and a separate assembly is created on the client to communicate with the data model.
If the module specific model changes, only the affected components have to be changed, since module specific data is encapsulated in its own service and client assembly. It is a better option from an isolation standpoint for testing, security, etc. The downside of course is if the base application model changes, all of the associated module specific implementations have to be updated.
But again, it really depends on your requirements. If you stick with PRISM 4 with MEF, modular design patterns, and entity framework 4, you should be able to come up with a good solution that is modular without being tightly coupled.
If your modules are truly independent - how about a database per module? If you need foreign keys between your modules - they are in essense not really encapsulated - and I'd put the whole database into play from the get-go. Much easier to keep the schema up-to-date betweeen updates.
Modularity comes in many flavours - business-perspective (pay-per-module), modularity in terms of responsibilities etc. etc.
My 5 cents :)
This response is for anyone coming here that wants to see code that hooks up a local database. It works for me, not sure if it's best practice or not.
I'm using prism and I needed to get my database working. Here is what I did. The Entity Framework seems to "just work" for putting the database somewhere.
Bootstrapper.cs file:
....
protected override void ConfigureContainer() {
base.ConfigureContainer();
// Register my navigation
Container.RegisterType<IAppDatabaseContext, AppDatabaseContext>();
}
....
My AppDatabaseContext.cs file:
public class AppDatabaseContext : DbContext, IAppDatabaseContext {
DbSet<MyModelOne> MyModelOnes { get; set; }
DbSet<MyModelTwo> MyModelTwos { get; set; }
DbSet<MyModelThree> MyModelThrees { get; set; }
}
public interface IAppDatabaseContext {
DbSet<MyModelOne> MyModelOnes { get; set; }
DbSet<MyModelTwo> MyModelTwos { get; set; }
DbSet<MyModelThree> MyModelThrees { get; set; }
int SaveChanges();
// Other methods needed to use the DbContext
}
In one of my ViewModels:
public ConstructorMethod(IEventAggregator eventAggregator, IAppDatabaseContext dbContext) {
_db = dbContext; // I then use this in my Observed Properties
}
Related
I am designing a new application and plan to use CakePHP 3. Typically when designing an application on top of a PHP MVC framework (CakePHP, CodeIgniter) I have implemented a separate set of classes that represents the business layer or service layer (depending on what words you like to use). Thus, the stack:
-- Views
-- Controllers [really just another part of the views; code-behind from C# land]
-- Business layer [where business logic goes, because business logic spans multiple domain objects and isn't appropriate for a single Model/DAO]
-- Models [aka Data Access Objects]
...with domain objects (now "Entities" in Cake PHP 3) traversing between the layers.
I see that Cake PHP 3 still lacks any concept of a business layer in the default architecture, which is disappointing but hardly insurmountable. But after all that, my question is actually rather pedestrian. It is: Where do I put MyBusinessLayer.php? In Cake PHP 2 I would have put it in app/Lib, and loaded it up with
App::Import('Lib', 'MyBusinessLayer');
What is the equivalent in CakePHP 3, and how do I load it? I realize I could just stick it any old place and require_once(), but I wonder if there is a way that is more consistent with Cake PHP standards. This question could just as easily apply to a ten-line utility class, but in my case it applies to the business layer.
Your business layer can be put anywhere under the src directory. For example you could have a folder called src/Core or src/MyBusiness or you could have multiple folders like src/Command src/Handler, it is up to you how you want to architecture your application other than serving a web request or accessing data in the database.
Classes put inside any folder inside src will be automatically loaded if given the right namespace.
// in src/MyBusiness/BusinessRules.php
namespace App\MyBusiness;
class BusinessRules
{
...
}
You can automatically load this class from another use the use keyword:
// In another file
use App\MyBusiness\BusinessRules;
...
$rules = new BusinessRules();
CakePHP does not make assumptions for you when it comes to this kind of objects as they usually are very specific to your application. It does offer a wide variety of tools that can help you build a better architecture more rapidly and with less code. For example using the Event system for Aspect Oriented Programming, the configuration traits for creating adaptable implementations or the Collection library for working with data using a functional approach.
There is a class which is responsible for communicating with server and hold the most of data. In client, There is without database.
VMs will get required data from the class instance.
Now I'm doubt where the class instance to be put, in App class? in ViewModelLocator class? or others?
Are there some opensource projects like this?
This really depends on the size of your project. If I write a small MVVM application (with a single project), then I create the following folders:
Converters (if required)
DataTypes - some developers put this into 'Models' also, but I prefer to separate them
Models - I use this for data access classes <<< put your class in here <<<
Utilities - some developers call this 'Services'
ViewModels
Views
If the application is large, then I create separate projects with these names instead and add a few others as well.
So, I don't know if the question is explicit enough, but here's my problem:
I am writing a small application in VB.Net, that retrieves information from a website and present it to the user. Basically, I have written a class, which has a Get(URL) method which retrieves the webpage, reads it and populates the various Properties (Read-only) of the object.
This class works OK.
Now, I would like to store that information in a Database (I'm using Access for now), so that I can read the data from the DB, if the class gets called for a known URL. As I'm fairly new to OOP and completely new to DB usage in desktop applications (no problems in designing the DB though), I am not sure on how to proceed:
Should I put the database code in my existing class?
Should I create an extended class based on the existing one, adding the DB code?
Should I create a completely different class for the DB data and put the switch logic (read from DB or from web) in my application?
...
I realize that my question may sound silly to the most experienced of you, but I'm new to this and I would really like to learn how to do things the right way the first time!!!
Thanks!
This is what I would do:
Create a new class for the database code, and create an
interface for it that it implements.
Then create another class that has the code to fetch the web data. Make it implement the same interface.
Now you can subsitute either class to do your data access from your controller class.
Also, I usually put database and data access in separate projects from my service and ui classes, which are in their own classes, but that might be overkill for your situation.
If you'd like to read more on the subject, look up n-tier application design. The tier you're talking about here is data access.
http://en.wikipedia.org/wiki/Data_access_layer
I'm new to .Net and trying to learn things. I'm trying to develop a Prism4 WPF app with
Visual Studio CSharp 2010 Express Edition,
Prism v4,
Unity as IoC,
SQL Server CE as data store.
I've studied a lot(?) and infuenced by this and this among others, and decided to implement MVVM, Repository and UnitofWork patterns. This application will be a Desktop application with a single user (me:-)
So, I've created a solution with the following projects:
Shell (Application Layout and startup logic)
Common (Application Infrastructure and Workflow Logic)
BusinessModuleA (Views and ViewModels)
BusinessModuleA.Model (Business Entities - POCO)
BusinessModuleA.Data (Repositories, Data Access (EF?) )
BusinessModuleB (Views and ViewModels)
BusinessModuleB.Model (Business Entities - POCO)
BusinessModuleB.Data (Repositories, Data Access (EF?) )
My questions are:
Which projects should reference which projects ?
If I implement Repositories in 'BusinessModuleX.Data', which is
obvious, where should I define IRepositories ?
Where should I define IUnitOfWork and where should I implement UnitOfWork ?
Is it ok if I consume UnitOfWork and Repositories in my ViewModels ?
Instict says it is bad design.
If (4) above is bad, then ViewModel should get data via a Service
Layer (another project ?). Then, how can we track changes to the
entities so as to call the relevant CRUD methods on those objects at the Service Layer?
Is any of this making any sense or am I missing the big picture ?
Ok, may be I've not made myself clear on what I wanted exactly in my first post. There are not many answers coming up. I'm still looking for answers because while what #Rachel suggested may be effective for the immediate requirements, I want to be careful not to paint myself into a corner. I've an Access Db that I developed for my personal use at Office, and which became kind of a success and now being used by 50+ users and growing. Maintaining and modifying the access code base has been fairly simple at the beginning, but as the app evolved, began to fall apart. That's why I have chosen to re-write everything in .Net/Wpf/Prism and want to make sure that I get the basic design right.
Please discuss.
Meanwhile, I came up with this...
First off, I would simplify your project list a bit to just Shell, Common, ModuleA, and ModuleB. Inside each Project I'd have sub-folders to specify where everything is. For example, ModuleA might be separated into folders for Views, ViewModels, and Models
I would put all interfaces and global shared objects such as IUnitOfWork in your Common project, since it will be used by all modules.
How you implement IUnitOfWork and your Repositories probably depends on what your Modules are.
If your entire application links to one database, or shares database objects, then I would probably create two more projects for the DataAccessLayer. One would contain public interfaces/classes that can be used by your modules, and the other would contain the actual implementation of the Data Access Layer, such as Entity Framework.
If each Module has it's own database, or its own set of objects in the database (ie. Customer objects don't exist unless you have the Customer Module installed), then I would implement IUnitOfWork in the modules and have them handle their own data access. I would probably still have some generic interfaces in the Common library for the modules to build from though.
Ideally, all your modules and your Shell can access the Common library. Modules should not access each other unless they build on them. For example, a Customer Statistics module that builds on the base Customer module should access the Customer module.
As for if your ViewModels should consume a UnitOfWork orRepository, I would have them use a Repository only. Ideally your Repository should be like a black box - ViewModels can Get/Save data using the Repository, but should have no idea how it's implemented. Repositories can get the data from a service, entity framework, direct data access, or wherever, and the ViewModel won't care.
I'm no expert on design architecture, however that's how I'd build it :)
I would highly recommend you to get the Introduction to PRISM and Repository pattern inside Design Patterns Library training videos. They are great ones. Hope it helps
I'm using NHibernate on a project and I need to do data auditing. I found this article on codeproject which discusses the IInterceptor interface.
What is your preferred way of auditing data? Do you use database triggers? Do you use something similar to what's dicussed in the article?
For NHibernate 2.0, you should also look at Event Listeners. These are the evolution of the IInterceptor interface and we use them successfully for auditing.
[EDIT]
Post NH2.0 release, please look at the Event Listeners as suggested below. My answer is outdated.
The IInterceptor is the recommended way to modify any data in nhibernate in a non-invasive fashion. It's also useful for decryption / encryption of data without your application code needing to know.
Triggers on the database are moving the responsibility of logging (an application concern) in to the DBMS layer which effectively ties your logging solution to your database platform. By encapsulating the auditing mechanics in the persistance layer you retain platform independance and code transportability.
I use Interceptors in production code to provide auditing in a few large systems.
I prefer the CodeProject approach you mentioned.
One problem with database triggers is that it leaves you no choice but to use Integrated Security coupled with ActiveDirectory as access to your SQL Server. The reason for that is that your connection should inherit the identity of the user who triggered the connection; if your application uses a named "sa" account or other user accounts, the "user" field will only reflect "sa".
This can be overriden by creating a named SQL Server account for each and every user of the application, but this will be impractical for non-intranet, public facing web applications, for example.
I do like the Interceptor approach mentioned, and use this on the project I'm currently working on.
However, one obvious disadvantage that deserves highlighting is that this approach will only audit data changes made via your application. Any direct data modifications such as ad-hoc SQL scripts that you may need to execute from time to time (it always happens!) won't be audited, unless you remember to perform the audit table insertions at the same time.
I understand this is an old question. But I would like to answer this in the light of the new Event System in NH 2.0. Event Listeners are better for auditing-like-functions than Interceptors. Ayende wrote a great example on his blog last month. Here's the URL to his blog post -
ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx
As an entirely different approach, you could use the decorator pattern with your repositories.
Say I have
public interface IRepository<EntityType> where EntityType:IAuditably
{
public void Save(EntityType entity);
}
Then, we'd have our NHibernateRepository:
public class NHibernateRepository<EntityType>:IRepository<EntityType>
{
/*...*/
public void Save ( EntityType entity )
{
session.SaveOrUpdate(entity);
}
}
Then we could have an Auditing Repository:
public class AuditingRepository<EntityType>:IRepository<EntityType>
{
/*...*/
public void Save ( EntityType entity )
{
entity.LastUser = security.CurrentUser;
entity.LastUpdate = DateTime.UtcNow;
innerRepository.Save(entity)
}
}
Then, using an IoC Framework (StructureMap, Castle Windsor, NInject) you could build it all up without the rest of your code every knowing you had auditing going on.
Of course, how you audit the elements of cascaded collections is another issue entirely...