Class linking best practices in C# - c#

First off, EF is not an option for our development environment so please no "just use EF" answers ...
I think this is a pretty standard dilemma so I'm sure there must be a way that most Pros do it that I just have not stumbled across ... so I'm out here hoping y'all can show me what it is.
Let's say you have the following database tables:
tblCompanies
ID
NAME
tblDepartments
ID
COMPANY_ID
NAME
tblEmployees
ID
DEPARTMENT_ID
FIRSTNAME
LASTNAME
... what's the best way to represent this in Classes within your code?
I assume the best way is like this:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public List<Department> Departments { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set;}
public string LastName { get; set; }
}
I believe that to the be the "OOP Proper approach" to this. However, what seems to always happens is something like this:
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public int CompanyID { get; set; }
public List<Employee> Employees { get; set; }
}
... mainly because when you pull just a Department from the database you are only going to have Company ID, not all the other attributes needed to fully populated an instance of the Company class.
(I've used a pretty vanilla example here but the one I'm actually tackling in my current project has 3 fields that it uses to link the data together so the thought of having the same 3 fields in several classes seems wrong to me)
Is there a Best Practice for these scenarios? As much as I don't like the thought of storing the same data in multiple classes just out of laziness, I also don't like returning an instance of a class with just one of its fields populated because that's all I had at the time.

This is a common problem, and one that ORMs try to solve. To be sure it isn't an easy one depending on what your wants are and what your constraints are.
There are only two fundamental options to keep one copy of the information. Lazily load the data as requested or load it all to begin with (Greedy load). Otherwise you have to duplicate the data.
With lazy loading you basically set things up such that when navigating into a property you make a call to the database and grab the information needed to load the entity representing the property you are accessing. The tricky part to watch with this is the SELECT N + 1 problem. You experience this problem when you end up iterating a set of parent entities and trigger lazy loads on every child entity, thus resulting in N+1 calls to the database to load a set of entities (1) and their children (N).
Greedy loading basically says load everything you need to start with. ORMs (where they work) are nice because they take care of many of the details via LINQ and create solutions that can be performant and maintainable usually along with the ability of allowing you to manipulate the usage of Greedy and Lazy Loading.
Another important gotcha is many to many relationships. You need to make sure not to have circular initialization, and get all the baggage of circular dependencies. There are surely many more I have missed.
In my humble opinion I am not so sure there is a best practice as much as there are practices with some of them bad - nothing is perfect. You can:
Start rolling your own object relational mapper allowing you to get rid of the duplicate ID
Use a lighter ORM framework to handle some of this allowing you to get rid of the duplicate ID
Create specialized queries to load aggregations of data allowing you to get rid of the duplicate ID (* cough * DDD)
Just keep the duplication of the ID like you mention above and not worry about creating an explicit relational model in your domain.
This one is on you to choose what is best based on your constraints. This is a deep topic and my experience is limited...so take what I am saying with alot of salt.

I don't think there's a "best practices" manual for this kind of things, and surely it depends on how your classes are going to be used. But in my personal experience, I have ended up following this approach:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<Department> GetDepartments()
{
// Get departments here
}
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
protected int CompanyID { get; set; }
private Company _Company;
public Company Company
{
get
{
// Get company here
}
}
public IEnumberable<Employee> GetEmployees()
{
// Get employees here
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
protected int DepartmentID { get; set; }
private Department _Department;
public Department Department
{
get
{
// Get department here
}
}
public IEnumberable<Employee> GetEmployees()
{
// Get employees here
}
}
In some cases I have exposed some of the "navigation" properties of my classes as public (like CompanyID and DepartmentID) to prevent the instantiation of a new class to get a value that has been loaded already.
As others have noted, you could also simulate "lazy loading", but this will require some extra effort from your part.

I would think it depends on requirements. Do you need to traverse upward (get company from department, department from employee, etc). If you do, then it is best that you provide a means of doing that. Ideally that would be something like a Company or Department property, of course you wouldn't want to get data you don't really need, so you'd likely keep a private company id and have a public getCompany function which queries for the data.

I believe that this is not a really OOP question, in your case you just have an database model (database representation in classes) which does not contain any logic and all the classes are used as structs, and this is a right way to map your database to classes - structs. So in your next module which will represent the logic of your program you have to map your database module to the real classes which will contain the logic (I mean methods which will implement it) of course if you really need them. So in my opinion the OO question should be in the logic part of your application. On the other hand you could take a look on nhibernate and how the mapping done in there it will give you a hint for the bes database model implementation.

I believe this is what your classes would look like in NHibernate:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public IList<Department> Departments { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public Company Company { get; set; }
public IList<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set;}
public string LastName { get; set; }
public Department Department { get; set; }
}
Note that there is a way to navigate from Employee to Department and from Department to Company (in addition to what you already specified).
NHibernate has all kinds of features to make that just work. And it works very, very well. The main trick is run-time proxy objects to allow for lazy loading. Also, NHibernate supports a lot of different ways to eager and lazy load just exactly how you want to do it.
Sure, you can get these same features without NHibernate or a similar ORM, but why wouldn't use just use a feature rich mainstream techology instead of hand coding your own feature poor custom ORM?

There is another option. Create a 'DataController' class which handles the loading and 'memoization' of your objects. The dataController maintains a dictionary of [CompanyIDs, Company objects] and [DepartmentIDs, Department objects]. When you load a new Department or Company, you keep a record in this DataController dictionary. Then when you instantiate a new Department or Employee you can either directly set the references to the parent objects OR you can use a Lazy[Company/Department] object and set it using a lambda (in the constructor) which will maintain the scope of the DataController without it being referenced directly inside the objects. One thing I forgot to mention, you can also place logic in the getter / get method for the Dictionaries that queries the database if a particular ID is not found. Using all of this together allows your Classes (Models) to be very clean while still being fairly flexible as to when / how their data is loaded.

Related

C#/ASP.net classes uml diagram

Can somebody explain this to me? How it would work and what it does. I am creating a c# student registration form.
I created my class:
public class Course
{
public string Number { get; set; }
public string Name { get; set; }
}
Just confused on the list part and what it does....
The goal is to think in an object oriented paradigm. Your course will have the following:
Number (Id)
Name
List Of Students (Collection)
Based on the following description, your model isn't complete. As I indicated above, think in an object oriented paradigm. If I have a school, how would I govern the courses and students?
public class Student : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Student> Students { get; set; }
}
The initial foundation exist, for every course I'll have a collection of students enrolled in the course. That would be the foundation, to build upon the example though:
How would the model change, if I have to track courses by school?
How would the model change, if I need to append a numeric grade for a student?
These questions, will impact your model's. As I denoted above, if you think in the paradigm of objects and how they relate together it will help you build your initial objects but the relational data tables in the database as well.
Also, without the entire diagram we would be assuming or inferring our own interpretation, hopefully this will assist you.

MongoDb relationship

In my Domain layer, I have the following two Entities for mongodb collections
public class League
{
public ObjectId Id { get; set; }
public string LeagueName { get; set; }
public List<ObjectId> Teams { get; set; }
}
public class Team
{
public ObjectId Id { get; set; }
public string UserId { get; set; }
public string Teamname { get; set; }
public int Rating { get; set; }
}
Service layer
private static IEnumerable<ObjectId> GetTeamsNotInSeason(Season season, IEnumerable<Team> teams )
{
var teamsInSeason = season.Leagues.Where(x => x.Teams != null).SelectMany(x => x.Teams).ToList();
return teams.OrderByDescending(x => x.Rating).Select(x => x.Id).Except(teamsInSeason);
}
So each League has a list of Teams, referencing use ObjectId. But that means in my service layer it needs to know about ObjectId, hence I have to have a mongoDb reference in there. So my question is, do you have to store relationship references as a type of ObjectId, What is the normal standard for relationships in mongodb? Within c#
Yoe need to analyze first the reads vrs the writes that you are going to have in the database.
Some basic rules:
If you'll perform more reads than writes then embed as much as you can, that will improve read performance and allow to read data in a single call to the database. The choice embed or not is usually made on how data will grow, change over time and how you gonna query it.
Consider de-normalization of data for fast querying, sometimes duplicated name on other collection is not that bad as long you avoid to do multiples queries to the database.
other notes:
Denormalization always add some overhead to writes, but increase read speed. For your case I assumed will be more reads than writes and heavily used denormalization.
When reading data consider excluding fields from object when you no need them, proyections doesn't work very well on c# using linq but there are some workarounds for this particular issue so just be aware of it.
EDIT:
if you plan to have more reads than writes probably you can have a single collection with all the imofrmation.
public class League
{
public ObjectId Id { get; set; }
public string LeagueName { get; set; }
public List<Team> Teams { get; set; }
}
EDIT:
The best datatype to creating references between objects is ObjectId, however I'd be great for you to check the escenarios for data modeling in the case you haven't looked at it.

NHibernate - Add Comment Property to Entities (Store with Join)

I have a system where I need to be able to add a Comment field onto Customer and Location models but I cannot touch the schema of the existing tables. However, I can add a Comments table. I have simplified this example. We would like the ability to add this Comment to more models moving forward they all use a Guid as Id.
This existing system is a 3rd party system with its own data access layer.
We are just starting to get into NHibernate. From what I can tell it looks like a Join map.
Example:
public class Customer
{
public Guid Id { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Comment { get; set; }
}
public class Location
{
public Guid Id { get; private set; }
public string Name { get; private set; }
public string Address { get; private set; }
public string Comment { get; set; }
}
Note: we are sure we want the Comment as a 1-to-1 relationship and not a 1-to-many.
How do I configure a separate table just capture Id and Comment? I'm looking for the right terminology to use. I'm looking for examples with XML (and if possible Fluent config). I would like to keep the Comments for all objects in one table. Thanks.
If you can add Comment table (and corresponding key columns in the existing tables) than fluent mapping can look like
public class CustomerMap : ClassMap<Customer>{
public CustomerMap(){
//...other columns mappings
References(c=>c.Comment).Column("CommentId");
}
}
And repeat it for other entities as well. You can set desired fetch-mode(join) and other action there as well. I have wrote References there (so many-to-one) but if you need one-to-one mapping it is not a big difference
If you can't change the database schema your options are very limited.
MAYBE, you can do it using the mapping.
Take a look here:
http://ayende.com/blog/3961/nhibernate-mapping-join
Try to use the same column name in mapping for all entities.

Modeling Localized Data in Entity Framework Code First

I'm trying to create a domain model that supports localization. I've looked at some SO questions that were asked before but I did not really like the solutions proposed. For example, in this question I think it's an overkill to create an additional entity for each and every entity that should support localized data. I'd rather have the localized data for all of my entities be in one entity whose sole purpose is to hold the localized data.
My proposed solution is as follows:
Create a LocalizedEntity class from which all of the entities that should support localization would inherit.
Create a LocalizedData entity which will basically hold the actual localized values by culture.
UPDATE:
public class LocalizedEntity
{
public String Code { get; set; }
}
public enum ResourceType
{
CityName,
CityDescription,
StreetName,
AreaName
//others...
}
public class LocalizedData
{
public Int32 Id { get; set; }
public String Code { get; set; }
public ResourceType Type { get; set; }
public Int32 CultureId { get; set; }
public String Value { get; set; }
}
public class City : LocalizedEntity
{
public Int32 Id { get; set; }
public virtual ICollection<Area> Areas { get; set; }
//others
}
Accordingly, the business layer will fetch the localized entity, and then go and get its localized value from the LocalizedData entity by Code and ResourceType. And of course, in order to avoid many round-trips to the database, some caching could be appropriate.
Your thoughts?
What you're modeling is EAV (entity-attribute-value) storage. Very soon your "resource type" enum will overflow into unmanageable state. To add injury to insult, that's pretty uneffective in terms of database structure.
If you want localization that's still maintainable in terms of db usage of code readability, you should localize entities, not properties, which means that each row in database should hold entity id, language id and data for that entity in that language.

Entities Architecture

Using VS2010, .NET4.0, MVC3, EF4.1 Code-First
I have this POCO entities:
public class XBLContent
{
[Key]
[StringLength(36, ErrorMessage="Must have 36 characters")]
[Required(ErrorMessage="Must have a unique GUID")]
public string GUID { get; set; }
public int Price { get; set; }
public float FileSize { get; set; }
public virtual ICollection<XBLRegionalContent> RegionalInfo { get; set; }
public string RelatedGameId { get; set; }
[ForeignKey("RelatedGameId")]
public virtual XBLContent RelatedGame { get; set; }
}
public class XBLRegionalContent
{
[Key, Column(Order = 0)]
public string ContentId { get; set; }
[ForeignKey("ContentId")]
public virtual XBLContent Content { get; set; }
[Key, Column(Order = 1)]
public string RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual XBLRegion Region { get; set; }
public string Name { get; set; }
}
public class XBLRegion
{
[Key]
[StringLength(5, ErrorMessage="ID must have 5 characters")]
[Required]
[RegularExpression(#"[a-z|A-Z]{2}-[A-Z|a-z]{2}")]
public string ID { get; set; }
public string Country { get; set; }
public string Language { get; set; }
}
Relationships:
One XBLContent has many XBLRegionalContent;
One XBLContent can be related to another XBLContent(most of them are not);
One XBLRegionalContent has one XBLContent and one XBLRegion;
One XBLRegion has many XBLRegionalContent;
The Context objetc is really simple:
public class XBLContentContext : DbContext
{
public DbSet<XBLContent> XBLContents { get; set; }
public DbSet<XBLRegionalContent> XBLRegionalInfos { get; set; }
public DbSet<XBLRegion> XBLRegion { get; set; }
public XBLContentContext() : base("XBLToolsDB")
{
}
}
I'm using XBLContent as my main business object and maybe that is not the best idea. I think there is something wrong with the architecture I designed because I'm having trouble to send information to the View and filter, sort, etc.
Now, I'm using Telerik grid and when I try to sort by a navigation property field I get an error saying that "No property or field exist". Maybe I should not use XBLContent as my main business object, or create a ViewModel containing all needed fields and send it to the View. Or create one single entity that splits into two EF tables(I don't know if that is possible or how to achieve that).
I'm just padawan in .NET and need some Jedi Masters advice.
I need contents that can have multiple translations.
How to best achieve this goal?
this should fix your problem.
http://weblogs.asp.net/manavi/ A great resource for beginners and i can see you have used a lot of annotations ,so a little bit of fluent api would make your concepts stronger.
I'm assuming you're using the Telerik MVC Extensions here, but if you are using a different product please let me know and I'll re-answer accordingly :)
In regards to the Grid what kind of binding are you utilizing? If you are using regular server or ajax binding then you might run into some issues when binding to a navigational property, as by default these bindings only work with primitive (int, string etc.) types. However, there is such a thing as custom binding which allows you to take full control over paging/sorting/filtering. I believe this could account for why you are getting this error, as the automatic LINQ expressions cannot find the specific field you are looking for. Here are two demos (which have source code for both WebForms and Razor ViewEngines) that can help with setting up custom binding. It's just a little more work than the automatic binding, but should still work (note that these examples are using Razor):
Ajax Binding
Server Binding
The added benefit here is that you get to control everything on your own, which can be quite nice in somewhat more complex scenarios. If you're already using custom binding, and/or if the links there do not help let me know. It could also be helpful to have the code for the Telerik Grid.
I've resolved these kinds of issues by normalizing the results like:
from r in ctx.XBLContents
select new
{
r.Guid,
RelatedGuid = r.RelatedGame.Guid
};
Essentially creating an anonymous classes that is more denormalized has worked for me to work around these kinds of issues, where the results denormalizes those navigational properties too.
HTH.

Categories